Hibernate Correlation Relationship Mapping

Source: Internet
Author: User

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;}}
One

In the case of people and identity cards, one person corresponds to an identity card number.

Use <many-to-one> for one-on-one

Key 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-one

The code can refer to the previous section, where only the key configurations are listed:

People:

ID Number:

One-to-many

Take 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.