【问题标题】:Hibernate,one-to-many, children not inserted with parent休眠,一对多,孩子不与父母一起插入
【发布时间】:2012-10-17 11:12:59
【问题描述】:

我有一个双向的一对多关系,即一个父母可以有很多孩子,一个孩子必须有一个父母,很多孩子可以有同一个父母。

请参阅以下休眠 xml 配置。这适用于加载对象。通过 id 选择父对象成功返回包含一组子对象(如果有)的父对象。

但是,当我创建一个新的父级时,添加一些子级,然后让 hibernate 创建父级,子级并没有随之保存。我会认为hibernate会为我解决这个问题?

我怀疑我的问题是因为父级有一个 db 托管序列作为 id,因此在插入父级之前,子级不能拥有其父级的 id。但这对hibernate来说应该不会太难处理吧?

父.hbm.xml;

<hibernate-mapping default-cascade="none">
    <class name="com.mydomain.ParentImpl" table="PARENT" dynamic-insert="false" dynamic-update="false">
        <id name="id" type="java.lang.Long" unsaved-value="null">
            <column name="ID" sql-type="BIGINT"/>
            <generator class="sequence">
                <param name="sequence">PARENT_SEQ</param>
            </generator>
        </id>
        ....
        lots of other properties
        ...
        <set name="children" lazy="true" fetch="select" inverse="true">
            <key foreign-key="parent_child_fkc">
                <column name="PARENT_FK" sql-type="BIGINT"/>
            </key>
            <one-to-many class="com.mydomain.ChildImpl" not-found="exception"/>
        </set>
   </class>
</hibernate-mapping>

子.hbm.xml;

<hibernate-mapping default-cascade="none">
    <class name="com.mydomain.ChildImpl" table="CHILD" dynamic-insert="false" dynamic-update="false">
        <id name="id" type="java.lang.Integer" unsaved-value="null">
            <column name="ID" sql-type="INTEGER"/>
            <generator class="sequence">
                <param name="sequence">child_seq</param>
            </generator>
        </id>
        <many-to-one name="parent" class="com.mydomain.ParentImpl" foreign-key="parent_child_fkc" not-null="true" lazy="proxy" fetch="select">
            <column name="PARENT_FK" not-null="true" sql-type="BIGINT"/>
        </many-to-one>
        ...
        lots of other properties
        ...
    </class>
</hibernate-mapping>

假设Parent类有一个方法;

public void addChild(Child child){
    if(children == null) {
        children = new HashSet<Child>();
    }
    children.add(child);
    child.setParent(this);
}

然后在某处的代码中;

Parent parent = new Parent();
parent.addChild(new Child());
parent.addChild(new Child());
getHibernateTemplate().save(parent);

结果是 Parent 保存到 PARENT 表,CHILD 表保持为空。

【问题讨论】:

    标签: java hibernate


    【解决方案1】:

    您需要使用映射和级联来保存子对象。

    <set name="children" inverse="true" cascade="all-delete-orphan">
    <key column="parent_id"/>
    <one-to-many class="Child"/>
    

    看这里:

    http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/example-parentchild.html#example-parentchild-update

    http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/collections.html

    【讨论】:

    • 谢谢,我已经做了你建议的改变,基本上 cascade="all-delete-orphan" 是我需要的。但是我现在得到一个 PropertyValueException 因为 Child.parent_fk 在保存孩子时为空。这是因为 Parent.id 仅在插入 IT 时才设置,并且 hibernate 尚未将其分配给 Child.parent_fk 字段?
    • 没关系,该异常是因为在从 VO 转换为域对象时,代码更下方未正确设置 Child.Parent 对象关系。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2020-12-11
    • 1970-01-01
    相关资源
    最近更新 更多