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

Hibernate之多对多

时间:2016-01-03 00:31:15      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:

一、项目结构如下图

技术分享

二、代码如下(测试类中不能再有双向关联,否则会报错,因为,都维护了中间表外键,会有中间表外键冲突,如果非要写双向关联,就需要配置中设置某一方维护主键,课程和学生映射配置文件中相应配置inverse为true和false)

技术分享
package com.hjp.manytomany;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by JiaPeng on 2016/1/2.
 */
public class Course {
    private int id;
    private String name;

    public Set<Student> getStudentSet() {
        return studentSet;
    }

    public void setStudentSet(Set<Student> studentSet) {
        this.studentSet = studentSet;
    }

    private Set<Student> studentSet = new HashSet<Student>();

    @Override
    public String toString() {
        return "Course{" +
                "id=" + id +
                ", name=‘" + name + ‘\‘‘ +
                ‘}‘;
    }

    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;
    }
}
Course
技术分享
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="com.hjp.manytomany.Course" table="course">
      <id name="id">
          <generator class="native"></generator>
      </id>
      <property name="name" length="20"></property>
      <set name="studentSet" table="s_c">
          <key column="c_id"></key>
          <many-to-many class="com.hjp.manytomany.Student" column="s_id"></many-to-many>
      </set>
  </class>
</hibernate-mapping>
course.hbm.xml
技术分享
package com.hjp.manytomany;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by JiaPeng on 2016/1/2.
 */
public class Student {
    private int id;
    private String name;
    private Set<Course> courseSet = new HashSet<Course>();

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name=‘" + name + ‘\‘‘ +
                ", courseSet=" + courseSet +
                ‘}‘;
    }

    public Set<Course> getCourseSet() {
        return courseSet;
    }

    public void setCourseSet(Set<Course> courseSet) {
        this.courseSet = courseSet;
    }

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

}
Student
技术分享
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
   <class name="com.hjp.manytomany.Student" table="student">
       <id name="id">
           <generator class="native"></generator>
       </id>
       <property name="name" length="20"></property>
       <set name="courseSet" table="s_c">
           <key column="s_id"></key>
           <many-to-many class="com.hjp.manytomany.Course" column="c_id"></many-to-many>
       </set>
   </class>
</hibernate-mapping>
student.hbm.xml
技术分享
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/h1</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">hjp123</property>
        <!--设置c3p0-->
        <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
        <!--c3p0连接池配置信息-->
        <property name="c3p0.min_size">5</property>
        <property name="c3p0.max_size">20</property>
        <property name="c3p0.timeout">120</property>
        <property name="c3p0.idle_test_period">3000</property>
        <!--显示SQL并格式化-->
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
        <!--ddl生成策略,后面可以改为update-->
        <property name="hibernate.hbm2ddl.auto">create</property>
        <!--导入映射文件-->
        <mapping resource="com/hjp/manytomany/course.hbm.xml"></mapping>
        <mapping resource="com/hjp/manytomany/student.hbm.xml"></mapping>
    </session-factory>
</hibernate-configuration>
hibernate.cfg.xml
技术分享
 public void func3() {
        Session session = HibernateUtils.getSession();
        session.beginTransaction();
//创建两个学生
        Student student1 = new Student();
        student1.setName("张三");

        Student student2 = new Student();
        student2.setName("李四");
        //创建三门课程
        Course course1 = new Course();
        course1.setName("Java");

        Course course2 = new Course();
        course2.setName(".Net");

        Course course3 = new Course();
        course3.setName("php");

        //学生关联课程
        //张三java\php
        student1.getCourseSet().add(course1);
        student1.getCourseSet().add(course3);
        //李四php\.Net
        student2.getCourseSet().add(course2);
        student2.getCourseSet().add(course3);

        //课程关联学生
//        course1.getStudentSet().add(student1);
//        course2.getStudentSet().add(student2);
//        course3.getStudentSet().add(student1);
//        course3.getStudentSet().add(student2);

        session.save(student1);
        session.save(student2);
        session.save(course1);
        session.save(course2);
        session.save(course3);
        session.getTransaction().commit();
        session.close();
    }
测试类中测试方法

 

Hibernate之多对多

标签:

原文地址:http://www.cnblogs.com/hujiapeng/p/5095518.html

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