简体   繁体   中英

Hibernate. Several foreign keys in one table

Have next tables structure in SQL schema :Clients, Employees, Orders. And 3 Entity classes in java code accordingly : Client, Employee, Order.

Both primary id fields from Clients and Employees are in Orders table as foreign keys.

Question is how it should be displayed in java code? As I understand here it should be done smth like adding Set field to Clients and Employees annotated with @OneToMany.

But what should be done in Order Entity and maybe I have to add any additional annotations except @OneToMany?

I think you have some misconceptions about the relational mapping of Hibernate.

If in fact your Orders table have foreign keys of Clients and Employees, then the annotation you are looking for is @ManyToOne

@OneToMany annotation is used when your entity have multiple records referenced by the targeted entity, while @ManyToOne is used when your entity have only one record referencing the targeted entity.

For example:

Orders entity have one reference from Clients and one reference from Employees entities.

In this case, Orders entity could be mapped by the following way:

@Entity
@Table(name = "Orders")
public class Order implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @ManyToOne
    private Client client;
    @ManyToOne
    private Employee employee;

    //getters and setters
}

@Entity
@Table(name = "Clients")
public class Client implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String attribute1;

    //getters and setters
}

@Entity
@Table(name = "Employees")
public class Employee implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String attribute1;

    //getters and setters
}

With the example given above you should be able to make your schema work fine with Hibernate, but for the sake of understanding, let's imagine a scenario where you would need to get all the Orders from a Client, of course you could do it with a query selecting only the Orders inside the Client table, however Hibernate offers the @OneToMany annotation which will give you the possibility to access all the Orders from a Client without the need of a separate query, only by mapping! Let's see an example:

@Entity
@Table(name = "Orders")
public class Order implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @ManyToOne
    private Client client;

    //getters and setters
}

@Entity
@Table(name = "Clients")
public class Client implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String attribute1;
    @OneToMany(mappedBy = "client")
    private List<Order> orders;

    //getters and setters
}

In this example you should be able to get all the Orders from a Client just by calling the get of the "orders" attribute. Please, note that on the @OneToMany mapping we have specified the "mappedBy" attribute as "client", it was needed because we have a bidirectional mapping between Client and Order, in a simple usage of @OneToMany you would not need this mapping.

Important: When working with @OneToMany mapping you would eventually face some lazy fetching problems, in this case I highly recommend you to take a look at this question:

Solve “failed to lazily initialize a collection of role” exception

Also, I think you should start reading more about Hibernate to understand about it's basic concepts, please, check this other question about @OneToMany and @ManyToOne annotations on Hibernate:

Hibernate/JPA ManyToOne vs OneToMany

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM