简体   繁体   English

使用Hibernate和Spring MVC在级联上保存包

[英]Save bag on cascade using Hibernate and Spring MVC

I have te following parent class 我有以下家长班

public class Parent {

    private Integer id;
    private List<Child> detail = ShrinkableLazyList.decorate(
            new ArrayList<Child>(),
            FactoryUtils.instantiateFactory(Child.class));

With it's corresponding HBM as follows: 与之对应的HBM如下:

<class name="Parent" table="parents">
    <id name="id">
        <generator class="increment"></generator>
    </id>
    <bag name="detail" cascade="all-delete-orphan">
        <key column="parentId" not-null="true" />
        <one-to-many class="Child" />
    </bag>
</class>

In my JSP edit form I list this detail as follows: 在我的JSP编辑表单中,我列出了以下详细信息:

<c:forEach items="${ parent.detail }" var="child" varStatus="status">
    <tr class="detail">
        <td>
            <input name="detail[${ status.count }].id" type="hidden" value="${ child.id }">
            <input name="detail[${ status.count }].account" type="hidden" value="${ child.account.id }"><span>${ child.account }</span>
        </td>
    </tr>
</c:forEach>

But when I try to perform an update() on the parent object I receive the following error: 但是,当我尝试在父对象上执行update()时,出现以下错误:

identifier of an instance of Child was altered from 20 to 19; nested exception is org.hibernate.HibernateException: identifier of an instance of Child was altered from 20 to 19

This is my DAO code 这是我的DAO代码

@Override
public void update (Parent parent) {
    hibernateTemplate.update(parent);
}

Any idea? 任何想法? Thanks 谢谢

status.count是从1开始而不是从0开始的问题,这将导致分配的值具有偏移的索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM