hibernate 對應關係配置

配置實例一:

特殊配置:

Java代碼  收藏代碼
  1. <hibernate-mapping package="net.share_info.dxw.hibernate">  
  2.   
  3.    
  4.   
  5.     <class name="Smkhabitword" table="SMKHABITWORD" node="">  
  6.   
  7.         <id name="id" column="ID" type="java.lang.String" node="">  
  8.   
  9.             <generator class="uuid.hex" />  
  10.   
  11.         </id>  
  12.   
  13.    
  14.   
  15.         <property name="sortid" column="SORTID" type="java.lang.String" />  
  16.   
  17.         <property name="addtime" column="ADDTIME" type="java.util.Date" />  
  18.   
  19.         <property name="msg" column="MSG" type="java.lang.String" />  
  20.   
  21.         <property name="sortname" formula="( select s.sortname from SMKHABITWORDSORT s where s.id = sortid )" type="java.lang.String">  
  22.   
  23.         </property>  
  24.   
  25.     </class>  
  26.   
  27. </hibernate-mapping>  

  

 

一對多配置:

 

Java代碼  收藏代碼
  1. <hibernate-mapping>    
  2.   
  3.      <class name="com.shareinfo.model.Peruserinfo"    
  4.   
  5.           table="Peruserinfo">    
  6.   
  7.      <!--hibernate爲我們生成主鍵id-->    
  8.   
  9.     <id name="userid" type="long">    
  10.   
  11.        <generator class="sequence">    
  12.   
  13.         <param name="sequence">peruserinfo_userid</param>    
  14.   
  15.       </generator>    
  16.   
  17.      </id>    
  18.   
  19.      <property name="username"/>    
  20.   
  21.      <property name="password"/>  
  22.   
  23.    <set name="books" lazy="true" inverse="true" cascade="all" >    
  24.   
  25.         <key column="peruserid"/>   //這個是Book表的外鍵(book外的一個字段)  
  26.   
  27.         <one-to-many class="com.shareinfo.model.Bookinfo"/>    
  28.   
  29.    </set>      
  30.   
  31.   </class>    
  32.   
  33.  </hibernate-mapping>  

  

 

 

 

多對多配置:

Java代碼  收藏代碼
  1. <set  
  2.             name="courses"  
  3.             table="Student_Course_Link"   //中間表  
  4.             lazy="false"  
  5.             inverse="false"  
  6.             cascade="all"  
  7.             sort="unsorted"  
  8.         >  
  9.               <key  
  10.                   column="StudentId"  // Student_Course_Link的StudentId  
  11.               />  
  12.               <many-to-many  
  13.                   class="com.hellking.study.hibernate.Course"  
  14.                   column="CourseId"    
  15. // Student_Course_Link中的CourseId(意思就是用這個CourseId關聯Course的主鍵)  
  16.                   outer-join="auto"  
  17.               />  

 

一對多的配置:

  

Java代碼  收藏代碼
  1. <set name="books" lazy="false" inverse="true" cascade="all" >    
  2.   
  3.       <key column="peruserid"/>   // books表的peruserid  
  4.   
  5.       <one-to-many class="com.shareinfo.model.Bookinfo"/>    
  6.   
  7.  </set>   

 

多對一的配置:(學生映射表可以這樣配置)

Java代碼  收藏代碼
  1. <many-to-one  
  2.             name="classes"  
  3.             class="com.hellking.study.hibernate.Classes"  
  4.             cascade="none"  
  5.             outer-join="auto"  
  6.             update="true"  
  7.             insert="true"  
  8.             column="ClassesId" //當前表的ClassesId        />  

 

 

 

一對一的配置:

 

Java代碼  收藏代碼
  1. <one-to-one  
  2.   
  3.             name="address"  
  4.   
  5.             class="com.hellking.study.hibernate.Address"  
  6.   
  7.             cascade="none"  
  8.   
  9.             outer-join="auto"  
  10.   
  11.             constrained="false"  
  12.   
  13.         /> 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章