码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate之关于多对多双向关联映射

时间:2014-10-28 17:52:06      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:many-to-many双向关联映射   多对多双向关联映射   


【Hibernate】之关于多对多双向关联映射


多对多的双向关联映射在项目实战中还是相当重要的,所以这里着重写一下!以学生表(Student)和老师表(Teacher)为例。


首先我们还是先看Annotations配置!


@Entity
@Table(name="t_student")
public class Student {
    private Integer id;
    private String name;
    private Integer age;
    private Set<Teacher> teachers=new HashSet<Teacher>();//指定从学生也可以映射老师
     
    @ManyToMany(mappedBy="students")
    public Set<Teacher>getTeachers() {
       returnteachers;
    }
    publicvoidsetTeachers(Set<Teacher> teachers) {
       this.teachers = teachers;
    }
    @Id
    @GeneratedValue
    public Integer getId() {
       returnid;
    }
    publicvoid setId(Integerid) {
       this.id = id;
    }
    @Column(name="s_name")
    public String getName() {
       returnname;
    }
    publicvoid setName(Stringname) {
       this.name = name;
    }
    @Column(name="s_age")
    public Integer getAge() {
       returnage;
    }
    publicvoid setAge(Integerage) {
       this.age = age;
    }
}

@Entity
@Table(name="t_teacher")
public class Teacher {
    private Integer id;
    private String name;
    private Set<Student> students=new HashSet<Student>();//set不允许重复,最适合数据库模型
    @Id
    @GeneratedValue
    public Integer getId() {
       returnid;
    }
    publicvoid setId(Integerid) {
       this.id = id;
    }
    @Column(name="t_name")
    public String getName() {
       returnname;
    }
    publicvoid setName(Stringname) {
       this.name = name;
    }
    @ManyToMany
    @JoinTable(name="t_s_two",//自定义表名
           joinColumns={@JoinColumn(name="teacher_id")},//自定义列名
           inverseJoinColumns={@JoinColumn(name="student_id")})//反转,和Teacher对应的那个表的ID,也是自定义
    public Set<Student>getStudents() {
       returnstudents;
    }
    publicvoidsetStudents(Set<Student> students) {
       this.students = students;
    }
}

JunitTest单元测试


@Test
    publicvoid add(){
       try {
           Configuration cfg=new Configuration();
           cfg.configure();
           SessionFactory    sessionFactory=cfg.buildSessionFactory();
           Sessionsession=sessionFactory.openSession();
           session.beginTransaction();
           Student s=new Student();
           s.setAge(12);
           s.setName("张三");
           session.save(s);
           Student s2=new Student();
           s2.setAge(13);
           s2.setName("李四");
           session.save(s2);
           Teacher t=new Teacher();
           t.setName("张老师");
           Teacher t2=new Teacher();
           t2.setName("李老师");
           Set<Student>students=newHashSet<Student>();
           students.add(s);
           students.add(s2);
           t.setStudents(students);
//         Set<Teacher>teachers=new HashSet<Teacher>();
//         teachers.add(t);
//         teachers.add(t2);
//         s.setTeacher(teachers);
//         s2.setTeacher(teachers);
           session.save(t);
           session.save(t2);
           session.getTransaction().commit();
           sessionFactory.close();
       } catch (HibernateException e){
           e.printStackTrace();
       }
    }

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="csg.hibernate.entity">
    <class name="Student" table="t_student">
       <id name="id">
           <column name="id"/>
           <generator class="native" />
       </id>
       <property name="name" />
       <property name="age" />
       <set name="teachers" table="t_s_two">
           <key column="student_id"/>
           <many-to-many class="csg.hibernate.entity.Teacher" column="Teacher_id"/>
       </set>
    </class>
</hibernate-mapping>

<?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="csg.hibernate.entity">
    <class name="Teacher" table="t_teacher">
       <id name="id">
           <column name="id"/>
           <generator class="native" />
       </id>
       <property name="name" />
       <set name="students" table="t_s_two">
           <key column="teacher_id"/>
           <many-to-many class="csg.hibernate.entity.Student" column="student_id"/>
       </set>
    </class>
</hibernate-mapping>

Ok,大家看到的其实就是将XML中的Set里面的值进行拷贝,修改, 需要注意的是

tablecloum都需要一致!这样建立的中间表才正规!




Hibernate之关于多对多双向关联映射

标签:many-to-many双向关联映射   多对多双向关联映射   

原文地址:http://blog.csdn.net/u010506940/article/details/40542883

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!