在Hibernate中重新附加分离对象的正确方法是什么? [英] What is the proper way to re-attach detached objects in Hibernate?

查看:117
本文介绍了在Hibernate中重新附加分离对象的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,我需要将分离的对象重新挂接到一个hibernate会话,但会话中可能已存在相同身份的对象,这会导致错误。



现在,我可以做两件事之一。


  1. getHibernateTemplate( ).update(obj)
    当且仅当一个对象在休眠会话中不存在时,它才起作用。当我需要它时,在会话中已经存在具有给定标识符的对象时抛出异常。

  2. (obj)
    当且仅当hibernate会话中存在一个对象时,这才起作用。鉴于这两种情况,在我需要对象进入会话后抛出异常。 ,我怎样才能将会话一般地附加到对象?我不想使用异常来控制这个问题的解决方案的流程,因为必须有一个更优雅的解决方案......

    解决方案

因此,似乎没有办法在JPA中重新附加陈旧的分离实体。


$ b

merge()会将陈旧状态推到数据库
并覆盖所有干预更新。
$ b

refresh()不能在分离的实体上调用。
$ b

lock()不能在分离的实体上调用
,即使它可以,并且它重新连接实体,
用参数'LockMode.NONE'调用'lock'
意味着你正在锁定,但不锁定
是这是我见过的最直截了当的API设计。



所以你被卡住了。
有一个 detach()方法,但没有 attach()重新附加()
对象生命周期中的一个明显步骤不适用于您。



从关于JPA的类似问题的数量来看,
似乎甚至如果JPA声称有一个连贯的模型,
它肯定与大多数程序员的心智模型不匹配,
被诅咒浪费了很多时间试图了解
如何让JPA做最简单的事情,并最终在其应用程序中缓存
管理代码。



看起来唯一的方法是放弃陈旧的分离实体
,然后做一个具有相同ID的查询查询,这将会打到L2或DB。



Mik


I have a situation in which I need to re-attach detached objects to a hibernate session, although an object of the same identity MAY already exist in the session, which will cause errors.

Right now, I can do one of two things.

  1. getHibernateTemplate().update( obj ) This works if and only if an object doesn't already exist in the hibernate session. Exceptions are thrown stating an object with the given identifier already exists in the session when I need it later.

  2. getHibernateTemplate().merge( obj ) This works if and only if an object exists in the hibernate session. Exceptions are thrown when I need the object to be in a session later if I use this.

Given these two scenarios, how can I generically attach sessions to objects? I don't want to use exceptions to control the flow of this problem's solution, as there must be a more elegant solution...

解决方案

So it seems that there is no way to reattach a stale detached entity in JPA.

merge() will push the stale state to the DB, and overwrite any intervening updates.

refresh() cannot be called on a detached entity.

lock() cannot be called on a detached entity, and even if it could, and it did reattach the entity, calling 'lock' with argument 'LockMode.NONE' implying that you are locking, but not locking, is the most counterintuitive piece of API design I've ever seen.

So you are stuck. There's an detach() method, but no attach() or reattach(). An obvious step in the object lifecycle is not available to you.

Judging by the number of similar questions about JPA, it seems that even if JPA does claim to have a coherent model, it most certainly does not match the mental model of most programmers, who have been cursed to waste many hours trying understand how to get JPA to do the simplest things, and end up with cache management code all over their applications.

It seems the only way to do it is discard your stale detached entity and do a find query with the same id, that will hit the L2 or the DB.

Mik

这篇关于在Hibernate中重新附加分离对象的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆