北京北大青鳥通州校區(qū)提供:Hibernate各種映射關系總結

北京北大青鳥通州校區(qū)學術部提供:

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 外鍵關聯(lián)映射
  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不知道是哪個班級,所以需要發(fā)出多余的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
  通常將復合主鍵相關屬性,單獨放到一個類中
  這個類必須實現(xiàn) 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>

 

北大青鳥網(wǎng)上報名
北大青鳥招生簡章