简体   繁体   English

视图合并中的Java Hibernate会话会丢失更改吗?

[英]Java Hibernate Session in View Merge loses Changes?

i have a Hibernate APP we are trying to implement Open Session in View Pattern with ZK i have a question Hibernate creates a session per thread and closes it but we have severals operations with [criteria and save or update] methods in one session we started to implement using update method we very quickly received the infamous 我有一个Hibernate APP,我们正在尝试使用ZK Open Session in View Pattern实现Open Session in View Pattern我有一个问题,Hibernate为每个线程创建一个会话并关闭它,但是在我们开始使用的一个会话中,我们有几个使用[criteria and save or update]方法的操作。使用更新方法实现我们很快就收到了臭名昭著的通知

NONUNIQUEOBJECTEXCEPTION 

we have changed update() to merge() my question if the following. 如果发生以下情况,我们已将update()更改为merge()我的问题。

if i have a Student class and trying to update the the student using update[making sure is not in session avoid the exception] but later in other method the student is required to updated again differently from the previous update this time we should merging and merge() method is called. 如果我有一个Student班级,并尝试使用update[making sure is not in session avoid the exception]来更新该学生,但是以后在另一种方法中,该学生需要再次不同于先前的update这次我们应该mergingmerge()方法被调用。 i was wondering when [flush][commit] method is called the changes to the first student is loss? 我想知道什么时候[flush][commit]方法被称为第一个学生的更改丢失? the changes to the both students is OK? 对两个学生的更改都可以吗? what happens?? 怎么了??

i am afraid the changes in the first student is loss! 恐怕第一个student的变化是损失! what can i do in this case? 在这种情况下我该怎么办?

thanks a lot a best regards. 非常感谢。

you should take a look at locking algorithm. 您应该看看锁定算法。 There are 2 popular ones: optimistic and pessimistic. 流行的有2种:乐观和悲观。 For web application optimistic locking usually works better. 对于Web应用程序,乐观锁定通常效果更好。 Long story short each revision of entity object is given a version label (often a bigint or string db column). 长话短说,实体对象的每个修订版都有一个版本标签(通常是bigint或字符串db列)。 When someone pre-empt your update and your version became stale, hibernate will throw optimistic lock exception, in which case you can capture and ask the user what to do (or take other appropriate measure) 当有人抢占您的更新并且您的版本过时时,休眠将抛出乐观锁异常,在这种情况下,您可以捕获并询问用户该怎么做(或采取其他适当措施)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM