This article will introduce several relational mappings of hibernate: Many pairs of one or one to one or one pairs, many to many.
Many-to-one
Take users and groups as an example, assuming that multiple users correspond to one group, and that the user is one end of the other, and that the group is a single end.
Key code and configuration:
User:
public class User implements Serializable {private static final long Serialversionuid = 1l;private long id;private String Name;private Group group;public Long getId () {return ID;} public void SetId (Long id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public Group Getgroup () {return group;} public void Setgroup (group group) {this.group = group;}}
Group:
public class Group implements Serializable {private static final long Serialversionuid = 1l;private long id;private String Name;public Long getId () {return ID;} public void SetId (Long id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;}}
OneIn the case of people and identity cards, one person corresponds to an identity card number.
Use <many-to-one> for one-on-oneKey code and configuration:
People:
public class Person implements Serializable {private static final long Serialversionuid = 1l;private long id;private strin G name;private Idcard idcard;public Long getId () {return ID;} Public String GetName () {return name;} public void SetId (Long id) {this.id = ID;} public void SetName (String name) {this.name = name;} Public Idcard Getidcard () {return idcard;} public void Setidcard (Idcard idcard) {this.idcard = Idcard;}}
ID Number:
public class Idcard implements Serializable {private static final long Serialversionuid = 1l;private long id;private strin G Name;public Long GetId () {return ID;} Public String GetName () {return name;} public void SetId (Long id) {this.id = ID;} public void SetName (String name) {this.name = name;}}
Use <one-to-one> for one-on-oneThe code can refer to the previous section, where only the key configurations are listed:
People:
ID Number:
One-to-manyTake classes and students as an example, a class has multiple students, that is, the class is one end, the student is more than the end.
Key code and configuration:
Class:
public class Classes implements Serializable {private static final long Serialversionuid = 1l;private long id;private stri ng name;private set<student> students;public Long getId () {return ID;} Public String GetName () {return name;} public void SetId (Long id) {this.id = ID;} public void SetName (String name) {this.name = name;} Public set<student> getstudents () {return students;} public void Setstudents (set<student> students) {this.students = students;}}
Note: The above configuration adds the inverse= "true" property to avoid saving classes when generating an UPDATE statement to maintain the mapping relationship (update t_student). After you set this property, you should be aware of the order when you save the data, or the foreign key may be set to a null value.
Students:
public class Student implements Serializable {private static final long Serialversionuid = 1l;private long id;private stri ng Name;public Long getId () {return ID;} Public String GetName () {return name;} public void SetId (Long id) {this.id = ID;} public void SetName (String name) {this.name = name;}}
Hibernate Correlation Relationship Mapping