hibernate中基于annotation(注解)的many2many双向

2019-02-28 23:40|来源: 领悟书生

修改hibernate中多对多映射关系中的例子,让它基于annotation(注解)的方式。


简单的多对多映射关系

Admin.java

package org.zttc.itat.model;
 
import java.util.Set;
 
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
 
@Entity
@Table(name="t_admin")
public class Admin {
    private int id;
    private String name;
    private Set<Role> roles;
     
    @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;
    }
    @ManyToMany(mappedBy="admins")
    public Set<Role> getRoles() {
        return roles;
    }
    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }
}

Role.java

package org.zttc.itat.model;
 
import java.util.HashSet;
import java.util.Set;
 
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
 
@Entity
@Table(name="t_role")
public class Role {
    private int id;
    private String name;
    private Set<Admin> admins;
     
    public Role() {
        admins = new HashSet<Admin>();
    }
    public void add(Admin admin) {
        admins.add(admin);
    }
     
    @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;
    }
     
    @ManyToMany
    @JoinTable(name="t_role_admin",joinColumns={@JoinColumn(name="rid")},
            inverseJoinColumns={@JoinColumn(name="aid")})
    public Set<Admin> getAdmins() {
        return admins;
    }
    public void setAdmins(Set<Admin> admins) {
        this.admins = admins;
    }
}


把多对多分为两个一对多的关系进行映射

Teacher.java

package org.zttc.itat.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;
 
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
 
 
@Entity
@Table(name="t_teacher")
public class Teacher {
    private int id;
    private String name;
    private Set<TeacherCourse> tcs;
     
    @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="teacher")
    @LazyCollection(LazyCollectionOption.EXTRA)
    public Set<TeacherCourse> getTcs() {
        return tcs;
    }
    public void setTcs(Set<TeacherCourse> tcs) {
        this.tcs = tcs;
    }
}

Course.java

package org.zttc.itat.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;
 
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
 
@Entity
@Table(name="t_course")
public class Course {
    private int id;
    private String name;
    private Set<TeacherCourse> tcs;
     
    @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="course")
    @LazyCollection(LazyCollectionOption.EXTRA)
    public Set<TeacherCourse> getTcs() {
        return tcs;
    }
    public void setTcs(Set<TeacherCourse> tcs) {
        this.tcs = tcs;
    }
     
}

TeacherCourse.java

package org.zttc.itat.model;
 
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
 
@Entity
@Table(name="t_teacher_course")
public class TeacherCourse {
    private int id;
    private double ach;
    private Teacher teacher;
    private Course course;
     
    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public double getAch() {
        return ach;
    }
    public void setAch(double ach) {
        this.ach = ach;
    }
    @ManyToOne
    @JoinColumn(name="tid")
    public Teacher getTeacher() {
        return teacher;
    }
    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }
     
    @ManyToOne
    @JoinColumn(name="cid")
    public Course getCourse() {
        return course;
    }
    public void setCourse(Course course) {
        this.course = course;
    }
}

测试代码请参考hibernate中多对多映射关系


本文链接:hibernate中基于annotation(注解)的many2many双向,领悟书生原创(笔记),转载请注明出处http://www.656463.com/article/406

相关问答

更多