• (八)Hibernate 映射关系


    所有项目导入对应的hibernate的jar包、mysql的jar包和添加每次都需要用到的HibernateUtil.java

    第一节:Hibernate 一对一映射关系实现

    1,按照主键映射;

    2,按照外键映射;

    1,按照主键映射:

    User.java

     1 package com.wishwzp.model;
     2 
     3 public class User {
     4 
     5     private int id;
     6     private String name;
     7     private Address address;
     8     
     9     public int getId() {
    10         return id;
    11     }
    12     public void setId(int id) {
    13         this.id = id;
    14     }
    15     public String getName() {
    16         return name;
    17     }
    18     public void setName(String name) {
    19         this.name = name;
    20     }
    21     public Address getAddress() {
    22         return address;
    23     }
    24     public void setAddress(Address address) {
    25         this.address = address;
    26     }
    27     
    28     
    29 }

    Address.java

     1 package com.wishwzp.model;
     2 
     3 public class Address {
     4 
     5     private int id;
     6     private String address;
     7     private String zipcode;
     8     private User user;
     9     
    10     public int getId() {
    11         return id;
    12     }
    13     public void setId(int id) {
    14         this.id = id;
    15     }
    16     public String getAddress() {
    17         return address;
    18     }
    19     public void setAddress(String address) {
    20         this.address = address;
    21     }
    22     public String getZipcode() {
    23         return zipcode;
    24     }
    25     public void setZipcode(String zipcode) {
    26         this.zipcode = zipcode;
    27     }
    28     public User getUser() {
    29         return user;
    30     }
    31     public void setUser(User user) {
    32         this.user = user;
    33     }
    34     
    35     
    36 }

    User.hbm.xml

     1 <?xml version="1.0"?>
     2 <!DOCTYPE hibernate-mapping PUBLIC
     3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
     4         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
     5 
     6 <hibernate-mapping package="com.wishwzp.model">
     7 
     8     <class name="User" table="t_user">
     9         <id name="id" column="userId">
    10             <generator class="native"></generator>
    11         </id>
    12         
    13         <property name="name" column="userName"></property>
    14         
    15         <one-to-one name="address" class="com.wishwzp.model.Address" cascade="all"></one-to-one>
    16     </class>
    17 
    18 </hibernate-mapping>

    Address.hbm.xml

     1 <?xml version="1.0"?>
     2 <!DOCTYPE hibernate-mapping PUBLIC
     3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
     4         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
     5 
     6 <hibernate-mapping package="com.wishwzp.model">
     7 
     8     <class name="Address" table="t_address">
     9         <id name="id" column="addressId">
    10             <generator class="foreign">
    11                 <param name="property">user</param>
    12             </generator>
    13         </id>
    14         
    15         <property name="address" column="address"></property>
    16         <property name="zipcode" column="zipcode"></property>
    17         
    18         <one-to-one name="user" class="com.wishwzp.model.User" constrained="true"></one-to-one>
    19     </class>
    20 
    21 </hibernate-mapping>

    hibernate.cfg.xml

     1 <?xml version='1.0' encoding='utf-8'?>
     2 <!DOCTYPE hibernate-configuration PUBLIC
     3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
     4         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
     5 
     6 <hibernate-configuration>
     7 
     8     <session-factory>
     9 
    10         <!--数据库连接设置 -->
    11         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    12         <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
    13         <property name="connection.username">root</property>
    14         <property name="connection.password">123456</property>
    15 
    16        
    17         <!-- 方言 -->
    18         <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
    19     
    20         <!-- 控制台显示SQL -->
    21         <property name="show_sql">true</property>
    22 
    23         <!-- 自动更新表结构 -->
    24         <property name="hbm2ddl.auto">update</property>
    25         
    26           <mapping resource="com/wishwzp/model/User.hbm.xml"/>
    27           <mapping resource="com/wishwzp/model/Address.hbm.xml"/>
    28 
    29     </session-factory>
    30 
    31 </hibernate-configuration>

    UserTest.java

     1 package com.wishwzp.service;
     2 
     3 import org.hibernate.Session;
     4 import org.hibernate.SessionFactory;
     5 import org.junit.After;
     6 import org.junit.Before;
     7 import org.junit.Test;
     8 
     9 import com.wishwzp.model.Address;
    10 import com.wishwzp.model.Address2;
    11 import com.wishwzp.model.User;
    12 import com.wishwzp.model.User2;
    13 import com.wishwzp.util.HibernateUtil;
    14 
    15 public class UserTest {
    16 
    17     private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
    18     private Session session;
    19     
    20     @Before
    21     public void setUp() throws Exception {
    22         session=sessionFactory.openSession(); // 生成一个session
    23         session.beginTransaction(); // 开启事务
    24     }
    25 
    26     @After
    27     public void tearDown() throws Exception {
    28          session.getTransaction().commit(); // 提交事务
    29          session.close(); // 关闭session
    30     }
    31 
    32     @Test
    33     public void testSave1(){
    34         
    35     }
    36     
    37 }

    UserTest.java

     1 package com.wishwzp.service;
     2 
     3 import org.hibernate.Session;
     4 import org.hibernate.SessionFactory;
     5 import org.junit.After;
     6 import org.junit.Before;
     7 import org.junit.Test;
     8 
     9 import com.wishwzp.model.Address;
    10 import com.wishwzp.model.Address2;
    11 import com.wishwzp.model.User;
    12 import com.wishwzp.model.User2;
    13 import com.wishwzp.util.HibernateUtil;
    14 
    15 public class UserTest {
    16 
    17     private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
    18     private Session session;
    19     
    20     @Before
    21     public void setUp() throws Exception {
    22         session=sessionFactory.openSession(); // 生成一个session
    23         session.beginTransaction(); // 开启事务
    24     }
    25 
    26     @After
    27     public void tearDown() throws Exception {
    28          session.getTransaction().commit(); // 提交事务
    29          session.close(); // 关闭session
    30     }
    31 
    32     @Test
    33     public void testSave1(){
    34         User user=new User();
    35         user.setName("张三");
    36         
    37         Address address=new Address();
    38         address.setAddress("某地方");
    39         address.setZipcode("43242");
    40         address.setUser(user);
    41         
    42         user.setAddress(address);
    43         session.save(user);
    44     }
    45     
    46 }

    我们发现主键一对一的。。。。

    2,按照外键映射:

    User2.java

     1 package com.wishwzp.model;
     2 
     3 public class User2 {
     4 
     5     private int id;
     6     private String name;
     7     private Address2 address;
     8     
     9     public int getId() {
    10         return id;
    11     }
    12     public void setId(int id) {
    13         this.id = id;
    14     }
    15     public String getName() {
    16         return name;
    17     }
    18     public void setName(String name) {
    19         this.name = name;
    20     }
    21     public Address2 getAddress() {
    22         return address;
    23     }
    24     public void setAddress(Address2 address) {
    25         this.address = address;
    26     }
    27     
    28     
    29 }

    Address2.java

     1 package com.wishwzp.model;
     2 
     3 public class Address2 {
     4 
     5     private int id;
     6     private String address;
     7     private String zipcode;
     8     private User2 user;
     9     
    10     public int getId() {
    11         return id;
    12     }
    13     public void setId(int id) {
    14         this.id = id;
    15     }
    16     public String getAddress() {
    17         return address;
    18     }
    19     public void setAddress(String address) {
    20         this.address = address;
    21     }
    22     public String getZipcode() {
    23         return zipcode;
    24     }
    25     public void setZipcode(String zipcode) {
    26         this.zipcode = zipcode;
    27     }
    28     public User2 getUser() {
    29         return user;
    30     }
    31     public void setUser(User2 user) {
    32         this.user = user;
    33     }
    34     
    35     
    36     
    37 }

    User2.hbm.xml

     1 <?xml version="1.0"?>
     2 <!DOCTYPE hibernate-mapping PUBLIC
     3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
     4         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
     5 
     6 <hibernate-mapping package="com.wishwzp.model">
     7 
     8     <class name="User2" table="t_user2">
     9         <id name="id" column="userId">
    10             <generator class="native"></generator>
    11         </id>
    12         
    13         <property name="name" column="userName"></property>
    14         
    15         <many-to-one name="address" class="com.wishwzp.model.Address2" column="addressId" cascade="all" unique="true"></many-to-one>
    16     </class>
    17 
    18 </hibernate-mapping>

    Address2.hbm.xml

     1 <?xml version="1.0"?>
     2 <!DOCTYPE hibernate-mapping PUBLIC
     3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
     4         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
     5 
     6 <hibernate-mapping package="com.wishwzp.model">
     7 
     8     <class name="Address2" table="t_address2">
     9         <id name="id" column="addressId">
    10             <generator class="native">
    11             </generator>
    12         </id>
    13         
    14         <property name="address" column="address"></property>
    15         <property name="zipcode" column="zipcode"></property>
    16         
    17         <one-to-one name="user" class="com.wishwzp.model.User2" property-ref="address"></one-to-one>
    18     </class>
    19 
    20 </hibernate-mapping>

    hibernate.cfg.xml

     1 <?xml version='1.0' encoding='utf-8'?>
     2 <!DOCTYPE hibernate-configuration PUBLIC
     3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
     4         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
     5 
     6 <hibernate-configuration>
     7 
     8     <session-factory>
     9 
    10         <!--数据库连接设置 -->
    11         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    12         <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
    13         <property name="connection.username">root</property>
    14         <property name="connection.password">123456</property>
    15 
    16        
    17         <!-- 方言 -->
    18         <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
    19     
    20         <!-- 控制台显示SQL -->
    21         <property name="show_sql">true</property>
    22 
    23         <!-- 自动更新表结构 -->
    24         <property name="hbm2ddl.auto">update</property>
    25 
    26           
    27           <mapping resource="com/wishwzp/model/User2.hbm.xml"/>
    28           <mapping resource="com/wishwzp/model/Address2.hbm.xml"/>
    29 
    30     </session-factory>
    31 
    32 </hibernate-configuration>

    UserTest.java

     1 package com.wishwzp.service;
     2 
     3 import org.hibernate.Session;
     4 import org.hibernate.SessionFactory;
     5 import org.junit.After;
     6 import org.junit.Before;
     7 import org.junit.Test;
     8 
     9 import com.wishwzp.model.Address;
    10 import com.wishwzp.model.Address2;
    11 import com.wishwzp.model.User;
    12 import com.wishwzp.model.User2;
    13 import com.wishwzp.util.HibernateUtil;
    14 
    15 public class UserTest {
    16 
    17     private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
    18     private Session session;
    19     
    20     @Before
    21     public void setUp() throws Exception {
    22         session=sessionFactory.openSession(); // 生成一个session
    23         session.beginTransaction(); // 开启事务
    24     }
    25 
    26     @After
    27     public void tearDown() throws Exception {
    28          session.getTransaction().commit(); // 提交事务
    29          session.close(); // 关闭session
    30     }
    31     
    32     @Test
    33     public void testSave2(){
    34         
    35     }
    36     
    37 }

    UserTest.java

     1 package com.wishwzp.service;
     2 
     3 import org.hibernate.Session;
     4 import org.hibernate.SessionFactory;
     5 import org.junit.After;
     6 import org.junit.Before;
     7 import org.junit.Test;
     8 
     9 import com.wishwzp.model.Address;
    10 import com.wishwzp.model.Address2;
    11 import com.wishwzp.model.User;
    12 import com.wishwzp.model.User2;
    13 import com.wishwzp.util.HibernateUtil;
    14 
    15 public class UserTest {
    16 
    17     private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
    18     private Session session;
    19     
    20     @Before
    21     public void setUp() throws Exception {
    22         session=sessionFactory.openSession(); // 生成一个session
    23         session.beginTransaction(); // 开启事务
    24     }
    25 
    26     @After
    27     public void tearDown() throws Exception {
    28          session.getTransaction().commit(); // 提交事务
    29          session.close(); // 关闭session
    30     }
    31     
    32     @Test
    33     public void testSave2(){
    34         User2 user=new User2();
    35         user.setName("李四");
    36         
    37         Address2 address=new Address2();
    38         address.setAddress("某地方2");
    39         address.setZipcode("432422");
    40         address.setUser(user);
    41         
    42         user.setAddress(address);
    43         session.save(user);
    44     }
    45     
    46 }

    我们发现:t_User2表中的addressId对应着t_address2表中的主键addressId

    第二节:Hibernate 多对多映射关系实现

    1,多对多单向实现;

    2,多对多双向实现;

    1,多对多单向实现:

    Course.java

     1 package com.wishwzp.model;
     2 
     3 public class Course {
     4 
     5     private int id;
     6     private String name;
     7     
     8     public int getId() {
     9         return id;
    10     }
    11     public void setId(int id) {
    12         this.id = id;
    13     }
    14     public String getName() {
    15         return name;
    16     }
    17     public void setName(String name) {
    18         this.name = name;
    19     }
    20     
    21     
    22 }

    Student.java

     1 package com.wishwzp.model;
     2 
     3 import java.util.HashSet;
     4 import java.util.Set;
     5 
     6 public class Student {
     7 
     8     private int id;
     9     private String name;
    10     private Set<Course> courses=new HashSet<Course>();
    11     
    12     public int getId() {
    13         return id;
    14     }
    15     public void setId(int id) {
    16         this.id = id;
    17     }
    18     public String getName() {
    19         return name;
    20     }
    21     public void setName(String name) {
    22         this.name = name;
    23     }
    24     public Set<Course> getCourses() {
    25         return courses;
    26     }
    27     public void setCourses(Set<Course> courses) {
    28         this.courses = courses;
    29     }
    30     
    31     
    32 }

    Course.hbm.xml

     1 <?xml version="1.0"?>
     2 <!DOCTYPE hibernate-mapping PUBLIC
     3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
     4         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
     5 
     6 <hibernate-mapping package="com.wishwzp.model">
     7 
     8     <class name="Course" table="t_course">
     9         <id name="id" column="courseId">
    10             <generator class="native"></generator>
    11         </id>
    12         
    13         <property name="name" column="courseName"></property>
    14         
    15     </class>
    16 
    17 </hibernate-mapping>

    Student.hbm.xml

     1 <?xml version="1.0"?>
     2 <!DOCTYPE hibernate-mapping PUBLIC
     3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
     4         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
     5 
     6 <hibernate-mapping package="com.wishwzp.model">
     7 
     8     <class name="Student" table="t_student">
     9         <id name="id" column="studentId">
    10             <generator class="native"></generator>
    11         </id>
    12         
    13         <property name="name" column="studentName"></property>
    14         
    15         <set name="courses" table="student_course" cascade="save-update">
    16             <key column="student_id"></key>
    17             <many-to-many class="com.wishwzp.model.Course" column="course_id"></many-to-many>
    18         </set>
    19     </class>
    20 
    21 </hibernate-mapping>

    hibernate.cfg.xml

     1 <?xml version='1.0' encoding='utf-8'?>
     2 <!DOCTYPE hibernate-configuration PUBLIC
     3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
     4         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
     5 
     6 <hibernate-configuration>
     7 
     8     <session-factory>
     9 
    10         <!--数据库连接设置 -->
    11         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    12         <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
    13         <property name="connection.username">root</property>
    14         <property name="connection.password">123456</property>
    15 
    16        
    17         <!-- 方言 -->
    18         <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
    19     
    20         <!-- 控制台显示SQL -->
    21         <property name="show_sql">true</property>
    22 
    23         <!-- 自动更新表结构 -->
    24         <property name="hbm2ddl.auto">update</property>
    25         
    26           <mapping resource="com/wishwzp/model/Student.hbm.xml"/>
    27           <mapping resource="com/wishwzp/model/Course.hbm.xml"/>
    28 
    29     </session-factory>
    30 
    31 </hibernate-configuration>

    StudentTest.java

     1 package com.wishwzp.service;
     2 
     3 import java.util.Iterator;
     4 import java.util.Set;
     5 
     6 import org.hibernate.Session;
     7 import org.hibernate.SessionFactory;
     8 import org.junit.After;
     9 import org.junit.Before;
    10 import org.junit.Test;
    11 
    12 import com.wishwzp.model.Course;
    13 import com.wishwzp.model.Course2;
    14 import com.wishwzp.model.Student;
    15 import com.wishwzp.model.Student2;
    16 import com.wishwzp.util.HibernateUtil;
    17 
    18 public class StudentTest {
    19 
    20     private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
    21     private Session session;
    22     
    23     @Before
    24     public void setUp() throws Exception {
    25         session=sessionFactory.openSession(); // 生成一个session
    26         session.beginTransaction(); // 开启事务
    27     }
    28 
    29     @After
    30     public void tearDown() throws Exception {
    31          session.getTransaction().commit(); // 提交事务
    32          session.close(); // 关闭session
    33     }
    34 
    35     @Test
    36     public void testSave1(){
    37         
    38         
    39     }
    40     
    41     
    42 }

    StudentTest.java

     1 package com.wishwzp.service;
     2 
     3 import java.util.Iterator;
     4 import java.util.Set;
     5 
     6 import org.hibernate.Session;
     7 import org.hibernate.SessionFactory;
     8 import org.junit.After;
     9 import org.junit.Before;
    10 import org.junit.Test;
    11 
    12 import com.wishwzp.model.Course;
    13 import com.wishwzp.model.Course2;
    14 import com.wishwzp.model.Student;
    15 import com.wishwzp.model.Student2;
    16 import com.wishwzp.util.HibernateUtil;
    17 
    18 public class StudentTest {
    19 
    20     private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
    21     private Session session;
    22     
    23     @Before
    24     public void setUp() throws Exception {
    25         session=sessionFactory.openSession(); // 生成一个session
    26         session.beginTransaction(); // 开启事务
    27     }
    28 
    29     @After
    30     public void tearDown() throws Exception {
    31          session.getTransaction().commit(); // 提交事务
    32          session.close(); // 关闭session
    33     }
    34 
    35     @Test
    36     public void testSave1(){
    37         Course course1=new Course();
    38         course1.setName("语文");
    39         
    40         Course course2=new Course();
    41         course2.setName("数学");
    42         
    43         Student student1=new Student();
    44         student1.setName("张三");
    45         student1.getCourses().add(course1);
    46         student1.getCourses().add(course2);
    47         
    48         Student student2=new Student();
    49         student2.setName("李四");
    50         student2.getCourses().add(course1);
    51         student2.getCourses().add(course2);
    52         
    53         session.save(student1);
    54         session.save(student2);
    55     }
    56     
    57     
    58 }

    我们现在查询一下数据:

    StudentTest.java

     1 package com.wishwzp.service;
     2 
     3 import java.util.Iterator;
     4 import java.util.Set;
     5 
     6 import org.hibernate.Session;
     7 import org.hibernate.SessionFactory;
     8 import org.junit.After;
     9 import org.junit.Before;
    10 import org.junit.Test;
    11 
    12 import com.wishwzp.model.Course;
    13 import com.wishwzp.model.Course2;
    14 import com.wishwzp.model.Student;
    15 import com.wishwzp.model.Student2;
    16 import com.wishwzp.util.HibernateUtil;
    17 
    18 public class StudentTest {
    19 
    20     private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
    21     private Session session;
    22     
    23     @Before
    24     public void setUp() throws Exception {
    25         session=sessionFactory.openSession(); // 生成一个session
    26         session.beginTransaction(); // 开启事务
    27     }
    28 
    29     @After
    30     public void tearDown() throws Exception {
    31          session.getTransaction().commit(); // 提交事务
    32          session.close(); // 关闭session
    33     }
    34 
    35     @Test
    36     public void testSave1(){
    37         Course course1=new Course();
    38         course1.setName("语文");
    39         
    40         Course course2=new Course();
    41         course2.setName("数学");
    42         
    43         Student student1=new Student();
    44         student1.setName("张三");
    45         student1.getCourses().add(course1);
    46         student1.getCourses().add(course2);
    47         
    48         Student student2=new Student();
    49         student2.setName("李四");
    50         student2.getCourses().add(course1);
    51         student2.getCourses().add(course2);
    52         
    53         session.save(student1);
    54         session.save(student2);
    55     }
    56     
    57     @Test
    58     public void testLoad1(){
    59         Student student=(Student)session.get(Student.class, 1);
    60         Set<Course> courses=(Set<Course>)student.getCourses();
    61         Iterator it=courses.iterator();
    62         while(it.hasNext()){
    63             Course c=(Course)it.next();
    64             System.out.println(c.getName());
    65         }
    66     }
    67     
    68     
    69     
    70 }

     我们这里是单向的,只能从学生这端去查找课程,无法从课程这端查找学生的,这就是单向的。

    2,多对多双向实现:

     Course2.java

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

    Student2.java

     1 package com.wishwzp.model;
     2 
     3 import java.util.HashSet;
     4 import java.util.Set;
     5 
     6 public class Student2 {
     7 
     8     private int id;
     9     private String name;
    10     private Set<Course2> courses=new HashSet<Course2>();
    11     
    12     public int getId() {
    13         return id;
    14     }
    15     public void setId(int id) {
    16         this.id = id;
    17     }
    18     public String getName() {
    19         return name;
    20     }
    21     public void setName(String name) {
    22         this.name = name;
    23     }
    24     public Set<Course2> getCourses() {
    25         return courses;
    26     }
    27     public void setCourses(Set<Course2> courses) {
    28         this.courses = courses;
    29     }
    30     
    31     
    32     
    33     
    34 }

    Course2.hbm.xml

     1 <?xml version="1.0"?>
     2 <!DOCTYPE hibernate-mapping PUBLIC
     3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
     4         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
     5 
     6 <hibernate-mapping package="com.wishwzp.model">
     7 
     8     <class name="Course2" table="t_course2">
     9         <id name="id" column="courseId">
    10             <generator class="native"></generator>
    11         </id>
    12         
    13         <property name="name" column="courseName"></property>
    14         
    15         <set name="students" table="student_course2" inverse="true" >
    16             <key column="course_id"></key>
    17             <many-to-many class="com.wishwzp.model.Student2" column="student_id"></many-to-many>
    18         </set>
    19         
    20     </class>
    21 
    22 </hibernate-mapping>

    Student2.hbm.xml

     1 <?xml version="1.0"?>
     2 <!DOCTYPE hibernate-mapping PUBLIC
     3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
     4         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
     5 
     6 <hibernate-mapping package="com.wishwzp.model">
     7 
     8     <class name="Student2" table="t_student2">
     9         <id name="id" column="studentId">
    10             <generator class="native"></generator>
    11         </id>
    12         
    13         <property name="name" column="studentName"></property>
    14         
    15         <set name="courses" table="student_course2" cascade="save-update">
    16             <key column="student_id"></key>
    17             <many-to-many class="com.wishwzp.model.Course2" column="course_id"></many-to-many>
    18         </set>
    19     </class>
    20 
    21 </hibernate-mapping>

    hibernate.cfg.xml

     1 <?xml version='1.0' encoding='utf-8'?>
     2 <!DOCTYPE hibernate-configuration PUBLIC
     3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
     4         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
     5 
     6 <hibernate-configuration>
     7 
     8     <session-factory>
     9 
    10         <!--数据库连接设置 -->
    11         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    12         <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
    13         <property name="connection.username">root</property>
    14         <property name="connection.password">123456</property>
    15 
    16        
    17         <!-- 方言 -->
    18         <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
    19     
    20         <!-- 控制台显示SQL -->
    21         <property name="show_sql">true</property>
    22 
    23         <!-- 自动更新表结构 -->
    24         <property name="hbm2ddl.auto">update</property>
    25 
    26         <mapping resource="com/wishwzp/model/Student2.hbm.xml"/>
    27           <mapping resource="com/wishwzp/model/Course2.hbm.xml"/>
    28 
    29     </session-factory>
    30 
    31 </hibernate-configuration>

    StudentTest.java

     1 package com.wishwzp.service;
     2 
     3 import java.util.Iterator;
     4 import java.util.Set;
     5 
     6 import org.hibernate.Session;
     7 import org.hibernate.SessionFactory;
     8 import org.junit.After;
     9 import org.junit.Before;
    10 import org.junit.Test;
    11 
    12 import com.wishwzp.model.Course;
    13 import com.wishwzp.model.Course2;
    14 import com.wishwzp.model.Student;
    15 import com.wishwzp.model.Student2;
    16 import com.wishwzp.util.HibernateUtil;
    17 
    18 public class StudentTest {
    19 
    20     private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
    21     private Session session;
    22     
    23     @Before
    24     public void setUp() throws Exception {
    25         session=sessionFactory.openSession(); // 生成一个session
    26         session.beginTransaction(); // 开启事务
    27     }
    28 
    29     @After
    30     public void tearDown() throws Exception {
    31          session.getTransaction().commit(); // 提交事务
    32          session.close(); // 关闭session
    33     }
    34 
    35     
    36     @Test
    37     public void testSave2(){
    38         
    39     }
    40     
    41     
    42 }

     StudentTest.java

     1 package com.wishwzp.service;
     2 
     3 import java.util.Iterator;
     4 import java.util.Set;
     5 
     6 import org.hibernate.Session;
     7 import org.hibernate.SessionFactory;
     8 import org.junit.After;
     9 import org.junit.Before;
    10 import org.junit.Test;
    11 
    12 import com.wishwzp.model.Course;
    13 import com.wishwzp.model.Course2;
    14 import com.wishwzp.model.Student;
    15 import com.wishwzp.model.Student2;
    16 import com.wishwzp.util.HibernateUtil;
    17 
    18 public class StudentTest {
    19 
    20     private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
    21     private Session session;
    22     
    23     @Before
    24     public void setUp() throws Exception {
    25         session=sessionFactory.openSession(); // 生成一个session
    26         session.beginTransaction(); // 开启事务
    27     }
    28 
    29     @After
    30     public void tearDown() throws Exception {
    31          session.getTransaction().commit(); // 提交事务
    32          session.close(); // 关闭session
    33     }
    34 
    35     
    36     @Test
    37     public void testSave2(){
    38         Course2 course1=new Course2();
    39         course1.setName("语文");
    40         
    41         Course2 course2=new Course2();
    42         course2.setName("数学");
    43         
    44         Student2 student1=new Student2();
    45         student1.setName("张三");
    46         student1.getCourses().add(course1);
    47         student1.getCourses().add(course2);
    48         
    49         Student2 student2=new Student2();
    50         student2.setName("李四");
    51         student2.getCourses().add(course1);
    52         student2.getCourses().add(course2);
    53         
    54         session.save(student1);
    55         session.save(student2);
    56     }
    57     
    58     
    59 }

    我们现在查询一下数据:

    StudentTest.java

     1 package com.wishwzp.service;
     2 
     3 import java.util.Iterator;
     4 import java.util.Set;
     5 
     6 import org.hibernate.Session;
     7 import org.hibernate.SessionFactory;
     8 import org.junit.After;
     9 import org.junit.Before;
    10 import org.junit.Test;
    11 
    12 import com.wishwzp.model.Course;
    13 import com.wishwzp.model.Course2;
    14 import com.wishwzp.model.Student;
    15 import com.wishwzp.model.Student2;
    16 import com.wishwzp.util.HibernateUtil;
    17 
    18 public class StudentTest {
    19 
    20     private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
    21     private Session session;
    22     
    23     @Before
    24     public void setUp() throws Exception {
    25         session=sessionFactory.openSession(); // 生成一个session
    26         session.beginTransaction(); // 开启事务
    27     }
    28 
    29     @After
    30     public void tearDown() throws Exception {
    31          session.getTransaction().commit(); // 提交事务
    32          session.close(); // 关闭session
    33     }
    34 
    35     
    36     @Test
    37     public void testSave2(){
    38         Course2 course1=new Course2();
    39         course1.setName("语文");
    40         
    41         Course2 course2=new Course2();
    42         course2.setName("数学");
    43         
    44         Student2 student1=new Student2();
    45         student1.setName("张三");
    46         student1.getCourses().add(course1);
    47         student1.getCourses().add(course2);
    48         
    49         Student2 student2=new Student2();
    50         student2.setName("李四");
    51         student2.getCourses().add(course1);
    52         student2.getCourses().add(course2);
    53         
    54         session.save(student1);
    55         session.save(student2);
    56     }
    57     
    58     @Test
    59     public void testLoad2(){
    60         Course2 course=(Course2)session.get(Course2.class, 1);
    61         Set<Student2> students=(Set<Student2>)course.getStudents();
    62         Iterator it=students.iterator();
    63         while(it.hasNext()){
    64             Student2 s=(Student2)it.next();
    65             System.out.println(s.getName());
    66         }
    67         
    68     }
    69     
    70     
    71 }

     我们这里是双向的,既可以从学生这端去查找课程,又可以从课程这端查找学生的,这就是双向的。

  • 相关阅读:
    java最新工作流引擎Activiti7管理流程创建申请经理审核财务审核结案
    Activivi7使用步骤
    mysql数据存放的位置在哪
    关于JWT 和Token(转)
    golang go get 时提示 no Go files in xxx
    golang中的URL 的编码和解码(转)
    什么是 .gitkeep ?
    golang gin 框架读取无法用 body 传递的表单参数
    Goland 开启文件保存自动进行格式化 的两种方式
    golang 接口变量的赋值和方法的调用
  • 原文地址:https://www.cnblogs.com/wishwzp/p/5486014.html
Copyright © 2020-2023  润新知