hibernate多对多

10 篇文章 0 订阅

SQL:

CREATE TABLE TB_STUDENT
(
    ID INTEGER PRIMARY KEY,
    NAME VARCHAR2(20) NOT NULL
);

CREATE SEQUENCE SQ_STUDENT
INCREMENT BY 1
START WITH 1
NOMAXVALUE
NOCYCLE
CACHE 10;

CREATE TABLE TB_COURSE
(
    ID INTEGER PRIMARY KEY,
    NAME VARCHAR2(20) NOT NULL
);

CREATE SEQUENCE SQ_COURSE
INCREMENT BY 1
START WITH 1
NOMAXVALUE
NOCYCLE
CACHE 10;

CREATE TABLE TB_STUDENT_COURSE
(
    STUDENT_ID INTEGER NOT NULL REFERENCES TB_STUDENT(ID),
    COURSE_ID INTEGER NOT NULL REFERENCES TB_COURSE(ID),
    PRIMARY KEY(STUDENT_ID, COURSE_ID)
);

java代码:

package com.many2many.pojo;

import java.util.Set;

public class Course {
	private int id;

	private String name;
	
	private Set<Student> studentSet;

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

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

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

package com.many2many.pojo;

import java.util.Set;

public class Student {
	private int id;

	private String name;
	
	private Set<Course> 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;
	}

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

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

hbm:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
      "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.many2many.pojo">

	<class name="Course" table="tb_course">
		<id name="id" column="id" type="integer">
			<generator class="sequence">
				<param name="sequence">SQ_COURSE</param>
			</generator>
		</id>
		<property name="name" column="name" type="string" />
		<set name="studentSet" table="tb_student_course" cascade="save-update">
			<key column="course_id"></key>
			<many-to-many class="Student" column="student_id"></many-to-many>
		</set>
	</class>

</hibernate-mapping>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
      "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.many2many.pojo">

	<class name="Student" table="tb_student">
		<id name="id" column="id" type="integer">
			<generator class="sequence">
				<param name="sequence">SQ_STUDENT</param>
			</generator>
		</id>
		<property name="name" column="name" type="string" />
		<set name="courseSet" table="tb_student_course" cascade="save-update">
			<key column="student_id"></key>
			<many-to-many class="Course" column="course_id"></many-to-many>
		</set>
	</class>

</hibernate-mapping>

oracle.hibernate.cfg.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>

		<!-- Database connection settings -->
		<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
		<property name="connection.url">jdbc:oracle:thin:@localhost:1521:ORCL</property>
		<property name="connection.username">HIBERNATE</property>
		<property name="connection.password">HIBERNATE</property>

		<property name="connection.pool_size">1</property>

		<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>

		<property name="show_sql">true</property>

		<property name="hibernate.show_sql">true </property>
		
		<property name="jdbc.fetch_size">50</property>
		
		<mapping resource="com/many2many/pojo/Course.hbm.xml"/>
		<mapping resource="com/many2many/pojo/Student.hbm.xml"/>

	</session-factory>

</hibernate-configuration>

测试代码:

package com.many2many.test;

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

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.many2many.pojo.Course;
import com.many2many.pojo.Student;

import junit.framework.TestCase;

public class TestMany2Many extends TestCase {
	private Session session = null;

	private static final String cfgName = "/oracle.hibernate.cfg.xml";

	private static final SessionFactory factory = new Configuration().configure(cfgName).buildSessionFactory();

	private Transaction tran = null;

	private static final String[] courseNames = new String[] { "语文", "数学", "英语", "生物", "化学", "物理", "历史", "地理", "政治" };
	
	private static final String[] studentNames = new String[] {"赵云", "关羽", "刘备", "诸葛亮", "曹操", "司马懿", "孙权", "张飞", "周瑜"};

	@Override
	protected void setUp() throws Exception {
		session = factory.openSession();
		tran = session.beginTransaction();
	}

	public void testInsert() {
		Set<Course> courseSet = new HashSet<Course>();
		Set<Student> studentSet = new HashSet<Student>();
		
		for (String courseName: courseNames) {
			Course course = new Course();
			course.setName(courseName);
			courseSet.add(course);
		}
		for (String studentName: studentNames) {
			Student student = new Student();
			student.setName(studentName);
			studentSet.add(student);
		}
		
		for (Student student: studentSet) {
			student.setCourseSet(courseSet);
			session.save(student);
		}
		
//		保存student时自动会保存course,所以下面的代码,必须注释掉
//		for (Course course: courseSet) {
//			course.setStudentSet(studentSet);
//			session.save(course);
//		}

		tran.commit();
	}

	@Override
	protected void tearDown() throws Exception {
		tran = null;
		session.close();
		session = null;
	}

}

查询数据库:

SQL> SELECT * FROM TB_STUDENT;

        ID NAME
---------- --------------------
        55 张飞
        56 司马懿
        57 关羽
        58 孙权
        59 周瑜
        60 曹操
        61 诸葛亮
        62 赵云
        63 刘备

已选择9行。

SQL> SELECT * FROM TB_COURSE;

        ID NAME
---------- --------------------
        37 语文
        38 地理
        39 英语
        40 历史
        41 物理
        42 政治
        43 化学
        44 生物
        45 数学

已选择9行。

SQL> SELECT * FROM TB_STUDENT_COURSE;

STUDENT_ID  COURSE_ID
---------- ----------
        55         37
        55         38
        55         39
        55         40
        55         41
        55         42
        55         43
        55         44
        55         45
        56         37
        56         38

STUDENT_ID  COURSE_ID
---------- ----------
        56         39
        56         40
        56         41
        56         42
        56         43
        56         44
        56         45
        57         37
        57         38
        57         39
        57         40

STUDENT_ID  COURSE_ID
---------- ----------
        57         41
        57         42
        57         43
        57         44
        57         45
        58         37
        58         38
        58         39
        58         40
        58         41
        58         42

STUDENT_ID  COURSE_ID
---------- ----------
        58         43
        58         44
        58         45
        59         37
        59         38
        59         39
        59         40
        59         41
        59         42
        59         43
        59         44

STUDENT_ID  COURSE_ID
---------- ----------
        59         45
        60         37
        60         38
        60         39
        60         40
        60         41
        60         42
        60         43
        60         44
        60         45
        61         37

STUDENT_ID  COURSE_ID
---------- ----------
        61         38
        61         39
        61         40
        61         41
        61         42
        61         43
        61         44
        61         45
        62         37
        62         38
        62         39

STUDENT_ID  COURSE_ID
---------- ----------
        62         40
        62         41
        62         42
        62         43
        62         44
        62         45
        63         37
        63         38
        63         39
        63         40
        63         41

STUDENT_ID  COURSE_ID
---------- ----------
        63         42
        63         43
        63         44
        63         45

已选择81行。

SQL>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值