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.  [ 6 posts ] 
Author Message
 Post subject: IQuery.List() Tries To Insert
PostPosted: Fri Feb 01, 2008 12:15 pm 
Newbie

Joined: Mon Apr 30, 2007 6:17 pm
Posts: 14
Upon running a fairly simple select HQL query I get this error:

"Cannot insert duplicate key row in object 'dbo.PayrollBatchDrivers' with unique index 'IX_PayrollBatchDrivers_DriverID_PayrollBatchID'.
The statement has been terminated."

Here's the code snippet:

Code:
IQuery query = NHibernateSessionProvider.Session.CreateQuery(hqlQuery);
IList result = query.List();

Here's the HQL:

Code:
select
e.Id, e.TypeString, e.Amount, e.Comment
from PayrollBatchDriverExpense e
where e.Driver.Driver.Id = 20886 and e.IsActive = 1
and e.Driver.PayrollBatch.Id = 81

Now, before this HQL query I have fetched an object and made some modifications to it. Its seems that the List() method is wanting to flush the session and save those changes. Never mind that I don't see why a flush would even cause this error, I don't want a flush to happen.

Can anyone explain why calling IQuery.List() with a select would try to do any inserts? Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 01, 2008 5:39 pm 
Regular
Regular

Joined: Wed Aug 15, 2007 7:37 am
Posts: 73
I may be wrong, but I remember reading in the docs that queries will never return potentially wrong data (due to caching), so it's possible that a Flush is issued to make sure the database is consistent with Hibernate when the query's run. Just a stab in the dark though.

Steve


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 01, 2008 6:20 pm 
Newbie

Joined: Mon Apr 30, 2007 6:17 pm
Posts: 14
SteveMc,

I believe that you are exactly right. I noticed that I have a transient instance of one of a class that is referenced in the WHERE clause of my HQL. I bet that NHibernate is trying to persist the dirty data before making the query. This is logical I guess, but it's not what I want. I suppose the textbook solution would be to do the query in a new session, but I then might get some lazy exceptions.

In any case I have fixed it by plucking off the transient item, which is extra baggage for this particular task.

Tim


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 01, 2008 7:16 pm 
Regular
Regular

Joined: Wed Aug 15, 2007 7:37 am
Posts: 73
Glad that helped. It does make sense, though I can see how it would be annoying. It would be worse (probably) if somewhere in your code you'd set a value and a subsequent search didn't return it.

Have a good weekend,

Steve


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 02, 2008 1:55 am 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
Try using a different FlushMode on your session: see http://www.hibernate.org/hib_docs/nhibe ... a-flushing


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 06, 2008 4:45 pm 
Newbie

Joined: Mon Apr 30, 2007 6:17 pm
Posts: 14
marcal, I think you nailed it. Our FlushMode is Auto, and it probably should be Commit.


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