熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Hibernate各種映射關系總結

2022-06-13   來源: Java開源技術 

   多對一

  第一步 建立對象模型

  User

  

  idint

  nameString

  groupGroup

  Group

  

  id:int

  name:String

  第二步映射文件

  Xml代碼

  <?xml version=?>

  <!DOCTYPE hibernatemapping PUBLIC

  //Hibernate/Hibernate Mapping DTD //EN

  hiber/hibernatemappingdtd>

  <hibernatemapping package=comnintenyunhibernate>

  <class name=User table=t_user>

  <id name=id column=id>

  <generator class=identity/>

  </id>

  <property name=name column=User_name unique=true/>

  <manytoone name=group column=groupid/>

  </class>

  </hibernatemapping>

   一對一映射

   主鍵映射

   建立對象模型

  Person[idintnameString]

  IdCard[idintcardNoint]

   映射文件

  Xml代碼

  <?xml version=?>

  <!DOCTYPE hibernatemapping PUBLIC

  //Hibernate/Hibernate Mapping DTD //EN

  hiber/hibernatemappingdtd>

  <hibernatemapping package=comnintenyunhibernate>

  <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 />

  <onetoone name=idCard constrained=true/>

  </class>

  </hibernatemapping>

   主鍵映射雙向

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

   外鍵關聯映射

   對象模型

  Person[idintnameStringidcardIdCard]

  IdCard[idintcardNoint]

   mapping文件

  Xml代碼

  Personxml

  <?xml version=?>

  <!DOCTYPE hibernatemapping PUBLIC

  //Hibernate/Hibernate Mapping DTD //EN

  hiber/hibernatemappingdtd>

  <hibernatemapping package=comnintenyunhibernate>

  <class name=Person table=t_person>

  <id name=id>

  <generator class=identity>

  </generator>

  </id>

  <property name=name column=Person_name />

  <manytoone name=idCard unique=true></manyto one>

  </class>

  </hibernatemapping>

   onetoone 外鍵雙向mapping

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

  示意

  Xml代碼

  Idcard

  <?xml version=?>

  <!DOCTYPE hibernatemapping PUBLIC

  //Hibernate/Hibernate Mapping DTD //EN

  hiber/hibernatemappingdtd>

  <hibernatemapping package=comnintenyunhibernate>

  <class name=IdCard table=t_idcard>

  <id name=id column=id>

  <generator class=identity/>

  </id>

  <property name=cardNo column=cardno />

  <onetoone name=person propertyref=idCard></oneto one>

  </class>

  </hibernatemapping>

   一對多映射

    建立對象模型

  Classes[idintnameStringstudentSet]

  Student[idintnameString]

    映射文件

  Xml代碼

  <?xml version=?>

  <!DOCTYPE hibernatemapping PUBLIC

  //Hibernate/Hibernate Mapping DTD //EN

  hiber/hibernatemappingdtd>

  <hibernatemapping package=comnintenyunhibernate>

  <class name=Class table=t_class>

  <id name=id>

  <generator class=native/>

  </id>

  <property name=name />

  <set name=student >

  <key column=classid />

  <onetomany class=Student/>

  </set>

  </class>

  </hibernatemapping>

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

  (一對多) 先要sessionsava(學生)再sessiosave(班級)

  (多對一) 先要sessionsava(班級)再sessiosave(學生)

  改進 采用 一對多雙向關聯

  Xml代碼

  <set name=student inverse=true>

  <set name=student inverse=true>

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

   多對多mapping

  單向(采用第三方表)

    對象模型

  Role[idintnameString]

  User[idintnameStringroleSet]

  映射文件

  Xml代碼

  <?xml version=?>

  <!DOCTYPE hibernatemapping PUBLIC

  //Hibernate/Hibernate Mapping DTD //EN

  hiber/hibernatemappingdtd>

  <hibernatemapping package=comnintenyunhibernate>

  <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>

  <manytomany class=Role column=roleid />

  </set>

  </class>

  </hibernatemapping>

  雙向

   對象模型

  Role[idintnameStringuserSet]

  User[idintnameStringroleSet]

   mapping文件

  Xml代碼

  <?xml version=?>

  <!DOCTYPE hibernatemapping PUBLIC

  //Hibernate/Hibernate Mapping DTD //EN

  hiber/hibernatemappingdtd>

  <hibernatemapping package=comnintenyunhibernate>

  <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>

  <manytomany class=User column=userid />

  </set>

  </class>

  </hibernatemapping>

   繼承mapping

  第一類繼承映射

   對象模型

  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

   mapping 文件

  Xml代碼

  <?xml version=?>

  <!DOCTYPE hibernatemapping PUBLIC

  //Hibernate/Hibernate Mapping DTD //EN

  hiber/hibernatemappingdtd>

  <hibernatemapping package=comnintenyunhibernate>

  <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 discriminatorvalue=P>

  <property name=weight/>

  </subclass>

  <subclass name=Bird discriminatorvalue=B>

  <property name=height/>

  </subclass>

  </class>

  </hibernatemapping>

  第二類mapping

   對象模型

   mapping 文件

  Xml代碼

  <?xml version=?>

  <!DOCTYPE hibernatemapping PUBLIC

  //Hibernate/Hibernate Mapping DTD //EN

  hiber/hibernatemappingdtd>

  <hibernatemapping package=comnintenyunhibernate>

  <class name=Animal table=t_animal >

  <id name=id >

  <generator class=native/>

  </id>

  <property name=name/>

  <property name=sex/>

  <joinedsubclass name=Pig table=t_pig>

  <key column=pid/>

  <property name=weight/>

  </joinedsubclass>

  <joinedsubclass name=Bird table=t_bird>

  <key column=bid/>

  <property name=height/>

  </joinedsubclass>

  </class>

  </hibernatemapping>

   第三類mapping

  對象模型

   mapping 文件

  Xml代碼

  <?xml version=?>

  <!DOCTYPE hibernatemapping PUBLIC

  //Hibernate/Hibernate Mapping DTD //EN

  hiber/hibernatemappingdtd>

  <hibernatemapping package=comnintenyunhibernate>

  <class name=Animal table=t_animal >

  <id name=id >

  <generator class=native/>

  </id>

  <property name=name/>

  <property name=sex/>

  <unionsubclass name=Pig table=t_pig>

  <property name=weight/>

  </unionsubclass>

  <unionsubclass name=Bird table=t_bird>

  <property name=height/>

  </unionsubclass>

  </class>

  </hibernatemapping>

   Component 映射

  component不是實體沒有OID

   對象模型

  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

   mapping文件

  Xml代碼

  <?xml version=?>

  <!DOCTYPE hibernatemapping PUBLIC

  //Hibernate/Hibernate Mapping DTD //EN

  hiber/hibernatemappingdtd>

  <hibernatemapping package=comnintenyunhibernate>

  <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>

  </hibernatemapping>

   復合主鍵mapping

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

  這個類必須實現 Serializable接口

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

  XML

  <?xml version=?>

  <!DOCTYPE hibernatemapping PUBLIC

  //Hibernate/Hibernate Mapping DTD //EN

  hiber/hibernatemappingdtd>

  <hibernatemapping package=comnintenyunhibernate>

  <class name=SC table=t_sc >

  <compositeid name=scpk>

  <keyproperty name=sno/>

  <keyproperty name=cno/>

  </compositeid>

  <property name=sname></property>

  <property name=cname></property>

  </class>

  </hibernatemapping>


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28675.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.