-->
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: Query throws "object references unsaved transient insta
PostPosted: Sat Dec 16, 2006 4:45 pm 
Newbie

Joined: Sat Nov 25, 2006 2:56 pm
Posts: 11
I have this code:

Code:
Query query = session.createQuery("from Choice where text = :choiceText");
query.setString("choiceText", choice.getText());
Choice dbChoice = (Choice) query.uniqueResult();


And I get this exception:
Quote:
Exception in thread "main" org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: aftectest.Choice


Has anyone else seen this? This makes absolutely no sense to me - the exception seems to be saying that the Query object itself is referencing an unsaved transient instance...which doesn't make any sense because how it retrieve a unsaved object by doing a query against the database?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 16, 2006 4:47 pm 
Newbie

Joined: Sat Nov 25, 2006 2:56 pm
Posts: 11
P.S. It doesn't matter whether I do query.uniqueResult() or query.list(), I get the same exception either way.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 16, 2006 5:15 pm 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
You should consider to do what is indicated in the input form:

post mapping, classes, the source code calling the class. In addition a full stacktrace does help to indicate the root of the problem.

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 18, 2006 5:38 am 
Newbie

Joined: Sat Nov 25, 2006 2:56 pm
Posts: 11
It appears that some other unrelated database stuff is being done when I do the query call, and that other stuff is causing the exception.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 18, 2006 6:22 am 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
If the object you are retrieving hasnt been committed yet (in the same tx but not yet saved) or if it references a collection thats not saved it, the exception that you indicate below might appear. If you have any other unsaved objects in the same Transaction, consider saving those before calling the Query.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 18, 2006 6:27 am 
Newbie

Joined: Sat Nov 25, 2006 2:56 pm
Posts: 11
I'm completely familiar with saving objects before adding them to certain collections.

At the moment, I'm thinking this error happens because I was trying to add one object to two different collections, which isn't allowed with a one-to-many collection mapping. I know the error message doesn't make sense, but that's the only thing I can come up with.

Now I'm trying to do a many-to-many mapping, but unlike the examples which work perfectly, my mapping end up with a "must have same number of columns" error. *sigh* But that's a different problem...


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.