-->
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.  [ 5 posts ] 
Author Message
 Post subject: Lookup Value
PostPosted: Fri Nov 16, 2007 12:13 pm 
Newbie

Joined: Fri Nov 16, 2007 10:45 am
Posts: 7
Hello,

I am new to NHibernate and have two simple questions.

1. What is the easiest way to get a lookup value. I have have a persistent class, Patient that maps to tbl_Patients. tbl_Patients has an FK, PatientStatus_ID. Table tbl_PatientStatus is a simple lookup table with PatientStatus_ID as PK and PatientStatus to hold the value. I just want to get the value for Patient Status to come back with the the Patient - a simple join. All the examples I see are about mapping collections and whatnot. Is there just a simple way to map/get the lookup value? Do I have to use HQL?

2. If I just mapped PatientStatus_ID as a property of the Patient class, why do I get no results back?[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 16, 2007 1:55 pm 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
1. You can use a <many-to-one> in your Patient mapping like this:
Code:
<many-to-one name="PatientStatus" column="PatientStatus_ID"/>
The .net type of this property need to be a PatientStatus class.

2. If you are mapping PatientStatus_ID as a plain old <property>, the .net type of your property needs to be an int or long depending on your db column type.

My suggestion if you go with #1 above is to map the <many-to-one> with the fetch="select" attribute in combination with second-level cache. That way, you are doing one less join and you are not fetching all the PatientStatus objects all the time.

Our application has about 80 of these lookup classes. We found that mapping the PatientStatus_ID column as a true .net enum works way better from a test-driven development perspective. We still have the likes of the PatientStatus classes mapped via NHibernate, but we only fetch it when we need the details from it. Majority of the time, we are doing something like this:
Code:
Patient p = new Patient();
p.PatientStatus = PatientStatus.IdEnum.New;
// where PatientStatus.IdEnum is a nested enum defined inside the PatientStatus class

You situation will be different; but if you don't need the properties of the PatientStatus most of the time, then only mapping its ID to the Patient class is, in my opinion, a better way to go. Hope it helps.

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 16, 2007 3:12 pm 
Newbie

Joined: Fri Nov 16, 2007 10:45 am
Posts: 7
Hi Karl,

Thank you for the help. I tried your first suggestion (partly) and it is working! I'm not quite sure what "in combination with second-level cache", so I'll go do some reading about it. Thanks again!

-- Chris


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 16, 2007 3:40 pm 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
MetaDjinn wrote:
I'm not quite sure what "in combination with second-level cache" ...


Say you have 20 different PatientStatuses rows in the database, if you specify fetch="select" and you load, say, 1000 Patients, you would end up making up to 21 database round trip: one for the list of Patients, and possibly one for each PatientStatus. If in a later (and different) session, you issue the same query again, you would make another 21 round trips to the database; 20 of which are to load the same PatientStatuses again. If you had the second-level-cache enabled, you would save yourself that 20 round trips the second time since NHibernate would look in the cache first. For something like static lookup data, the second-level-cache is one of the best ways to lighten the load on your database server.

Word of caution: if you use a cache provider that need to serialize your entities (i.e. Memcache and Prevalence), then your entities need to be Serializable. Also, the Memcache provider is a bit buggy with composite id.

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 16, 2007 3:50 pm 
Newbie

Joined: Fri Nov 16, 2007 10:45 am
Posts: 7
Ah, yes. I just finished reading the help docs and specifying the cache provider in my config file and the cache usage in my PatientStatus mapping. All working well at this point. Thanks for helping me out!

-- Chris


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