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

时间:2015-11-09 19:18:25

标签: java spring hibernate spring-mvc

我有以下父类

public class Parent {

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

使用相应的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>

在我的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>

但是当我尝试在父对象上执行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

这是我的DAO代码

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

有什么想法吗?感谢

1 个答案:

答案 0 :(得分:1)

status.count基于1而非基于0的问题会导致分配的值具有移位索引。