久草最新视频-久草最新网址-久国产-久国产视频-天天插夜夜操-天天插一插

北京北大青鳥通州校區(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>標(biāo)簽指向person,就可以告訴Hibernate如何加載person,默認(rèn)按照主鍵加載
  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>標(biāo)簽,但是hibernate默認(rèn)按照主鍵加載,所以要告訴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>
  缺點(diǎn):若將Student的classid設(shè)置為非空,將無法保存。應(yīng)為不是student這一端維護(hù)關(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)屬性,單獨(dú)放到一個類中
  這個類必須實現(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)上報名
北大青鳥招生簡章
主站蜘蛛池模板: 青青青草视频| 日韩一区二区三区中文字幕| 一级毛片免费在线播放| 日本动漫黄网站在线观看| 青青青青青青久久久免费观看 | 日本jizz在线播放| 欧美成人剧场| 色哟哟国产成人精品| 四虎成人免费网址在线| 欧美成人精品在线| 一级毛片免费观看视频| 青青久久精品国产免费看| 日韩精品亚洲专区在线观看| 欧美在线中文| 骚碰人人| 欧美视频精品在线观看| 一级美国乱色毛片| 欧美日韩一区二区在线| 日韩精选| 欧美激情精品久久久久久大尺度| 三级黄色片日本| 午夜影院美女| 欧美精品一区二区三区久久| 亚洲国产精品一区二区三区久久 | 一级毛片不收费| 人人曰| 四虎一影院区永久精品| 四虎影视国产884a精品亚洲| 午夜成人在线视频| 欧美一级久久久久久久大片| 午夜高清福利| 欧美成综合网网站| 日本二区免费一片黄2019| 日本一道免费一区二区三区| 日本一区二区三区四区不卡| 欧美精品做人一级爱免费| 日韩在线观看一区二区不卡视频| 亚洲精品aaa| 欧美区亚洲区| 三级aa视频在线观看| 青青青国产免费线在|