Hibernate Learning Notes 9: Many-to-One and One-to-Many Relationships

Keywords: Hibernate xml Database

Hibernate Learning Notes 9: Many-to-One and One-to-Many Relationships

This blog post will introduce many-to-one and one-to-many relationships.

Many-to-one and one-to-many relationships are also common in our lives. For example, in our student days, a class can have more than one student, while a student can only belong to one class. This is an example of many-to-one (one-to-many).

Also in our work, a working group can have more than one user, and a user can only belong to one group, which is also an example of many-to-one (one-to-many) relationship.

1. Many-to-One One One-Way Association

Here's an example of how to build a many-to-one relationship with a working group that can have multiple users and a user can only belong to one group.

At the class level: add another reference to one side of the hierarchy

On the database level: set foreign keys on the "many-to-one" side of the "many-to-one" side

1.1. Annotation Implementation of Multi-to-One One-Way Association Relations

Class Group

@Entity
    @Table(name="t_group")  //Specified table name
    public class Group {
    private int idGroup;
    private String nameGroup;
    @Id
    @GeneratedValue
    public int getIdGroup() {
        return idGroup;
    }
    public void setIdGroup(int idGroup) {
        this.idGroup = idGroup;
    }
    public String getNameGroup() {
        return nameGroup;
    }
    public void setNameGroup(String nameGroup) {
        this.nameGroup = nameGroup;
    }

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

Class User

@Entity
    @Table(name="t_user")  //Specified table name
    public class User {
    private int idUser;
    private String nameUser;
    private Group group;//Designated foreign key
    @ManyToOne  //Many-to-one is that a group has more than one user, while a user can only belong to one group.
    @JoinColumn(name="group_id")
    public Group getGroup() {
        return group;
    }
    public void setGroup(Group group) {
        this.group = group;
    }
    @Id
    @GeneratedValue
    public int getIdUser() {
        return idUser;
    }
    public void setIdUser(int idUser) {
        this.idUser = idUser;
    }
    public String getNameUser() {
        return nameUser;
    }
    public void setNameUser(String nameUser) {
        this.nameUser = nameUser;
    }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

Because User entity and Group entity, User is the "many-to-many" party in "many-to-one", so we can establish many-to-one association by setting a foreign key of Group in User class.

The test results are as follows:

1.2. XXX.hbm.xml Implementation of Multi-to-One One-Way Association

Group and User classes

In the Group and User classes, the corresponding attributes and get and set methods can be used.
And there is a Group reference in the User class.

Group.hbm.xml

<?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping package="com.hibernate.model">
    <class name="Group" table="t_group" >
        <id name="idGroup" column="idGroup">
            <generator class="native"/>
        </id>
        <property name="nameGroup" column="nameGroup"/>   
    </class>
    </hibernate-mapping>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

User.hbm.xml

<?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping package="com.hibernate.model">
    <class name="User" table="t_user" >
        <id name="idUser" column="id">
            <generator class="native"/>
        </id>
        <property name="nameUser" column="nameUser"/>  
        <!--  This constitutes a many-to-one relationship. -->
        <many-to-one name="group" column="groupId"></many-to-one> 
    </class>
    </hibernate-mapping>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

By adding in the User.hbm.xml file

 <many-to-one name="group" column="groupId"></many-to-one> 
  • 1

It constructs many-to-one relationship.

2. One-to-many one-way correlation

On the level of class entities: there are multiple sets on one side

At the database level: adding foreign keys to multiple parties

Here's an example of how to build a one-to-many relationship with a working group that can have more than one user, and a user can only belong to one group.

2.1. Annotation Implementation of One-to-Many One-Way Association

The first thing to look for is the User class

@Entity
    @Table(name="t_user")  //Specified table name
    public class User {
    private int idUser;
    private String nameUser;
    @Id
    @GeneratedValue
    public int getIdUser() {
        return idUser;
    }
    public void setIdUser(int idUser) {
        this.idUser = idUser;
    }
    public String getNameUser() {
        return nameUser;
    }
    public void setNameUser(String nameUser) {
        this.nameUser = nameUser;
    }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

Here's another look: Group classes

Because of the one-to-many relationship, there must be a container on one side. Set container used here can be used to avoid duplication. Primary key duplication is not allowed in the database, which is consistent with the logic of our database.

Then the @OneToMany annotation is used to modify the get method of the Set container. The name of the foreign key is specified by @JoinColumn(name="groupId").

@Entity
    @Table(name="t_group")  //Specified table name
    public class Group {
    private int idGroup;
    private String nameGroup;
    private Set<User> set=new HashSet<User>();
    @OneToMany   //One-to-many relationship assignment
    @JoinColumn(name="groupId")
    public Set<User> getSet() {
        return set;
    }
    public void setSet(Set<User> set) {
        this.set = set;
    }
    @Id
    @GeneratedValue
    public int getIdGroup() {
        return idGroup;
    }
    public void setIdGroup(int idGroup) {
        this.idGroup = idGroup;
    }
    public String getNameGroup() {
        return nameGroup;
    }
    public void setNameGroup(String nameGroup) {
        this.nameGroup = nameGroup;
    }


    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

The test results are as follows:

If we do not specify the foreign key name by @JoinColumn(name="groupId"), Hibernate will generate an intermediate table for us, which treats one-to-many as many-to-many.

The test results are as follows:

2.2. XXX.hbm.xml Implementation of One-to-Many One-Way Association

Class Group

public class Group {
    private int idGroup;
    private String nameGroup;
    private Set<User> set=new HashSet<User>();
    public Set<User> getSet() {
        return set;
    }
    public void setSet(Set<User> set) {
        this.set = set;
    }
    public int getIdGroup() {
        return idGroup;
    }
    public void setIdGroup(int idGroup) {
        this.idGroup = idGroup;
    }
    public String getNameGroup() {
        return nameGroup;
    }
    public void setNameGroup(String nameGroup) {
        this.nameGroup = nameGroup;
    }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

In the Group.bhm.xml file:
For Set attributes in the Group class, adding the following code can form a one-to-many Association relationship:

<set name="set" >
            <key column="groupId"></key>
            <one-to-many class="com.hibernate.model.User"/>
 </set> 
  • 1
  • 2
  • 3
  • 4

The complete contents are as follows:

<?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping package="com.hibernate.model">
    <class name="Group" table="t_group" >
        <id name="idGroup" column="idGroup">
            <generator class="native"/>
        </id>
        <property name="nameGroup" column="nameGroup"/>  
        <set name="set" >
            <key column="groupId"></key>
            <one-to-many class="com.hibernate.model.User"/>
        </set> 
    </class>
    </hibernate-mapping>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

Class User

public class User {
    private int idUser;
    private String nameUser;
    public int getIdUser() {
        return idUser;
    }
    public void setIdUser(int idUser) {
        this.idUser = idUser;
    }
    public String getNameUser() {
        return nameUser;
    }
    public void setNameUser(String nameUser) {
        this.nameUser = nameUser;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

User.hbm.xml file

<?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping package="com.hibernate.model">
    <class name="User" table="t_user" >
        <id name="idUser" column="id">
            <generator class="native"/>
        </id>
        <property name="nameUser" column="nameUser"/>  
    </class>
    </hibernate-mapping>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

One-to-many (many-to-one) bidirectional association

After we understand one-to-many and many-to-one one one-way association, two-way association is quite simple, that is to merge the two one-way association. Next, I'll just show you how to use Annotation to build two-way associations.

On one side: Group entity classes

Similar to building one-to-many.

Just one thing to note: specify mappedBy

The code is as follows:

@Entity
    @Table(name="t_group")  //Specified table name
    public class Group {
    private int idGroup;
    private String nameGroup;
    private Set<User> set=new HashSet<User>();
    @OneToMany(mappedBy="group")   //The one-to-many relationship is specified and because it is bidirectional, you must specify mappedBy, otherwise two foreign keys will be generated.
    public Set<User> getSet() {
        return set;
    }
    public void setSet(Set<User> set) {
        this.set = set;
    }
    @Id
    @GeneratedValue
    public int getIdGroup() {
        return idGroup;
    }
    public void setIdGroup(int idGroup) {
        this.idGroup = idGroup;
    }
    public String getNameGroup() {
        return nameGroup;
    }
    public void setNameGroup(String nameGroup) {
        this.nameGroup = nameGroup;
    }

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

User classes on multiple sides

Similar to building many-to-one associations.

@Entity
    @Table(name="t_user")  //Specified table name
    public class User {
    private int idUser;
    private String nameUser;
    private Group group;
    @ManyToOne
    @JoinColumn(name="groupId")
    public Group getGroup() {
        return group;
    }
    public void setGroup(Group group) {
        this.group = group;
    }
    @Id
    @GeneratedValue
    public int getIdUser() {
        return idUser;
    }
    public void setIdUser(int idUser) {
        this.idUser = idUser;
    }
    public String getNameUser() {
        return nameUser;
    }
    public void setNameUser(String nameUser) {
        this.nameUser = nameUser;
    }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

That's Ok.

Summary

The implementation of Annotation and XXX.hbm.xml files of one-to-one and one-to-many one-way correlation is relatively simple.


from: http://blog.csdn.net/u010412719/article/details/51335064

Posted by stylefrog on Tue, 21 May 2019 14:39:24 -0700