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

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

问题描述

我有一种情况,我需要将分离的对象重新附加到休眠会话,尽管会话中可能已经存在相同身份的对象,这会导致错误.

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)当且仅当休眠会话中不存在对象时,这才有效.当我稍后需要时,会抛出异常,说明具有给定标识符的对象已存在于会话中.

  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.

getHibernateTemplate().merge(obj)当且仅当休眠会话中存在对象时,这才有效.如果我使用它,当我稍后需要对象在会话中时会抛出异常.

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...

推荐答案

所以似乎没有办法在 JPA 中重新附加一个陈旧的分离实体.

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

merge() 会将过时的状态推送到数据库,并覆盖任何中间更新.

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

refresh() 不能在分离的实体上调用.

refresh() cannot be called on a detached entity.

lock() 不能在分离的实体上调用,即使它可以,而且它确实重新连接了实体,使用参数LockMode.NONE"调用lock"暗示您正在锁定,但未锁定,这是我见过的最违反直觉的 API 设计.

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.

所以你被卡住了.有一个 detach() 方法,但没有 attach()reattach().您无法使用对象生命周期中的一个明显步骤.

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.

从JPA类似问题的数量来看,似乎即使 JPA 确实声称有一个连贯的模型,它肯定不符合大多数程序员的心理模型,谁被诅咒浪费了很多时间试图理解如何让 JPA 做最简单的事情,并以缓存结束管理代码遍布他们的应用程序.

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.

似乎唯一的方法就是丢弃陈旧的分离实体并使用相同的 id 执行查找查询,这将命中 L2 或 DB.

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.

米克

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

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