首页 > 代码库 > Hibernate关联映射

Hibernate关联映射

1.单项多对一关联

首先创建实体类

public class Qx {

private int jdid;
private String jdname;
// Getters & Setters ...

}

public class Jd{

private int qxid;
private String qxname;

private Qx qx

 // Getters & Setters ..;

}

<!--  Qx类映射文件 - -> 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.yunhe.entity.Qx" table="tb_qx">
<id name="qxid" column="qxid" type="java.lang.Integer">
<generator class="native"/>
</id>
<property name="qxname" column="qxname" type="java.lang.String"/>

<set name="jds" inverse="false" cascade="all"> <!--反转-->
<key column="qxid"/>
<one-to-many class="cn.yunhe.entity.Jd"/>
</set>
</class>

</hibernate-mapping>

<!-- Jd映射文件 - ->

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.yunhe.entity.Jd" table="tb_jd">
<id name="jdid" column="jdid" type="java.lang.Integer">
<generator class="native"/>
</id>
<property name="jdname" column="jdname" type="java.lang.String"/>

<!--多对一-->
<many-to-one name="qx" class="cn.yunhe.entity.Qx" column="qx_id"/>

</class>
</hibernate-mapping>

 

配置文件,下文省略

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hbm2ddl.auto">update</property> <!--正向工程-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/head</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">076634</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>

 

<mapping resource="hbm/Jd.hbm.xml"/>
<mapping resource="hbm/Qx.hbm.xml"/>
<mapping resource="hbm/Course.hbm.xml"/>
<mapping resource="hbm/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>

 

2:双对多

public class Course {

private int courseid;
private String courseName;

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

 

public class Student {

//学生

private int studentid;
private String studentname;
private Set<Course> courses = new HashSet<Course>();

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!-- 课程文件 -->

<hibernate-mapping>
<class name="cn.yunhe.entity.Course" table="course">
<id name="courseid" column="courseid" type="java.lang.Integer">
<generator class="native"/>
</id>
<property name="coursename" column="coursename"/>




<set name="students" inverse="true" table="r_course">
<!-- 首先对应自己的外键 cours——id -->
<key>
<column name="course_id" precision="11" scale="0"/>
</key>

<!--多对多-->
<many-to-many entity-name="cn.yunhe.entity.Student">
<column name="stu_id" precision="11" scale="0"/>
</many-to-many>
</set>
</class>
</hibernate-mapping>

 

 

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.yunhe.entity.Student" table="student">
<id name="studentid" column="studentid">
<generator class="native"/>
</id>

<property name="studentname" column="studentname" type="java.lang.String"/>




<set name="courses" table="r_course">
<key>
<column name="stu_id"/>
</key>

<!-- 多对多 -->
<many-to-many entity-name="cn.yunhe.entity.Course">
<column name="course_id"/>
</many-to-many>
</set>

</class>
</hibernate-mapping>

 

测试类

 

package cn.yunhe.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import cn.yuhe.entity.Course;
import cn.yuhe.entity.Student;


public class Demo {

Session session = null;
Transaction tr = null;

@Before
public void setUp() {
Configuration config = new Configuration().configure();
SessionFactory factory = config.buildSessionFactory();
session = factory.openSession();
tr = session.beginTransaction();
}


@Test
public void manyTomany(){

Course course1 = new Course();
Course course2 = new Course();

course1.setCourseName("java");
course2.setCourseName("c");

session.save(course1);
session.save(course2);

Student stu1 = new Student();
Student stu2 = new Student();

stu1.setStudentName("小样");
stu2.setStudentName("集雨");

stu1.getCourses().add(course1);
stu1.getCourses().add(course2);
stu2.getCourses().add(course1);
stu2.getCourses().add(course2);

session.save(stu1);
session.save(stu2);
tr.commit();
}

@After
public void tearDown() {
session.close();
}


}

 

Hibernate关联映射