使用 Tapestry5 删除休眠实体给出:“具有相同标识符值的不同对象已与会话关联”

发布于 2024-08-03 15:24:08 字数 2186 浏览 3 评论 0原文

可能的重复:
Hibernate:具有相同标识符值的不同对象是已与会话关联

我在删除实体时抛出错误。我一次只有一个组中的用户,因此 User 类上是多对一关系。在页面类中,我有一个属性组:

@Property @Persist private Group group;

在页面激活时填充:

public void onActivate(Group g) {
    group = g;
}

单击删除 ActionLink 时,执行此操作:

@CommitAfter
public ListBillingUserGroups onActionFromDelete() {
    for (User u : getUsersInGroup())
        u.setGroup(null);
    session.delete(group);
    return listPage;
}

public List<User> getUsersInGroup() {
    Criteria c = session.createCriteria(User.class)
        .add(eq("company", ctx.getUser().getCompany()))
        .add(eq("group", group));
    return c.list();
}

当该组中没有用户时,就会发生删除,浏览器重定向到列表页面(如预期)。但是,如果组中有用户,那么我会收到从 hibernate 抛出的异常,并显示消息:

a different object with the same identifier value was already associated with the session: [my.package.Group#10]

with a stacktrace:

org.hibernate.engine.StatefulPersistenceContext.checkUniqueness(StatefulPersistenceContext.java:613)
org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:121)
org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:74)
org.hibernate.impl.SessionImpl.fireDelete(SessionImpl.java:793)
org.hibernate.impl.SessionImpl.delete(SessionImpl.java:771)
... my code ...

如果我将 onActionFromDelete 方法替换为:错误仍然会发生

@CommitAfter
public ListBillingUserGroups onActionFromDelete() {
    for (User u : getUsersInGroup())
        System.out.print(" >> " + u.getGroup());
    session.delete(group);
    return listPage;
}

(而不是获取约束破坏异常) ),但如果我将其替换为:

@CommitAfter
public ListBillingUserGroups onActionFromDelete() {
    session.delete(group);
    return listPage;
}

我会得到约束违规异常。

因此,它的行为就好像调用 getAllUsersInGroup 方法将组对象更改为表示相同实体(相同类、相同 id)的另一个对象。关于我做错了什么有任何提示吗?

Possible Duplicate:
Hibernate: different object with the same identifier value was already associated with the session

I have an error being thrown on deleting an entity. I have users in only one group at a time, so it's a ManyToOne relationship on the User class. In the page class I have a property group:

@Property @Persist private Group group;

that is populated on page activation:

public void onActivate(Group g) {
    group = g;
}

When the deletion ActionLink is clicked, this is executed:

@CommitAfter
public ListBillingUserGroups onActionFromDelete() {
    for (User u : getUsersInGroup())
        u.setGroup(null);
    session.delete(group);
    return listPage;
}

public List<User> getUsersInGroup() {
    Criteria c = session.createCriteria(User.class)
        .add(eq("company", ctx.getUser().getCompany()))
        .add(eq("group", group));
    return c.list();
}

When the group has no users in it, then the delete happens, and the browser redirects to the list page (as expected). However, if the group has users in, then I get an exception thrown from hibernate, with message:

a different object with the same identifier value was already associated with the session: [my.package.Group#10]

with a stacktrace:

org.hibernate.engine.StatefulPersistenceContext.checkUniqueness(StatefulPersistenceContext.java:613)
org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:121)
org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:74)
org.hibernate.impl.SessionImpl.fireDelete(SessionImpl.java:793)
org.hibernate.impl.SessionImpl.delete(SessionImpl.java:771)
... my code ...

The error still happens if I replace the onActionFromDelete method with:

@CommitAfter
public ListBillingUserGroups onActionFromDelete() {
    for (User u : getUsersInGroup())
        System.out.print(" >> " + u.getGroup());
    session.delete(group);
    return listPage;
}

the error still happens (as opposed to getting the constraint voilation exception), but if I replace it with:

@CommitAfter
public ListBillingUserGroups onActionFromDelete() {
    session.delete(group);
    return listPage;
}

I get the constraint violation exception.

So it behaves as if calling the getAllUsersInGroup method changes the group object to another object representing the same entity (same class, same id). Any hints as to what I am doing wrong ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小伙你站住 2024-08-10 15:24:09

Tapestry5 Wiki 上有一个关于此主题的操作方法:

http://wiki.apache.org/tapestry/ Tapestry5避免DifferentObjectWithSameIDExceptions

HTH

There is a HowTo about this topic at the Tapestry5 Wiki:

http://wiki.apache.org/tapestry/Tapestry5AvoidingDifferentObjectWithSameIDExceptions

HTH

当爱已成负担 2024-08-10 15:24:08

我认为你应该尝试:

@Persist("entity")

因为 @Pesist 注释(没有参数)将你的 Group 对象存储在会话中,当你删除它时,它是一个分离的对象。

http://tapestry.apache.org/tapestry5/tapestry-hibernate/userguide。另

一种方法是存储 Group id(而不是 Group Object)并在 onActivate 方法中检索该对象。

I think you should try:

@Persist("entity")

because the @Pesist annotation (without params) store your Group object in the session and by the time you delete it, is a detached object.

http://tapestry.apache.org/tapestry5/tapestry-hibernate/userguide.html

the alternative is to store the Group id (instead of the Group Object) and retrieve the object in the onActivate method.

找回味觉 2024-08-10 15:24:08

我不知道 Tapestry,但在 Hibernate 中...

当您尝试使用对象 A 删除实体(对:类和 id),而 Hibernate 会话已经有一个与该对相对应的实例 B 时,会发生此消息。

例如,加载用户会加载实体(可能是惰性的)。因此,要删除组,您应该使用 Hibernate 用作用户组的组实例(确切的 java 对象)。如果您使用另一个实例,您将得到该异常。

I don't know for Tapestry, but in Hibernate ...

This message happens when you try to delete an entity (pair : class and id) using an object A, while the Hibernate session already has an instance B that corresponds to this pair.

For example, loading your users loads the entity (possibly lazy). So to delete the group, you should use the group instance (the exact java object) that was used by Hibernate as the group of the users. If you use another instance, you will get that exception.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文