当前位置: 代码迷 >> Java Web开发 >> Hibernate 一对多 再一对多的有关问题
  详细解决方案

Hibernate 一对多 再一对多的有关问题

热度:103   发布时间:2016-04-16 21:56:29.0
Hibernate 一对多 再一对多的问题
有这样一个需求:某人多套房产,这是单向1对多;每套房产又可能有多个产权共有人,这也是单向1对多。

那么综上:就是1对多,再1对多。级联的关系。

请问,这样怎么用Hibernate实现呢?谢谢!
------解决方案--------------------
class Person{
     Set<House> houses = new HashSet<House>();
    //set和get方法
}

class House{
    private Person person;
     Set<Equity> equitys = new HashSet<Equity>();
     //set和get方法
}

class Equity{   
      private House house;
}

在Person.hbm.xml文件中配置:
<set name="houses">
     <key column="houseId"></key>
     <one-to-many class="House" />
</set>



在House.hbm.xml中配置:
<many-to-one name="person" class="Person" column="houseId"></many-to-one>
<set name="equitys ">
     <key column="equityId "></key>
     <one-to-many class="Equity " />
    </set>
在Equity.hbm.xml中配置:
<many-to-one name="house" class="House" column="equityId"></many-to-one>

我也只是个新手...
  相关解决方案