hibernate(八)一对多关联

一、一对多单向关良

一对多单向关联与多对一相似

假设一个组有多个用户,即一(Group)对多(User)

需要在Group类中添加一个User类的Set集合(数据库中的用户不可能是重复的,所以要用Set)属性:private Set<User> users;然后在getUser()方法上添加注解:
@OneToMany和@JoinColumn(name="groupId"),如果不加@JoinColumn则会自动生成一个中间表

Group类:

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类:

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

生成的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)

 

二、一对多双向关联

配置方式:在Group类中的getUsers()方法上添加注解@OneToMany(mappedBy="group"),如果不写mappedBy则会在user表中生成两个groupid字段

;然后在User类中添加group属性,在getGroup()方法上添加注解@ManyToOne

Group类:

 

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类:

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

生成的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)

 

posted @ 2015-12-28 20:12  orlion  阅读(335)  评论(0编辑  收藏  举报