原创

Hibernate实现many-to-many的映射关系

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://xuliugen.blog.csdn.net/article/details/39969513


(1)一个最简单的例子就是学生选课的数据表了


(2)Student.java

public class Course {
	private Integer id;
	private String name;
	private Set<StuCourse> stuCourses;
	//get/set方法
}

(3)Student.java

public class Student {
	private Integer id;
	private String name;
	private Set<StuCourse> stuCourses;
	<span style="font-family: Arial, Helvetica, sans-serif;">//get/set方法</span>

}
(4)StuCourse.java学生选课表

package com.hsp.domain;

public class StuCourse {
	private Integer id;
	private Student student;
	private Course course;
	private Integer grade;
	//get/set方法
	
}

(5)Course.hbm.xml文件

<?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 package="com.hsp.domain">
	<class name="Course">
		<id name="id" type="java.lang.Integer">
			<generator class="sequence">
				<param name="sequence">course_seq</param>
			</generator>
		</id>
		<property name="name" type="java.lang.String">
			<column name="name" length="64" />
		</property>
		<!-- 配置one-to-many 表示一门课程可以对应多个选课记录 -->
		<set name="stuCourses">
			<key column="course_id" />
			<one-to-many class="StuCourse" />
		</set>
	</class>
</hibernate-mapping>

(6)Student.hbm.xml文件

<?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 package="com.hsp.domain">
	<class name="Student">
		<id name="id" type="java.lang.Integer">
			<generator class="sequence">
				<param name="sequence">stu_seq</param>
			</generator>
		</id>
		<property name="name" type="java.lang.String">
			<column name="name" length="64" />
		</property>
	 	<!-- 这里我们配置了one-to-many 一个学生可以对应多个选课记录 -->
		<set name="stuCourses">
			<key column="student_id" /> <!-- 这里的column是外键 -->
			<one-to-many class="StuCourse" /> <!-- many所对应的表 -->
		</set>
	</class>
</hibernate-mapping>

(7)StuCourse.hbm.xml文件

<?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 package="com.hsp.domain">
	<class name="StuCourse">
		<id name="id" type="java.lang.Integer">
			<generator class="sequence">
				<param name="sequence">stucourse_seq</param>
			</generator>
		</id>
		<property name="grade" type="java.lang.Integer">
			<column name="grade" length="3" />
		</property>
		<many-to-one name="course" column="course_id" />
		<many-to-one name="student" column="student_id" />
	</class>
</hibernate-mapping>

(8)hibernate.cfg.xml文件

<?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">

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>

	<session-factory>
		<property name="connection.username">root</property>
		<property name="connection.url">
			jdbc:oracle:thin:@127.0.0.1:1521:OracleDB
		</property>
		<property name="dialect">
			org.hibernate.dialect.Oracle9Dialect
		</property>
		<property name="connection.password">root</property>
		<property name="connection.driver_class">
			oracle.jdbc.driver.OracleDriver
		</property>
		<property name="show_sql">true</property>
		<!-- 配置让hibernate自动创建关系模型(表) -->
		<property name="hbm2ddl.auto">update</property>
		<mapping resource="com/hsp/domain/Course.hbm.xml" />
		<mapping resource="com/hsp/domain/StuCourse.hbm.xml" />
		<mapping resource="com/hsp/domain/Student.hbm.xml" />

	</session-factory>

</hibernate-configuration>






0 个人打赏
文章最后发布于: 2014-10-10 21:25:02
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

打赏

徐刘根

“你的鼓励将是我创作的最大动力”

5C币 10C币 20C币 50C币 100C币 200C币

分享到微信朋友圈

×

扫一扫,手机浏览