• Hibernate多对多关系映射学习笔记


    Student.java

     1 package cn.hai.domain;
    2
    3 import java.util.Set;
    4
    5 public class Student {
    6 private int id;
    7 private String name;
    8 private Set<Teacher> teachers;//多对多关系一般有一个中间表用于相关联,Hibernate面向对象,更注重于对象关系
    9
    10 public int getId() {
    11 return id;
    12 }
    13
    14 public void setId(int id) {
    15 this.id = id;
    16 }
    17
    18 public String getName() {
    19 return name;
    20 }
    21
    22 public void setName(String name) {
    23 this.name = name;
    24 }
    25
    26 public Set<Teacher> getTeachers() {
    27 return teachers;
    28 }
    29
    30 public void setTeachers(Set<Teacher> teachers) {
    31 this.teachers = teachers;
    32 }
    33
    34 }


    Teacher.jaca

     1 package cn.hai.domain;
    2
    3 import java.util.Set;
    4
    5 public class Teacher {
    6 private int id;
    7 private String name;
    8 private Set<Student> students;
    9
    10 public int getId() {
    11 return id;
    12 }
    13
    14 public void setId(int id) {
    15 this.id = id;
    16 }
    17
    18 public String getName() {
    19 return name;
    20 }
    21
    22 public void setName(String name) {
    23 this.name = name;
    24 }
    25
    26 public Set<Student> getStudents() {
    27 return students;
    28 }
    29
    30 public void setStudents(Set<Student> students) {
    31 this.students = students;
    32 }
    33
    34 }

    Student.hbm.xml

     1 <?xml version="1.0"?>
    2 <!DOCTYPE hibernate-mapping PUBLIC
    3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    5 <hibernate-mapping package="cn.hai.domain">
    6 <class name="Student">
    7 <id name="id">
    8 <generator class="native" /> <!--主键,自增长-->
    9 </id>
    10 <property name="name" />
    11 <!-- 声明中间表teacher_student,声明Teacher表主键teacher_id,声明多对多对应的表Student,声明多对多对应表的主键student_id -->
    12 <set name="teachers" table="teacher_student">
    13 <key column="student_id" />
    14 <many-to-many class="Teacher" column="Teacher_id" />
    15 </set>
    16 </class>
    17
    18 </hibernate-mapping>

    Teacher.hbm.xml

     1 <?xml version="1.0"?>
    2 <!DOCTYPE hibernate-mapping PUBLIC
    3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    5 <hibernate-mapping package="cn.hai.domain">
    6 <class name="Teacher">
    7 <id name="id">
    8 <generator class="native" />
    9 </id>
    10 <property name="name" />
    11 <set name="students" table="teacher_student">
    12 <key column="teacher_id" />
    13 <many-to-many class="Student" column="student_id" />
    14 </set>
    15 </class>
    16
    17 </hibernate-mapping>

    ManyToManyTest.java

     1 package cn.hai;
    2
    3 import java.util.HashSet;
    4 import java.util.Set;
    5
    6 import org.hibernate.Session;
    7 import org.hibernate.Transaction;
    8
    9 import cn.hai.dao.HibernateUtil;
    10 import cn.hai.domain.Student;
    11 import cn.hai.domain.Teacher;
    12
    13 public class ManyToMany {
    14
    15 /**
    16 * @param args
    17 */
    18 public static void main(String[] args) {
    19 Session s = null;
    20 Transaction tx = null;
    21 try {
    22 // 建立关系
    23 Set<Teacher> ts = new HashSet<Teacher>();
    24 Teacher t1 = new Teacher();
    25 t1.setName("XiangfGang1");
    26 ts.add(t1);
    27
    28 Teacher t2 = new Teacher();
    29 t2.setName("ZhengHui1");
    30 ts.add(t2);
    31
    32 Teacher t3 = new Teacher();
    33 t3.setName("WangJinFeng1");
    34 ts.add(t3);
    35
    36 Set<Student> ss = new HashSet<Student>();
    37 Student sd1 = new Student();
    38 sd1.setName("ChengHai1");
    39 ss.add(sd1);
    40
    41 Student sd2 = new Student();
    42 sd2.setName("JiangKui1");
    43 ss.add(sd2);
    44
    45 Student sd3 = new Student();
    46 sd3.setName("YanJiaYang1");
    47 ss.add(sd3);
    48
    49 t1.setStudents(ss);
    50 t2.setStudents(ss);
    51 t3.setStudents(ss);
    52 // 告诉学生有哪些老师,与上面告诉老师有哪些学生任选一种即可
    53 // sd1.setTeachers(ts);
    54 // sd2.setTeachers(ts);
    55 // sd3.setTeachers(ts);
    56
    57 s = HibernateUtil.getSession();
    58 tx = s.beginTransaction();
    59 s.save(t1);
    60 s.save(t2);
    61 s.save(t3);
    62 s.save(sd1);
    63 s.save(sd2);
    64 s.save(sd3);
    65 tx.commit();
    66 } finally {
    67 if (s != null) {
    68 s.close();
    69 }
    70 }
    71
    72 }
    //根据Teacher,id查学生的数量,左右连接查询导致效率低,少用
    static void query(int id){
    Session s=null;
    Transaction tx=null;
    try{
    s=HibernateUtil.getSession();
    tx=s.beginTransaction();
    Teacher t=(Teacher)s.get(Teacher.class, id);
    System.out.println("students:"+t.getStudents().size());
    //Hibernate.initialize(t.getStudents());
    tx.commit();
    }finally{
    if(s!=null){
    s.close();
    }
    }
    }
     




    mysql


    作者: 人名树影/renmingshuying
  • 相关阅读:
    11月20日
    11月19日
    11月26日
    11月25日
    生活有感(一)
    c# word 删除指定内容
    mysql insert语句
    c# 删除word文档中某一页
    mysql 相同表结构拷贝数据
    调试再次出错
  • 原文地址:https://www.cnblogs.com/cqchai/p/2277572.html
Copyright © 2020-2023  润新知