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

Hibernate- 多对多

时间:2017-08-22 23:18:53      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:root   app   oca   driver   turn   www.   als   integer   doctype   

实体类:

Student.java:

package com.gordon.domain;

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

public class Student {
	private Integer id;
	private String name;

	private Set<Course> courses = new HashSet<Course>();

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Set<Course> getCourses() {
		return courses;
	}

	public void setCourses(Set<Course> courses) {
		this.courses = courses;
	}

}

Course.java:

package com.gordon.domain;

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

public class Course {
	private Integer id;
	private String name;

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

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Set<Student> getStudents() {
		return students;
	}

	public void setStudents(Set<Student> students) {
		this.students = students;
	}

}

 

hbm映射文件:

Student.hbm.xml:

<?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.gordon.domain.Student" table="t_student">
		<id name="id" column="id">
			<generator class="native" />
		</id>

		<property name="name" column="name" />

		<set name="courses" table="t_student_course" cascade="save-update" inverse="false" lazy="true">
			<key column="student_id"/>
			<many-to-many column="course_id" class="com.gordon.domain.Course"/>
		</set>

	</class>
</hibernate-mapping>

Course.hbm.xml:

<?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.gordon.domain.Course" table="t_course">
		<id name="id" column="id">
			<generator class="native" />
		</id>

		<property name="name" column="name" />

		<set name="students" table="t_student_course" cascade="save-update" inverse="true" lazy="true">
			<key column="course_id"/>
			<many-to-many column="student_id" class="com.gordon.domain.Student"/>
		</set>

	</class>
</hibernate-mapping>

 

核心配置文件:

<?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.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/relation</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">root</property>
		
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		
		<property name="hbm2ddl.auto">update</property>
		
		<property name="hibernate.show_sql">true</property>
		<property name="hibernate.format_sql">false</property>
		
		<mapping resource="com/gordon/domain/Student.hbm.xml"/>
		<mapping resource="com/gordon/domain/Course.hbm.xml"/>
		
	</session-factory>
</hibernate-configuration>

 

测试类:

package com.gordon.test;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import com.gordon.domain.Course;
import com.gordon.domain.Student;
import com.gordon.utils.HibernateUtil;

public class TestN2N {
	/**
	 * 删除数据
	 */
	@Test
	public void run4() {
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();

		// 删除学生1的3课程
		 Student student = session.get(Student.class, new Integer(1));
		 Course course = session.get(Course.class, new Integer(3));
		 student.getCourses().remove(course);
		
		transaction.commit();
		session.close();
	}

	/**
	 * 修改数据
	 */
	@Test
	public void run3() {
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();

		// 将学生2的3课程删除
		// Student student = session.get(Student.class, new Integer(2));
		// Course course_ori = session.get(Course.class, 3);
		// student.getCourses().remove(course_ori);

		// 为学生1添加3课程
		// Student student = session.get(Student.class, new Integer(1));
		// Course course = session.get(Course.class, new Integer(3));
		// student.getCourses().add(course);
		
		transaction.commit();
		session.close();
	}

	/**
	 * 查询数据
	 */
	@Test
	public void run2() {
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();

		Student student = session.get(Student.class, new Integer(1));
		System.out.println("学生姓名:" + student.getName());
		System.out.println("所选课程:");
		for (Course c : student.getCourses()) {
			System.out.println("--" + c.getName());
		}

		Course course = session.get(Course.class, new Integer(3));
		System.out.println("课程名称:" + course.getName());
		System.out.println("选此课程的学生:");
		for (Student s : course.getStudents()) {
			System.out.println("--" + s.getName());
		}

		transaction.commit();
		session.close();
	}

	/**
	 * 添加数据
	 */
	@Test
	public void run1() {
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();

		Student student1 = new Student();
		student1.setName("张三");
		Student student2 = new Student();
		student2.setName("张三");

		Course course1 = new Course();
		course1.setName("高数");
		Course course2 = new Course();
		course2.setName("线代");
		Course course3 = new Course();
		course3.setName("离散");

		student1.getCourses().add(course1);
		student1.getCourses().add(course2);

		student2.getCourses().add(course1);
		student2.getCourses().add(course2);
		student2.getCourses().add(course3);

		session.save(student1);
		session.save(student2);

		transaction.commit();
		session.close();
	}
}

 

Hibernate- 多对多

标签:root   app   oca   driver   turn   www.   als   integer   doctype   

原文地址:http://www.cnblogs.com/hfultrastrong/p/7413737.html

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