hibernate插入一对多

时间:2012-04-04 16:42:15

标签: hibernate

I have a class called A which has set of object of B. On inserting the A all the objects in the set must be inserted. But in my case only the object A is inserting not the sets.

注意,B类也有对象集合,在某些情况下可能为空。 关系:第1页----> *类 Page.hbm.xml

 <hibernate-mapping>
      <class catalog="uml" name="com.project.lwuml.classdiagram.entity.Page" table="page">
        <id name="id" type="java.lang.Integer">
          <column name="id"/>
          <generator class="identity"/>
        </id>
        <property name="content" type="string">
          <column length="65535" name="content"/>
        </property>
        <set inverse="true" name="classboxes" table="classbox" lazy="true" fetch="select">
          <key>
            <column name="page_id" not-null="true"/>
          </key>
          <one-to-many class="com.project.lwuml.classdiagram.entity.Classbox"/>
        </set>
      </class>
    </hibernate-mapping>

Classboxes.hbm.xml

<hibernate-mapping>
      <class catalog="uml" name="com.project.lwuml.classdiagram.entity.Classbox" table="classbox">
       <id name="id" type="java.lang.Integer">
          <column name="id"/>
          <generator class="identity"/>
        </id>
       <many-to-one class="com.project.lwuml.classdiagram.entity.Accessspecifiers" fetch="select" name="accessspecifiers">
          <column name="accessspecifiers_id" not-null="true"/>
        </many-to-one>
       <many-to-one class="com.project.lwuml.classdiagram.entity.Page" fetch="select" name="page">
          <column name="page_id" not-null="true"/>
        </many-to-one>
       <property name="className" type="string">
          <column length="128" name="className"/>
        </property>
       <set inverse="true" lazy="true" name="classmethods">
          <key>
            <column name="classbox_id" not-null="true"/>
          </key>
          <one-to-many class="com.project.lwuml.classdiagram.entity.Classmethod"/>
        </set>
       <set inverse="true" name="classdatamembers">
          <key>
            <column name="classbox_id" not-null="true"/>
          </key>
          <one-to-many class="com.project.lwuml.classdiagram.entity.Classdatamember"/>
        </set>
      </class>
   </hibernate-mapping>

我正在使用struts-spring-hibernate。任何身体都可以帮助我。 提前致谢

1 个答案:

答案 0 :(得分:0)

我认为您需要为多对一标记添加级联属性,以便在更新父级时保留子级。

   <many-to-one class="com.project.lwuml.classdiagram.entity.Page" fetch="select" cascade="save-update" name="page">
      <column name="page_id" not-null="true"/>
    </many-to-one>

http://pdngayan.blogspot.com/2010/02/cascade-attribute-in-hibernate-hbmxml.html

http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/mapping.html#mapping-declaration-manytoone