-->
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.  [ 5 posts ] 
Author Message
 Post subject: query.executeUpdate does not flush the session
PostPosted: Fri Jan 23, 2009 6:58 pm 
Newbie

Joined: Thu Oct 25, 2007 9:39 pm
Posts: 7
I am facing issues with native sql insert statements. Persisting as objects works fine. This may be because Hibernate does not flush the session when it is absolutely necessary. It surfaces only when we mix native queries and objects.


Consider 2 classes:

public class A {
@Id @GeneratedValue
private Integer id;

@MantToOne
private B b;
}

public class B {
@Id @GeneratedValue
private Integer id;
}

The following code persists an instance of A and B within same transaction
B b = new B();
getHibernateTemplate().save(b); // Persist as object

A a = new A();
a.setB(b);

Working
-----------
getHibernateTemplate.save(a); // Persist as object

Not working:
---------------
SQLQuery query = getSession().createSQLQuery("insert into table_a (column_b) values(?)");
query.setInteger(b.getId());
query.executeUpdate(); // Persist using native SQL query

It fails with foreign key violation constraint claiming that no record with id = xxx exists in table_b, even though i issued an insert of object 'b'. I can get this working by doing an explicit flush:

getSession().flush();
query.executeUpdate();

This happens only in Postgres and it appears like it has to do with the id generation mechanism. In SQLServer, Hibernate has to do an insert to get the id. In case of Postgres it uses sequence and so its not necessary for the insert to go through. Is this a known caveat and we have to get around it using explicit flush? Hibernate takes care of persisting it correctly if I use:

getHibernateTemplate().save(a);


Last edited by rkannan_82 on Sun Jan 25, 2009 5:42 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 23, 2009 7:46 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
A call to Session.save() doesn't mean that Hibernate issues an insert immediately. In general this only happens at when you call flush() or commit the transaction. It may also happen before a Query/Criteria is executed but that depends on which FlushMode you are using.

As you have discovered by yourself there is one exception. The insert happens immediately if the ID generation depends on inserting the object into the database. So what you are describing is expected and is also explained in the Hibernate documentation: http://www.hibernate.org/hib_docs/v3/re ... shing.html


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 23, 2009 10:36 pm 
Newbie

Joined: Thu Oct 25, 2007 9:39 pm
Posts: 7
But it does state that the order in which the SQL statements are executed is guaranteed. Correct me if I am wrong. So shouldn't it insert object 'b' before inserting object 'a'?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 24, 2009 6:37 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
From the document referenced above:

Quote:
all entity insertions, in the same order the corresponding objects were saved using Session.save()


In the case that doesn't work you are using a native SQL query, not Session.save().

One might argue that FlushMode.AUTO should detect that an automatic flush is needed but the documentation about when an automatic flush happens is quite vague and only mentions Query.list(). In any case, the only way to make the 'b' object to be inserted is to flush the session if that is an automatic or manual flush doesn't matter. It should not cause any harm to issue a manual flush.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 24, 2009 9:39 pm 
Newbie

Joined: Thu Oct 25, 2007 9:39 pm
Posts: 7
Thanks. I will file a feature request. It will be good to have this flush taken care automatically.


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