-->
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: Order of statements
PostPosted: Thu Dec 22, 2005 4:21 am 
Newbie

Joined: Thu Dec 22, 2005 4:09 am
Posts: 8
I see strange behavior of Hibernate, if this is documented somewhere, please give me more or less precise reference on documentation item.

Order of SQL statements is not same as order of invocations. First I remove all User's, then I add new one. But I see insert first and delete only after insert. Why? What if I will delete bean and insert bean with same primary key? I will have exception.



Hibernate version: 3.1

Code between sessionFactory.openSession() and session.close():

Transaction transaction = session.beginTransaction();
try {
Query query = session.createQuery("select u from User as u");
for (Iterator I = query.list().iterator(); I.hasNext();) {
User user = (User) I.next();
System.out.println(user);
session.delete(user);
}
//
User user = new User();
user.setName("New user " + System.currentTimeMillis());
user.setPassword("secret");
session.save(user);
} finally {
transaction.commit();
}


Full stack trace of any exception that occurs:
Hibernate: /* select u from User as u */ select user0_.id as id0_, user0_.name as name0_, user0_.password as password0_ from users user0_
ru.nlmk.test.User@f8395f[m_id=402886a408517e390108517e416b0001,m_name=New user 1135238594903,m_password=secret]
Hibernate: /* insert ru.nlmk.test.User */ insert into users (name, password, id) values (?, ?, ?)
Hibernate: /* delete ru.nlmk.test.User */ delete from users where id=?


Name and version of the database you are using: Oracle 9.2


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 22, 2005 5:27 am 
Senior
Senior

Joined: Tue May 10, 2005 9:00 am
Posts: 125
Yes, it's written in documentation.
Page 99 of hibernate 3.0.2 documentation (probably in 3.0.1 too):
Quote:
10.10 Flushing session
...
Flush occurs by default at the following points:
    before some query execution
    from org.hibernate.Transaction.commit();
    from session.flush();
The SQL statements are issued in the following order:
    all entity insertions, in the same order the corresponding objects were saved using Session.save()
    all entity updates
    all entity deletions


In general i won't recommend the reuse of a primary key inside a session (delete+insert), as an update is more appropriate.

however if you really need to do it, then issue a Session.flush() between the delete and the insert.


damn, coffee is cold[/i]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 22, 2005 5:38 am 
Newbie

Joined: Thu Dec 22, 2005 4:09 am
Posts: 8
tchize wrote:
Yes, it's written in documentation.
Page 99 of hibernate 3.0.2 documentation (probably in 3.0.1 too):


Thank you, session.flush() helped, this is page 126 in 3.1 documentation.

I just test how to work with Hibernate, so write useless code, like delete/re-insert. This is good that I found such problem during testing and not in real project. :-)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 22, 2005 6:05 am 
Senior
Senior

Joined: Tue May 10, 2005 9:00 am
Posts: 125
Sorry i read hibernate 3.0.1 as your version instead of 3.1

Anyway, a good project with unittesting would discover this quite easily ;)
but yes you would have to redo a bit of design :p


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.