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.  [ 7 posts ] 
Author Message
 Post subject: How to Load Transient Object w/ Query?
PostPosted: Thu Nov 03, 2005 4:06 pm 
I'd like to be able to Load a transient object by using a query (rather than simply an id). Obviously, I'd need to limit the query to one result, but I can't find an elegant way to do this...

My current, ugly, work around is to perform a Find, get the id from the first/only index of the IList, Evict that object/id from the session, then call Load with the ID in order to get the data into the transient object.

Surely there's a better way. Your help is much appreciated.


Top
  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 5:53 pm 
Newbie

Joined: Thu Oct 20, 2005 5:17 pm
Posts: 19
Are you looking for the .UniqueResult() method?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 6:02 pm 
Jeff Bigler wrote:
Are you looking for the .UniqueResult() method?


Well, not really. That's not the hard part. I'm sure I'm not describing it very well.

There's an overload for Load that accepts a transient object and an id. What I need is an overload that accepts a transient object and a query string... or something that accomplishes that concept.

Basically, it's like Load in the sense it can load a transient object, but like Find in the sense that you can pass it a query.


Top
  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 6:05 pm 
coltrane wrote:
Jeff Bigler wrote:
Are you looking for the .UniqueResult() method?


Is that what .UniqueResult does? I thought I had used it before, but realized after my last post that I should have made sure I understood what you were telling me. :D


Top
  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 6:53 pm 
Newbie

Joined: Thu Oct 20, 2005 5:17 pm
Posts: 19
UniqueResult returns a single object, so it can do what you want. However, your query has to return a single row for it to work. It sounds like yours returns multiple rows, so it would throw an exception. You could use an "order by" clause to make sure the first row is the one you want and then use the SetMaxResults method to only return the first result.

Code:
MyObject myObject = session.CreateQuery( "from MyObject myObject order by Id desc" )
   .SetMaxResults( 1 )
   .UniqueResult() as MyObject;


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 9:46 pm 
Jeff Bigler wrote:
UniqueResult returns a single object, so it can do what you want. However, your query has to return a single row for it to work. It sounds like yours returns multiple rows, so it would throw an exception. You could use an "order by" clause to make sure the first row is the one you want and then use the SetMaxResults method to only return the first result.


Unfortunately, that's not the issue I'm trying to solve. It's the loading into an instance of an object that I'm looking for. That is, I don't want session to to act as the object factory, I want it to Load an instance of an object I've already created. There's an overload of the Load method that permits this for a given ID:

Code:
session.Load(myObject, myID);


However, I want to load it using a query rather than an identifier:

Code:
session.Load(myObject, "from MyObject where id='123'");


Let's assume, for the moment, the resultset is unique... and ignore, for the moment, that it's a stupid example in the first place... but the concept holds for more complex hcl as well.

What I've done previously is similar to your code at first, but then:
Code:
string id = myObject.ID;
session.Evict(myObject, id);
session.Load(myNewObject, id);


Which, admittedly is an abomination.

The reason I'm doing all this is rather complex, but is because my objects are marshalbyref and therefore anchored on an application server and called via remoting. Each object has it's own factory method which calls CRUD methods within the object itself to load and persist its values to/from the database. Rather than have a Fetch/Read method only fetch by an object's ID, I want to be able to fetch by an ad hoc query... enter, the problem with loading values into an already created object.[/code]


Top
  
 
 Post subject:
PostPosted: Wed Nov 09, 2005 5:30 pm 
Newbie

Joined: Thu Oct 20, 2005 5:17 pm
Posts: 19
Ah, now I understand. Unfortunately, I don't know of any way to do it other than the way you've described. Load is the only method I know of that supports using an "empty" instance of a persistent class.


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