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]