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.  [ 2 posts ] 
Author Message
 Post subject: Load via IQuery / ICriterion
PostPosted: Mon Feb 04, 2008 10:16 am 
Newbie

Joined: Mon Feb 04, 2008 9:53 am
Posts: 1
Hi,

Is it possible to load information into a reference via a query / set of criterion rather than returning an object - i.e. making a call to List()?

The deal is - I have an object with a unique index as well as a primary key (user ID & username). Now I don't have the user ID until i've loaded it, so I want to be able to load via the username - the unique index.

I don't want to have something sitting ontop of my user object, so the dataaccess is performed in the class - which is why I'd like to pass in a reference:

using(ISession session = SessionManager.NewSession())
{
ICriteria nhCriteria = session.CreateCriteria(typeof(MyUserObject));
nhCriteria.Load(this, new ICriterion[] { Expression.Eq("Name", _name) });
}

or something like that - where we can supply queries / criterion to the session / criteria, and also a reference.

Can this be done?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 09, 2008 7:03 pm 
Regular
Regular

Joined: Tue Dec 02, 2003 6:25 pm
Posts: 61
Location: Dallas, TX
NH generally needs to be able to instantiate all the entities it loads, hence the requirement for a public default constructor, so it doesn't play well with an object that wants to load its own state.

AFAIK, the best you can do is copy the state of the returned instance over into the calling instance. Then you would still call Session.Evict() on the instance that NH created and then associate the calling instance with the Session using Session.Lock().

In light of that, unless you're saddled with a bunch of code that you can't really refactor, I'd recommend switching to a Factory approach, which could integrate nicely with NH.


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