Hibernate (8) one-to-many Association and hibernate Association

Source: Internet
Author: User

Hibernate (8) one-to-many Association and hibernate Association

1. One-to-multiple one-way Guan Liang

One-to-multiple unidirectional Association is similar to multiple-to-one association.

Assume that a Group has multiple users, that is, one (Group) to multiple (User)

You need to add a Set for the User class to the Group class (users in the database cannot be duplicate, so Set must be used) attribute: private Set <User> users; then in getUser () add Annotation on the method:
@ Onetoworkflow and @ JoinColumn (name = "groupId"). If @ JoinColumn is not added, an intermediate table is automatically generated.

Group class:

package cn.orlion.hibernate.model;import java.util.Set;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.OneToMany;import javax.persistence.Table;@Entity@Table(name="t_group")public class Group {    private int id;        private String name;        private Set<User> users;    @Id    @GeneratedValue    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    @OneToMany    @JoinColumn(name="groupId")    public Set<User> getUsers() {        return users;    }    public void setUsers(Set<User> users) {        this.users = users;    }}

User class:

package cn.orlion.hibernate.model;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.Table;@Entity@Table(name="t_user")public class User {    private int id;        private String name;    @Id    @GeneratedValue    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }}

Generated SQL:

create table t_group (        id integer not null auto_increment,        name varchar(255),        primary key (id)    )create table t_user (        id integer not null auto_increment,        name varchar(255),        groupId integer,        primary key (id)    )alter table t_user         add constraint FK_7ktm6l2qkykpqrf6oq01ys8wy         foreign key (groupId)         references t_group (id)

 

2. One-to-multiple bidirectional Association

Configuration method: Add annotation @ onetoworkflow (mappedBy = "Group") to the getUsers () method in the group class. If mappedBy is not written, two groupid fields will be generated in the user table.

Add the group attribute to the User class and add the annotation @ ManyToOne on the getGroup () method.

Group class:

 

package cn.orlion.hibernate.model;import java.util.Set;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.OneToMany;import javax.persistence.Table;@Entity@Table(name="t_group")public class Group {    private int id;        private String name;        private Set<User> users;    @Id    @GeneratedValue    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    @OneToMany(mappedBy="group")    public Set<User> getUsers() {        return users;    }    public void setUsers(Set<User> users) {        this.users = users;    }}

 

User class:

package cn.orlion.hibernate.model;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.ManyToOne;import javax.persistence.Table;@Entity@Table(name="t_user")public class User {    private int id;        private String name;        private Group group;    @Id    @GeneratedValue    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    @ManyToOne    public Group getGroup() {        return group;    }    public void setGroup(Group group) {        this.group = group;    }}

Generated SQL

create table t_group (        id integer not null auto_increment,        name varchar(255),        primary key (id)    )create table t_user (        id integer not null auto_increment,        name varchar(255),        group_id integer,        primary key (id)    )alter table t_user         add constraint FK_e5f24mh6aryt9hsy99oydps6g         foreign key (group_id)         references t_group (id)

 

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.