-->
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: Hibernate 2 issuing 2 queries to save new object
PostPosted: Mon Nov 21, 2005 3:16 pm 
Beginner
Beginner

Joined: Tue Aug 26, 2003 4:19 pm
Posts: 42
This is in Hibernate 2.1.7.

I have a simple class Customergroups (with a hilo indentifier).

When I simply instantiate a new object and save it, like this:

Code:
Customergroup object = new Customergroup();
sess.saveOrUpdate(object);
sess.flush();
sess.connection().commit();


this is the generated SQL which creates the entire record in one query

Code:
Hibernate: insert into customergroups (customergroupname, customergroupid) values (?, ?)



No problem so far. But for various reasons I need to get an identifier assigned at the start of the use case, so I do this:

Code:
Customergroup object = new Customergroup();
sess.saveOrUpdate(object);  //assign id
...do some work...
object.setCustomergroupname("test");
sess.saveOrUpdate(object);
sess.flush();
sess.connection().commit();


This is the generated SQL this time, inserting only the id, then updating it with the name:

Code:
Hibernate: insert into customergroups (customergroupname, customergroupid) values (?, ?)
Hibernate: update customergroups set customergroupname=? where customergroupid=?


This means that I can't have a not-null constraint on the name column in the customergroups table.

How can I get Hibernate to issue a single INSERT query even when I save the object in the session and then update it?

Any hints much appreciated


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 21, 2005 3:29 pm 
Beginner
Beginner

Joined: Tue Aug 26, 2003 4:19 pm
Posts: 42
Sorry, a mistake in my examples, the first code snippet should have read as follows:

Code:
Customergroup object = new Customergroup();
object.setCustomergroupname("test");
sess.saveOrUpdate(object);
sess.flush();
sess.connection().commit();


And just for sake of clarification, I should note that in the second example, where Hibernate is issuing two queries, both queries are issued after I call flush().


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 21, 2005 3:40 pm 
Beginner
Beginner

Joined: Tue Aug 26, 2003 4:19 pm
Posts: 42
Ugh, I forgot another detail:

If I specify not-null="true" for customergroupname in my mapping file, this code:

Code:
Customergroup object = new Customergroup();
sess.saveOrUpdate(object);  //assign id
...do some work...
object.setCustomergroupname("test");
sess.saveOrUpdate(object);
sess.flush();
sess.connection().commit();


results in this error:

Code:
Hibernate: update customergroups set customergroupname=? where customergroupid=?
Nov 21, 2005 11:36:44 AM net.sf.hibernate.impl.SessionImpl execute
SEVERE: Could not synchronize database state with session
net.sf.hibernate.HibernateException: Batch update row count wrong: 0
[/code]


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.