hibernate总结:Hibernate各种映射关系整理总结

  1 多对

  第步 :建立对象模型

  User                                            

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

  id:

  name:String

  group:Group

  Group

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

  id:

  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.nenyun.hibernate"> 
  < name="User" table="t_user"> 
   <id name="id" column="id"> 
    <generator ="identity"/> 
   </id> 
   <property name="name" column="User_name" unique="true"/> 
   <many-to-one name="group" column="groupid"/> 
  </> 
</hibernate-mapping>


  2 映射

  2.1 主键映射

  1. 建立对象模型

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


  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.nenyun.hibernate"> 
  < name="Person" table="t_person"> 
   <id name="id"> 
    <generator ="foreign"> 
    <param name="property">idCard</param> 
    </generator> 
   </id> 
   <property name="name" column="Person_name" /> 
   <one-to-one name="idCard" constrained="true"/> 
  </> 
</hibernate-mapping>


  2.2 主键映射双向

  只需要在IdCard XML中添加<one-to-one>标签指向person就可以告诉Hibernate如何加载person默认按照主键加载

  2.3 外键关联映射

  1. 对象模型

Person[id-name-Stringidcard-IdCard]
IdCard[id-cardNo-]


  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.nenyun.hibernate"> 
  < name="Person" table="t_person"> 
   <id name="id"> 
    <generator ="identity"> 
    </generator> 
   </id> 
   <property name="name" column="Person_name" /> 
   <many-to-one name="idCard" unique="true"></many-to-one> 
  </> 
</hibernate-mapping>


  2.4 _disibledevent=><!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping package="com.nenyun.hibernate"> 
  < name="IdCard" table="t_idcard"> 
   <id name="id" column="id"> 
    <generator ="identity"/> 
   </id> 
   <property name="cardNo" column="cardno" /> 
   <one-to-one name="person" property-ref 
 
 
 
 
 
 
="idCard"></one-to-one> 
  </> 
</hibernate-mapping>

  3 对多映射

  1  建立对象模型

Classes[id-,name-String,student-Set]
Student[id-,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.nenyun.hibernate"> 
  < name="Class" table="t_"> 
   <id name="id"> 
    <generator ="native"/> 
   </id> 
   <property name="name" /> 
   < name="student" > 
   <key column="id" /> 
   <one-to-many ="Student"/> 
   </> 
  </> 
</hibernate-mapping>


  缺点:若将Studentid设置为非空将无法保存应为不是student这端维护关系所以student不知道是哪个班级所以需要发出多余update语句来更新关系

  (对多): 先要session.sava(学生)再sessio.save(班级)

  (多对): 先要session.sava(班级)再sessio.save(学生)

  改进 采用 对多双向关联

  Xml代码

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


  这样好处是 存入数据让多端进行维护不需要发出多余update.

  4 多对多mapping

  单向(采用第 3方表)

  1  对象模型

Role[id-,name-String]
User[id-,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.nenyun.hibernate"> 
  < name="User" table="t_user"> 
   <id name="id"> 
    <generator ="native"/> 
   </id> 
   <property name="name" /> 
   < name="role" table="t_user_role"> 
   <key column="userid"></key> 
   <many-to-many ="Role" column="roleid" /> 
   </> 
  </> 
</hibernate-mapping>


  双向:

  1 对象模型:

Role[id-,name-String,user-Set]
User[id-,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.nenyun.hibernate"> 
  < name="Role" table="t_role"> 
   <id name="id"> 
    <generator ="native"/> 
   </id> 
   <property name="name" /> 
   < name="user" table="t_user_role"> 
   <key column="userid"></key> 
   <many-to-many ="User" column="userid" /> 
   </> 
  </> 
</hibernate-mapping> 


  5 继承mapping

  第类继承映射

  1 对象模型

Animal 
private  id
private String name
private Boolean sex
--------------------------------------------------
public  Bird extends Animal 
private  height
--------------------------------------------------
public  Pig extends Animal
private  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.nenyun.hibernate"> 
  < name="Animal" table="t_animal" > 
   <id name="id" > 
    <generator ="native"/> 
   </id> 
   <discriminator column="type" type=""></discriminator> 
   <property name="name"/> 
   <property name="sex"/> 
   <sub name="Pig" discriminator-value="P"> 
    <property name="weight"/> 
   </sub> 
   <sub name="Bird" discriminator-value="B"> 
    <property name="height"/> 
   </sub> 
  </> 
  
 
  
</hibernate-mapping>


  第 2类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.nenyun.hibernate"> 
  < name="Animal" table="t_animal" > 
   <id name="id" > 
    <generator ="native"/> 
   </id> 
   <property name="name"/> 
   <property name="sex"/> 
    <joined-sub name="Pig" table="t_pig"> 
     <key column="pid"/> 
     <property name="weight"/> 
    </joined-sub> 
    <joined-sub name="Bird" table="t_bird"> 
     <key column="bid"/> 
     <property name="height"/> 
    </joined-sub> 
</> 
</hibernate-mapping>


  3 第 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.nenyun.hibernate"> 
  < name="Animal" table="t_animal" > 
   <id name="id" > 
    <generator ="native"/> 
   </id> 
   <property name="name"/> 
   <property name="sex"/> 
   <union-sub name="Pig" table="t_pig"> 
   <property name="weight"/> 
   </union-sub> 
   <union-sub name="Bird" table="t_bird"> 
   <property name="height"/> 
   </union-sub> 
</> 
</hibernate-mapping>


  6 Component 映射

  component不是实体没有OID

  1 对象模型

public  Employee 
private  id
private String name
private Contact contact
-------------------------------------
public  Contact  
private String email
private String address
private String zipcode
private String contactTel
-------------------------------------
public  User  
private  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.nenyun.hibernate"> 
  < name="User" table="t_user" > 
   <id name="id" > 
    <generator ="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> 
  </> 
 </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.nenyun.hibernate"> 
  < 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> 
  </> 
</hibernate-mapping>




Tags:  hibernate总结

延伸阅读

最新评论

发表评论