Hibernate的关系配置

一对一: <hibernate-mapping>     

<class name="cn.jnit.bean.User" table="T_user">    

<cache usage="read-write"/> --二级缓存       

<id name="id">           

<generator class="sequence">           

</generator>         

</id>         

<property name="pwd"></property>         

<property name="name"></property>         

<one-to-one name="ud" class="cn.jnit.bean.UserDetail" property-ref="user" cascade="all" ></one-to-one>       

</class>

</hibernate-mapping>

<hibernate-mapping>       

<class name="cn.jnit.bean.UserDetail" table="t_ud">       

<cache usage="read-write"/>         

<id name="id">           

<generator class="foreign">           

<param name="property">user</param>           

</generator>         

</id>         

<property name="address"></property>         

<property name="phone"></property>          

<one-to-one name="user" class="cn.jnit.bean.User" cascade="all"></one-to-one>       

</class>    

</hibernate-mapping>

*******************************************************************

一对多和多对一:

一对多

<hibernate-mapping>  

<class name="cn.jbit.bean.Dept" table="DEPT">   

<cache usage="read-write" />   

<id name="deptno">    <generator class="sequence"></generator>   </id>   

<property name="dname"></property>   

<property name="loc"></property>   

<set name="emps" cascade="save-update" inverse="true" order-by="empno desc"    lazy="false">    

<cache usage="read-only" />    

<key column="deptno"></key>    

<one-to-many class="cn.jbit.bean.Emp" />   

</set>  

</class>

</hibernate-mapping>

多对一:

<hibernate-mapping>  

<class name="cn.jbit.bean.Emp" table="Emp" lazy="false">   <cache usage="read-only" />   

<id name="empno">    <generator class="sequence"></generator>   </id>   

<property name="ename"></property>   

<property name="job"></property>   

<property name="mgr"></property>   

<property name="hiredate"></property>  

 <property name="sal"></property>  

 <property name="comm"></property>   

<many-to-one name="dept" column="deptno" class="cn.jbit.bean.Dept"    cascade="save-update">

</many-to-one>  

</class>

</hibernate-mapping>

*******************************************************************************

多对多:

<hibernate-mapping>    

<class name="cn.jbit.bean.Empl" table="T_Empl">        

<id name="id">           <generator class="sequence"></generator>         </id>        

<property name="pname"></property>        

<set name="projects" table="Project_Empl" cascade="save-update">         

<key column="eid"></key>         

<many-to-many class="cn.jbit.bean.Project" column="pid"></many-to-many>        

</set>      

</class>

</hibernate-mapping>

   <hibernate-mapping>    

<class name="cn.jbit.bean.Project" table="T_PROJECT">    

<id name="id">      <generator class="sequence"></generator>     </id>    

<property name="pname"></property>    

<property name="pflag" type="java.lang.Boolean" ></property>    

<set name="empls" table="Project_Empl" cascade="save-update">      

<key column="pid"></key>      

<many-to-many class="cn.jbit.bean.Empl" column="eid"></many-to-many>    

</set>    

</class>    

</hibernate-mapping>

两个一对多: 职员表和奖励表通过第三张表:职员奖励表关联,三个实体类

public class Emp {  private Integer id;  private String name;     private Set<EmpPri> empPris=new HashSet<EmpPri>(); }

public class Prize {  private Integer id;  private String type;  private String comment;  private Set<EmpPri> empPris = new HashSet<EmpPri>(); }

public class EmpPri {  private Integer id;  private String year;  private Emp emp;  private Prize pri; }

Emp.hbm.xml:

<hibernate-mapping>  

<class name="cn.jnti.bean.Emp" table="Emp">    

<id name="id">       <generator class="sequence"></generator>     </id>    

<property name="name"></property>    

<set name="empPris" cascade="save-update">     

<key column="eid"></key>     

<one-to-many class="cn.jnti.bean.EmpPri"/>    

</set>  

</class>

</hibernate-mapping>

Prize.hbm.xml:

<hibernate-mapping>  

<class name="cn.jnti.bean.Prize" table="Prize">    

<id name="id">       <generator class="sequence"></generator>     </id>    

<property name="type"></property>    

<property name="comment" column="P_comment"></property>        

<set name="empPris" cascade="save-update">     

<key column="pid"></key>     

<one-to-many class="cn.jnti.bean.EmpPri"/>    

</set>  

</class>

</hibernate-mapping>

EmpPri.hbm.xml:

<hibernate-mapping>  

<class name="cn.jnti.bean.EmpPri" table="Emp_Pri">    

<id name="id">       <generator class="sequence"></generator>     </id>    

<property name="year"></property>    

<many-to-one name="emp" column="eid" class="cn.jnti.bean.Emp" cascade="save-update"></many-to-one>    

<many-to-one name="pri" column="pid" class="cn.jnti.bean.Prize" cascade="save-update"></many-to-one>  

</class>

</hibernate-mapping>

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。