论坛首页 综合技术论坛

hibernate 多对 多,操作中间表

浏览 5621 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-10-09  
利用hibernate解决多对多的问题

1建立实体多对多关系Manytomany,利用hibernate去维护多对多关系,但是hibernate无提供中间表操作。
2在1的基础上,建立中间表,利用一对多关系。

这样可以在中间表添加其他属性,也可以利用hibernate多对多去维护

EG:
学生:ID,name
学科:ID,classname

中间表:sid,cid,SCORE

hibernate: @MANYTOMANY Student,Class_tb

@ManyToOne score ,



eg2:用户,会议多对多关系,中间表记录参加会议的信息

会议
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class table="djsys_netmeeting"
name="com.gdass.model.djsys.NetMeeting">
<id name="id">
<generator class="native" />
</id>
<property name="dyId" />
<property name="createName" />
<property name="orgid" />
<property name="orgname" />
<property name="title" />
<property name="startDate" />
<property name="endDate" />
<property name="flag" />
<property name="moderator" />
<property name="content" />
<property name="learndata" />
<property name="xmldatapath" />
<property name="toupiao" />
<property name="tpcon" />
<property name="determine" />
<property name="onlinedatapath" />
<property name="chengdu" />
<property name="attachment" />
<property name="seldomtime" />
<property name="noticedata" />
<!--
<map name="dangYuans" table="djsys_user_netmeet" >
<key column="nmid" not-null="true" />
<map-key-many-to-many column="dyid"
class="com.gdass.model.djsys.DangYuan" />
<composite-element
class="com.gdass.model.djsys.UserNetMeet">
<property name="looker_ornot" column="looker_ornot" />
<property name="flag" column="flag" />
</composite-element>
</map> -->
   <set table="djsys_user_netmeet" fetch="select" name="dangYuans">
      <key column="nmid"/>
      <many-to-many column="dyid" class="com.gdass.model.djsys.DangYuan"/>
    </set>

    <set name="userNetMeets" inverse="true"
cascade="all-delete-orphan">
<key column="nmid" />
<one-to-many class="com.gdass.model.djsys.UserNetMeet" />
</set>
<set name="voteTopics" inverse="true"
cascade="all-delete-orphan">
<key column="nmid" />
<one-to-many class="com.gdass.model.djsys.VoteTopic" />
</set>
</class>
</hibernate-mapping>

用户

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class table="djsys_dangyuan"
name="com.gdass.model.djsys.DangYuan">
<id name="id">
<generator class="native" />
</id>
<many-to-one name="user" column="uid" />
<many-to-one name="djorg" column="orgid" />
<property name="dangfei" />
<property name="changedate" />
<property name="username" />
<property name="rdsqdate" />
<property name="jjfzdate" />
<property name="fzdxdate" />
<property name="rdlxr" />
<set name="dyOrgs" inverse="true" cascade="all-delete-orphan">
<key column="dangYuanId" />
<one-to-many class="com.gdass.model.djsys.DyOrg" />
</set>
<set name="personAwards" inverse="true"
cascade="all-delete-orphan">
<key column="dangYuanId" />
<one-to-many class="com.gdass.model.djsys.PersonAward" />
</set>
<!--
<map name="netMeetings" table="djsys_user_netmeet">
<key column="dyid" not-null="true" />
<map-key-many-to-many column="nmid"
class="com.gdass.model.djsys.NetMeeting" />
<composite-element
class="com.gdass.model.djsys.UserNetMeet">
<property name="looker_ornot" column="looker_ornot" />
<property name="flag" column="flag" />
</composite-element>
</map> -->
    <set table="djsys_user_netmeet" fetch="select" lazy="true" name="netMeetings">
      <key column="dyid"/>
      <many-to-many column="nmid" class="com.gdass.model.djsys.NetMeeting"/>
    </set>

     <set table="djsys_user_netword" fetch="select" lazy="true" name="netWords">
      <key column="dyid"/>
      <many-to-many column="nwid" class="com.gdass.model.djsys.NetWord"/>
    </set>

</class>
</hibernate-mapping>

中间表,记录参加会议情况
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class table="djsys_user_netmeet"
name="com.gdass.model.djsys.UserNetMeet">
<composite-id>
       <key-many-to-one name="netMeeting" class="com.gdass.model.djsys.NetMeeting" column="nmid">            
         </key-many-to-one>  
      <key-property name="dyid" />
    </composite-id>

<property name="vdata" />
<property name="sdata" />
<property name="id" />
<property name="toupiao" />
<property name="ddata" />
<property name="ntype" />
<property name="cuichu" />
</class>
</hibernate-mapping>

论坛首页 综合技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics