Hibernate object-relational mapping - annotation

1.OneToOne relationship mapping

OneToOne is a one-to-one mapping, which is simply a master table student and a slave table sciccreditcard. There is a foreign key in the B table associated with the student table. This association can be established either by a foreign key or manually. connection relation.

One-way one-to-one association (excluding primary key association)

This association can only be configured in the PO of the slave table. A brief example is as follows:

  1. @Entity

  2. @Table(name="stucreditcard")

  3. public class StudentCard {

  4. private Integer cardId;

  5. private Integer balance;

  6. private Student student;

  7. ......

  8. @OneToOne

  9. @JoinColumn(name="studentid",referencedColumnName="id")//The studentid here is the name of the foreign key field from the table, and the id is the name of the main table field referenced from the table.

  10. , default as the primary key

  1. public Student getStudent() {

  2. return student;

  3. }

  4. public void setStudent(Student student) {

  5. this.student = student;

  6. }

The above configuration can get the Student instance from the StudentCard instance.

Bidirectional one-to-one association (excluding primary key associations)

The configuration of the two-way association involves one attribute of OneToOne, mappedBy, which is briefly described as follows:

a) Only MapToBy is available on OneToOne, OneToMany, ManyToMany, and this property does not exist in ManyToOne;

b) The mappedBy tag must be defined on the owned side (the owner's, here is the main table), he points to the owning side (the owner, here is the slave);

c) The owner of the relationship is responsible for the maintenance of the relationship and establishes a foreign key at the owner. So use @JoinColumn

d) mappedBy and JoinColumn/JoinTable are always on the mutually exclusive side

e) If the bidirectional relationship does not configure mappedBy, the default primary key is associated (you must configure JoinColumn without configuring mappedBy)

The bidirectional association means that the information associated with the table can be obtained in the PO representing the primary table, and the information associated with the secondary table in the primary table can also be obtained from the PO of the secondary table, as follows:

Main table configuration:

  1. @Entity

  2. @Table(name="student")

  3. public class Student implements Serializable{

  4. private static final long serialVersionUID = 1L;

  5. private Integer studentId;

  6. private String name;

  7. private StudentCard sc;

  8. @OneToOne(mappedBy="student")//The student here is the attribute name of the main table PO from the PO of the table.

  1. public StudentCard getSc() {

  2. return sc;

  3. }

  4. public void setSc(StudentCard sc) {

  5. this.sc = sc;

  6. }

From the table configuration:

  1. @OneToOne

  2. @JoinColumn(name="studentid",referencedColumnName="id")//studentid is the name of the field from the table, id is the name of the field in the main table, where the id can also be the primary key

  1. public Student getStudent() {

  2. return student;

  3. }

  4. public void setStudent(Student student) {

  5. this.student = student;

  6. }

2.OneToMany relationship

One-to-many relationship, for example: an order can include multiple order items (orderItem), but at the same time a line item has one and only one corresponding order, so that the order and line item constitute a The relationship to many.

Configure one-way relationship on one side

The end-end PO only needs to configure the field mapping relationship, and does not need to configure the association relationship.

The one-end configuration association relationship is as follows:

Private Set<OrderItem> orderItems;//association attribute
  1. public void setOrderItems(Set<OrderItem> orderItems) {

  2. this.orderItems = orderItems;

  3. }

  4. @OneToMany(fetch=FetchType.EAGER)

  5. /**

  6. * The order_id below is the table field of the many side;

  7. * The table field with id one party (do not write referencedColumnName="XXX" is the default primary key, you can also specify the field that is not the primary key)

  8. * order_id and id association

  9. */

  10. @JoinColumn(name="order_id",referencedColumnName="id")

  11. public Set<OrderItem> getOrderItems() {

  12. return orderItems;

  13. }

In this way, the PO instance of the many side can be obtained in the one-end PO.

Configure a one-way relationship on the many side

One-side PO only needs to configure the field mapping relationship, and does not need to configure the association relationship.

The many end configuration is as follows:

  1. @Entity

  2. @Table(name="orderitem")

  3. public class OrderItem {

  4. Private order order;//association field

  5. private Integer id;

  6. // private Integer orderId; / / associated field with one table, comment out here, you can try not to comment

  7. // @Column(name="order_id")

  8. // public Integer getOrderId() {

  9. // return orderId;

  10. // }

  11. // public void setOrderId(Integer orderId) {

  12. // this.orderId = orderId;

  13. // }

  14. @Id

  15. @GeneratedValue(strategy=GenerationType.AUTO)

  16. public Integer getId() {

  17. return id;

  18. }

  19. public void setId(Integer id) {

  20. this.id = id;

  21. }

  22. @ManyToOne

  23. /**

  24. * order_id many table fields

  25. * The table field with id one party (do not write referencedColumnName="XXX" is the default primary key, you can also specify the field that is not the primary key)

  26. * order_id and id association

  27. */

  28. @JoinColumn(name="order_id",referencedColumnName="id")

  29. public Order getOrder() {

  30. return order;

  31. }

  32. public void setOrder(Order order) {

  33. this.order = order;

  34. }

}

Configuring OneToMany bidirectional relationships

One-end and many-side have to be configured with associations

One end:

Private Set<OrderItem> orderItems;//association field
  1. public void setOrderItems(Set<OrderItem> orderItems) {

  2. this.orderItems = orderItems;

  3. }

  4. @OneToMany(fetch=FetchType.EAGER,mappedBy="order")//Remember that the two-way association mentioned earlier needs to configure mappedBy?

  1. public Set<OrderItem> getOrderItems() {

  2. return orderItems;

  3. }

Many end:

  1. Private order order;//association field

  2. @ManyToOne

  3. /**

  4. * order_id many table fields

  5. * The table field with id one party (do not write referencedColumnName="XXX" is the default primary key, you can also specify the field that is not the primary key)

  6. * order_id and id association

  7. */

  8. @JoinColumn(name="order_id",referencedColumnName="id")

  9. public Order getOrder() {

  10. return order;

  11. }

  12. public void setOrder(Order order) {

  13. this.order = order;

  14. }