-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Update order
PostPosted: Tue Feb 16, 2010 4:11 am 
Newbie

Joined: Thu Sep 10, 2009 5:43 am
Posts: 15
Hello,
I have a problem with updating order. I have table "PERSON" with column "CODE". CODE has unique constraint.
Now I
Code:
Person p1 = session.load(Person.class,1); (CODE="one")
Person p2 = session.load(Person.class,2);(CODE="two")

p1.setCode("two");
p2.setCode("three");

session.update(p1);
session.update(p2);

And i get excpeption constraintViolationException because CODE="two" already exists but when I change order like
Code:
Person p1 = session.load(Person.class,1); (CODE="one")
Person p2 = session.load(Person.class,2);(CODE="two")

p1.setCode("two");
p2.setCode("three");

session.update(p2);
session.update(p1);

it dosen't help. How to resolve this problem

Best regards


Top
 Profile  
 
 Post subject: Re: Update order
PostPosted: Tue Feb 16, 2010 5:36 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
As in hibernate the order in which updates are executed is not deterministic,
you must flush explicitely in order to avoid potential constraints violations:


Code:
Person p1 = session.load(Person.class,1); (CODE="one")
Person p2 = session.load(Person.class,2);(CODE="two")


p2.setCode("three");
session.update(p2);
session.flush(); // writes the changes to the database without commiting

p1.setCode("two");
session.update(p1);  // the following commit will success



Another case is when you delete and recreate the same unique constraint key within a single transaction:

Code:
Person p1 = session.load(Person.class,1); (CODE="one")
session.delete(p1);
session.flush(); // writes the changes to the database without commiting

p2 = new Person("one")
session.persist(p2);  // the following commit will success


Top
 Profile  
 
 Post subject: Re: Update order
PostPosted: Tue Feb 16, 2010 6:23 am 
Newbie

Joined: Thu Sep 10, 2009 5:43 am
Posts: 15
Hello,
thanks for answering but flushing is not good. When i invoke flush() then all the awating quaries goes to database. In my case also pesimistick lock wich blocks persons.
I wonder why hibernate don't put object on which I invoke update at the end of queue. Now invoking update on object dosen't do nothing(dirty checking works)

Best regards


Top
 Profile  
 
 Post subject: Re: Update order
PostPosted: Tue Feb 16, 2010 6:39 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Yes flushing is not good.
Unfortunately flushing is the unique solution what Hibernate autors suggests in this case as far as I know,
if you want do such operations within one single transaction.
This and similiar issues (update insert delete order) are asked again and again in this forum.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.