-->
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.  [ 3 posts ] 
Author Message
 Post subject: Order of database access operations performed by Hibernate
PostPosted: Wed Apr 13, 2011 7:49 am 
Newbie

Joined: Wed Apr 13, 2011 7:18 am
Posts: 4
Hello,

as far as I know, when using Hibernate, data base accesses (SQL queries) are executed when arriving at the end of the Transaction code (in the Java source code). This corresponds to what I have observed when debugging.

Currently, in the Java code, I am deleting something from the database, then adding another object (actually to replace the former by the latter).
In the code, the delete operation is of course before the add one. However, that seems not to be the order of accesses in the database ==> the "former" object has not yet been deleted when the "latter" is attempted to be added to the DB. This results in an error b/c of an existing unique key constraint (the "latter" cannot be inserted since the "former", which at that time should no more be there, has its key).

To fix this, is there any way to influence the order of database access operations carried out by Hibernate? I.e. to ensure it deletes first, and inserts afterwards?
Or does the issue have another origin?


Top
 Profile  
 
 Post subject: Re: Order of database access operations performed by Hibernate
PostPosted: Wed Apr 13, 2011 8:50 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The order of SQL statements is documented here: http://docs.jboss.org/hibernate/core/3. ... e-flushing

I don't know about any way to change the order. Maybe you can put in clever calls to session.flush() after you have deleted the existing entity, but before the new entity is saved?


Top
 Profile  
 
 Post subject: Re: Order of database access operations performed by Hibernate
PostPosted: Wed Apr 13, 2011 9:50 am 
Newbie

Joined: Wed Apr 13, 2011 7:18 am
Posts: 4
It was indeed the call to flush() - as also proposed in the German forum - right after the delete operation that resolved this issue.
(A call to remove(objectToBeRemoved) has to precede the flush()).


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.