hibernate多對多 關(guān)聯(lián)映射(many-to-many)
在操作和性能方面都不太理想,所以多對多的映射使用較少,實際使用中最好轉(zhuǎn)換成一對多的對象模型;
hibernate會為我們創(chuàng)建中間關(guān)聯(lián)表,轉(zhuǎn)換成兩個一對多。
(1)一個最簡單的例子就是學(xué)生選課的數(shù)據(jù)表了
(2)student.java
1
2
3
4
5
6
|
public class course { private integer id; private string name; private set<stucourse> stucourses; //get/set方法 } |
(3)student.java
1
2
3
4
5
6
|
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學(xué)生選課表
1
2
3
4
5
6
7
8
|
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文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<?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 表示一門課程可以對應(yīng)多個選課記錄 --> <set name= "stucourses" > <key column= "course_id" /> <one-to-many class = "stucourse" /> </set> </ class > </hibernate-mapping> |
(6)student.hbm.xml文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<?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 一個學(xué)生可以對應(yīng)多個選課記錄 --> <set name= "stucourses" > <key column= "student_id" /> <!-- 這里的column是外鍵 --> <one-to-many class = "stucourse" /> <!-- many所對應(yīng)的表 --> </set> </ class > </hibernate-mapping> |
(7)stucourse.hbm.xml文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<?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文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<?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自動創(chuàng)建關(guān)系模型(表) --> <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> |
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對服務(wù)器之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
原文鏈接:https://blog.csdn.net/huanglong1218/article/details/52313047