欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-18 22:30:46
hibernate多对多 关联映射(many-to-many) 在操作和性能方面都不太理想,所以多对多的映射使用较少,实际使用中最好转换成一对多的对象模型; hiber...

hibernate多对多 关联映射(many-to-many)

在操作和性能方面都不太理想,所以多对多的映射使用较少,实际使用中最好转换成一对多的对象模型;

hibernate会为我们创建中间关联表,转换成两个一对多。

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

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

(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>

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接

上一篇:

下一篇: