日韩欧美亚州-日韩欧美亚洲国产-日韩欧美亚洲国产高清在线-日韩欧美亚洲精品-色135综合网-色91在线

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

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

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 外鍵關(guān)聯(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設(shè)置為非空,將無法保存。應(yīng)為不是student這一端維護關(guān)系,所以student不知道是哪個班級,所以需要發(fā)出多余的update語句來更新關(guān)系。
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 復(fù)合主鍵mapping
  通常將復(fù)合主鍵相關(guān)屬性,單獨放到一個類中
  這個類必須實現(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)上報名
北大青鳥招生簡章
主站蜘蛛池模板: 亚洲九九| 亚洲精品免费网站| 亚洲精品成人一区二区| 欧美精品99| 国产高清在线观看| 亚洲欧美精品一中文字幕| 女同日韩互慰互摸在线观看| 97欧美精品一区二区三区| 欧美成人片在线| 国产a级三级三级三级中国| 亚欧国产| 高清不卡毛片| 久久一区二区三区99| 99久久99热精品免费观看国产| 日本aa毛片a级毛片免费观看| 草草伊人| 久草网站在线观看| 亚洲欧美视频网站| 成年网在线观看免费观看网址| 欧美精品一区二区三区免费 | 欧美高清在线精品一区二区不卡| 国产片一级| 手机在线色| 97在线观看免费版| 精品一区二区三区三区| 一个人看的www日本高清视频| 加勒比一本一道在线| 欧美精品一区二区在线观看| 99久在线| 国产乱子伦在线观看不卡| 久久夜色邦福利网| 在线精品视频在线观看高清| 国产亚洲欧美在线视频| 蕾丝视频永久在线入口香蕉| 波多野结衣在线不卡| 欧美成人性做爰网站免费| 91久久福利国产成人精品| 国产手机视频| 久草青青视频| 日本一级~片免费永久| 特黄特a级特别特级特毛片|