java 如何在休眠/JPA 中禁用自动更新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12223314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to disable auto update in hibernate/JPA
提问by lutfijd
Is it possible to stop hibernate from auto updating a persistent object?
是否可以阻止休眠自动更新持久对象?
@Transactional
public ResultTO updateRecord(RequestTO requestTO) {
Entity entity = dao.getEntityById(requestTO.getId());
// now update the entity based on the data in the requestTO
ValidationResult validationResult = runValidation(entity);
if(validationResult.hasErrors()) {
// return ResultTO with validation errors
} else {
dao.persist(entity);
}
}
Here is what happens in the code, I retrieve the entity which would be considered by hibernate to be in persistent state, then I update some of the fields in the entity, then pass the entity to validation. if validation fails, then don't udpate, if validation succeeds then persist the entity.
这是代码中发生的事情,我检索了 hibernate 认为处于持久状态的实体,然后更新实体中的一些字段,然后将实体传递给验证。如果验证失败,则不要更新,如果验证成功则持久化实体。
Here is the main issue with this flow: because I updated the entity for it to be used in the validation, it does not matter whether I call persist() method (on the DAO) or not, the record will always be updated because hibernate detects that the entity has been changed and flags it for update.
这是此流程的主要问题:因为我更新了实体以便在验证中使用它,所以无论我是否调用 persist() 方法(在 DAO 上)都没有关系,记录将始终更新,因为休眠检测到实体已更改并将其标记为更新。
Keep im mind I can change the way i do validation and work around the issue, so I'm not interested in workarounds. I'm interested in knowing how i would be able to disable the hibernate feature where it automatically updates persistent objects.
请记住,我可以改变验证和解决问题的方式,所以我对解决方法不感兴趣。我很想知道如何禁用休眠功能,它会自动更新持久对象。
Please keep in mind I'm using hibernates' implementation of JPA. so Hibernate specific answers dealing with hibernate specific API will not work for me.
请记住,我正在使用 Hibernates 的 JPA 实现。因此,处理特定于 Hibernate 的 API 的 Hibernate 特定答案对我不起作用。
I tried to look for hibernate configuration and see if I can set any configuration to stop this behavior but no luck.
我试图寻找休眠配置,看看我是否可以设置任何配置来阻止这种行为,但没有运气。
Thanks
谢谢
--EDIT --- I couldn't find a solution to this, so I opted to rolling back the transaction without throwing any RuntimeException even though I'm in a declarative transaction using:
--EDIT --- 我找不到解决方案,所以我选择回滚事务而不抛出任何 RuntimeException 即使我在声明性事务中使用:
TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
which works like a charm.
这就像一个魅力。
回答by ?ukasz Rzeszotarski
Configure FlushMode for your session.
为您的会话配置 FlushMode。
http://docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/FlushMode.html
http://docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/FlushMode.html
回答by Alex Kartishev
You can use EntityManager.clear() method after getting object from database. http://docs.oracle.com/javaee/6/api/javax/persistence/EntityManager.html#clear()
从数据库中获取对象后,您可以使用 EntityManager.clear() 方法。 http://docs.oracle.com/javaee/6/api/javax/persistence/EntityManager.html#clear()
回答by Bartun
You can call the following code:
您可以调用以下代码:
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
回答by calvinkrishy
Throw an exception if validation fails and have the caller handle that.
如果验证失败并让调用者处理该异常,则抛出异常。