hibernate 多对多关系中间属性关联方法

Hibernate 框架可以将面向关系的数据库模型封装成面向对象的数据库模型,这样以来一方面了我们在具体应用中的数据库设计,另一方面又简化了我们的编码。然而在现实中我们往往会发现有些面向关系的数据库模型转化成面向对象的数据库模型之后很难再 Hibernate 中配置。比如:在多对多关系模型中,中间表往往含有自己的属性,这种情况在 Hibernate 中该如何配置?我在网上查了好多资料都说将多对多拆成两个一对多的方式来解决,这样做也不错,不过我个人觉得使用起来比较麻烦,更主要的一点是这种方法将 Hibernate 面向对象的设计思想降低到了关系型设计, Hibernate 带来的好处就只有编码方便这一点了。

 


下面我给大家提供一种 Hibernate 中多对多关系表中有属性的使用方法。

1 、 Project 和 Programmer 的实体关系图如下:

2 、将 project 、 programmer 、 participate 封装成三个实体

 

view plaincopy to clipboardprint?
 public class Project {  
   
       private Integer projectId;  
       private String projectNumber;  
       private String projectName;  
       private Map programmers;  
        
       get..  
       set..  
}  
public class Programmer {  
   
       private Integer programmerId;  
       private String programmerName;  
       private Map projects;  
        
       get..  
       set..  
}  
public class Participate {  
   
       private Date startDate;  
       private Date endDate;  
        
       get..  
       set..  

 public class Project {
 
       private Integer projectId;
       private String projectNumber;
       private String projectName;
       private Map programmers;
     
       get..
       set..
}
public class Programmer {
 
       private Integer programmerId;
       private String programmerName;
       private Map projects;
     
       get..
       set..
}
public class Participate {
 
       private Date startDate;
       private Date endDate;
     
       get..
       set..
}

 

3 、配置属性文件

 

Project.hbm.xml

 

view plaincopy to clipboardprint?
<hibernate-mapping > 
    <class name="Project" table="test.tb_project"> 
        <id name="projectId"> 
            <column name="id" /> 
            <generator class="native"/>         
        </id> 
        <property name="projectNumber" > 
            <column name="project_number" /> 
        </property> 
<property name=" projectName " > 
            <column name=" project_name" /> 
        </property> 
        <map name="programmers" table="test.tb_project_programmer" lazy="false"> 
         <key column="project_id" not-null="true"/> 
         <map-key-many-to-many column="programmer_id" class="Programmer"/> 
         <composite-element class="Participate"> 
            <property name=" startDate " column="start_date"/> 
            <property name=" endDate " column="end_date"/> 
         </composite-element> 
       </map> 
    </class> 
</hibernate-mapping> 
<hibernate-mapping >
    <class name="Project" table="test.tb_project">
        <id name="projectId">
            <column name="id" />
            <generator class="native"/>      
        </id>
        <property name="projectNumber" >
            <column name="project_number" />
        </property>
<property name=" projectName " >
            <column name=" project_name" />
        </property>
        <map name="programmers" table="test.tb_project_programmer" lazy="false">
         <key column="project_id" not-null="true"/>
         <map-key-many-to-many column="programmer_id" class="Programmer"/>
         <composite-element class="Participate">
            <property name=" startDate " column="start_date"/>
            <property name=" endDate " column="end_date"/>
         </composite-element>
       </map>
    </class>
</hibernate-mapping>

 

Programmer.hbm.xml

 


view plaincopy to clipboardprint?
<hibernate-mapping >  
    <class name="Programmer" table="test.tb_programmer" lasy=”false”>  
        <id name="programmerId">  
            <column name="id" />  
            <generator class="native"/>         
        </id>  
        <property name="programmerName" >  
            <column name="programmer_name" />  
        </property>  
        <map name="projects" table="test.tb_project_programmer" lazy="false" inverse=”true”>  
        <key column=" programmer _id" not-null="true"/>  
         <map-key-many-to-many column="project_id" class="Project"/>  
         <composite-element class="Participate">  
            <property name=" startDate " column="start_date"/>  
            <property name=" endDate " column="end_date"/>  
         </composite-element>  
       </map>  
    </class>  
</hibernate-mapping> 
<hibernate-mapping >
    <class name="Programmer" table="test.tb_programmer" lasy=”false”>
        <id name="programmerId">
            <column name="id" />
            <generator class="native"/>      
        </id>
        <property name="programmerName" >
            <column name="programmer_name" />
        </property>
        <map name="projects" table="test.tb_project_programmer" lazy="false" inverse=”true”>
        <key column=" programmer _id" not-null="true"/>
         <map-key-many-to-many column="project_id" class="Project"/>
         <composite-element class="Participate">
            <property name=" startDate " column="start_date"/>
            <property name=" endDate " column="end_date"/>
         </composite-element>
       </map>
    </class>
</hibernate-mapping>

 

 

4 、你调用调试就可以了

 

注意:

       在其中一方要用 inverse= “ true ”来实现控制翻转,在这一方的 <calss /> 要添加 lasy= “ false ”的属性,以便对方控制。

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/mini_snow/archive/2010/05/17/5599902.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值