程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Hibernate各種映射關系總結

Hibernate各種映射關系總結

編輯:關於JAVA

1 多對一

第一步 :建立對象模型

User

-----------------------------

id:int

name:String

group:Group

Group

------------------------------

id:int

name:String

第二步:映射文件

Xml代碼

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.nintenyun.hibernate">
   <class name="User" table="t_user">
    <id name="id" column="id">
     <generator class="identity"/>
    </id>
    <property name="name" column="User_name" unique="true"/>
    <many-to-one name="group" column="groupid"/>
   </class>
</hibernate-mapping>

2 一對一映射

2.1 主鍵映射

1. 建立對象模型

Person[id-int,name-String]
IdCard[id-int,cardNo-int]

2. 映射文件

Xml代碼

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.nintenyun.hibernate">
   <class name="Person" table="t_person">
    <id name="id">
     <generator class="foreign">
     <param name="property">idCard</param>
     </generator>
    </id>
    <property name="name" column="Person_name" />
    <one-to-one name="idCard" constrained="true"/>
   </class>
</hibernate-mapping>

2.2 主鍵映射雙向

只需要在IdCard 的XML中添加<one-to-one>標簽指向person,就可以 告訴Hibernate如何加載person,默認按照主鍵加載

2.3 外鍵關聯映射

1. 對象模型

Person[id-int,name-String,idcard-IdCard]
IdCard[id-int,cardNo-int]

2 mapping文件

Xml代碼

Person.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.nintenyun.hibernate">
   <class name="Person" table="t_person">
    <id name="id">
     <generator class="identity">
     </generator>
    </id>
    <property name="name" column="Person_name" />
    <many-to-one name="idCard" unique="true"></many-to- one>
   </class>
</hibernate-mapping>

2.4 one-to-one 外鍵雙向mapping

只需要在另一端(IdCard)加入<one-to-one>標簽,但是hibernate默 認按照主鍵加載,所以要告訴hibernate按照指定列來加載

示意:

Xml代碼

Id-card
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.nintenyun.hibernate">
   <class name="IdCard" table="t_idcard">
    <id name="id" column="id">
     <generator class="identity"/>
    </id>
    <property name="cardNo" column="cardno" />
    <one-to-one name="person" property-ref="idCard"></one-to- one>
   </class>
</hibernate-mapping>

3 一對多映射

1  建立對象模型

Classes[id-int,name-String,student-Set]
Student[id-int,name-String]

2  映射文件

Xml代碼

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.nintenyun.hibernate">
   <class name="Class" table="t_class">
    <id name="id">
     <generator class="native"/>
    </id>
    <property name="name" />
    <set name="student" >
    <key column="classid" />
    <one-to-many class="Student"/>
    </set>
   </class>
</hibernate-mapping>

缺點:若將Student的classid設置為非空,將無法保存。應為不是student這 一端維護關系,所以student不知道是哪個班級,所以需要發出多余的update語 句來更新關系。

(一對多): 先要session.sava(學生),再sessio.save(班級)

(多對一): 先要session.sava(班級),再sessio.save(學生)

改進 采用 一對多雙向關聯

Xml代碼

<set name="student" inverse="true">
   <set name="student" inverse="true">

這樣的好處是 存入數據讓多的一端進行維護,不需要發出多余的 update.

4 多對多mapping

單向(采用第三方表)

1  對象模型

Role[id-int,name-String]
User[id-int,name-String,role-Set]

2映射文件

Xml代碼

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.nintenyun.hibernate">
   <class name="User" table="t_user">
    <id name="id">
     <generator class="native"/>
    </id>
    <property name="name" />
    <set name="role" table="t_user_role">
    <key column="userid"></key>
    <many-to-many class="Role" column="roleid" />
    </set>
   </class>
</hibernate-mapping>

雙向:

1 對象模型:

Role[id-int,name-String,user-Set]
User[id-int,name-String,role-Set]

2 mapping文件:

Xml代碼

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.nintenyun.hibernate">
   <class name="Role" table="t_role">
    <id name="id">
     <generator class="native"/>
    </id>
    <property name="name" />
    <set name="user" table="t_user_role">
    <key column="userid"></key>
    <many-to-many class="User" column="userid" />
    </set>
   </class>
</hibernate-mapping>

5 繼承mapping

第一類繼承映射

1 對象模型

Animal
private int id
private String name
private Boolean sex
--------------------------------------------------
public class Bird extends Animal
private int height
--------------------------------------------------
public class Pig extends Animal
private int weight

2 mapping 文件

Xml代碼

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.nintenyun.hibernate">
   <class name="Animal" table="t_animal" >
    <id name="id" >
     <generator class="native"/>
    </id>
    <discriminator column="type"  type="string"></discriminator>
    <property name="name"/>
    <property name="sex"/>
    <subclass name="Pig" discriminator-value="P">
     <property name="weight"/>
    </subclass>
    <subclass name="Bird" discriminator-value="B">
     <property name="height"/>
    </subclass>
   </class>

</hibernate-mapping>

第二類mapping

1 對象模型

2 mapping 文件

Xml代碼

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.nintenyun.hibernate">
   <class name="Animal" table="t_animal" >
    <id name="id" >
     <generator class="native"/>
    </id>
    <property name="name"/>
    <property name="sex"/>
     <joined-subclass name="Pig" table="t_pig">
      <key column="pid"/>
      <property name="weight"/>
     </joined-subclass>
     <joined-subclass name="Bird" table="t_bird">
      <key column="bid"/>
      <property name="height"/>
     </joined-subclass>
</class>
</hibernate-mapping>

3 第三類mapping

1對象模型

2 mapping 文件

Xml代碼

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.nintenyun.hibernate">
   <class name="Animal" table="t_animal" >
    <id name="id" >
     <generator class="native"/>
    </id>
    <property name="name"/>
    <property name="sex"/>
    <union-subclass name="Pig" table="t_pig">
    <property name="weight"/>
    </union-subclass>
    <union-subclass name="Bird" table="t_bird">
    <property name="height"/>
    </union-subclass>
</class>
</hibernate-mapping>

6 Component 映射

component不是實體,沒有OID。

1 對象模型

public class Employee
private int id
private String name
private Contact contact
-------------------------------------
public class Contact  
private String email
private String address
private String zipcode
private String contactTel
-------------------------------------
public class User
private int id
private String name
private Contact contact

2 mapping文件

Xml代碼

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.nintenyun.hibernate">
   <class name="User" table="t_user" >
    <id name="id" >
     <generator class="native"/>
    </id>
    <property name="name"/>
    <component name="contact">
    <property name="email"></property>
    <property name="address"></property>
    <property name="zipcode"></property>
    <property name="contactTel"></property>
    </component>
   </class>
  </hibernate-mapping>

 

7 復合主鍵mapping

通常將復合主鍵相關屬性,單獨放到一個類中

這個類必須實現 Serializable接口

這個類必須重寫hashCode()和equals()方法

XML

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.nintenyun.hibernate">
   <class name="SC" table="t_sc" >
    <composite-id name="scpk">
    <key-property name="sno"/>
    <key-property name="cno"/>
    </composite-id>
    <property name="sname"></property>
    <property name="cname"></property>
   </class>
</hibernate-mapping>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved