-->
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: Mapping chosen collection item as its own property
PostPosted: Mon Dec 19, 2005 6:24 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
Most of our collections contain entities with a boolean IsPrimary property. Very frequently we will have queries that want to select a parent entity and its primary child entity. We would like to redundantly expose the "Primary" child entity directly on the parent, to make both queries and code simpler.

However, I can't figure out how to map this situation. I tried setting this up as a one-to-one, but only one-to-many has a "where" attribute to filter down to the primary collection item.

Is this possible with NHibernate?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 19, 2005 9:20 pm 
Regular
Regular

Joined: Mon May 16, 2005 1:35 am
Posts: 67
I can't think of how to achieve this with a mapping. However, there are two approaches I can think of to solve the problem:

1. Use a getter property to get the "primary" child entity by iterating through the child collection, looking for the item with the IsPrimary property = true. In this case, everything is handled by your domain logic.

2. Add a foreign key on your parent entity table to the child entity table, pointing to the primary child for any given parent entity record. This works quite nicely as you can just set your "PrimaryChild" property to the proper Child instance and NHibernate will persist to the database appropriately.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 20, 2005 11:08 am 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
#1 won't be very useful because you can't use it in queries (since NHibernate doesn't know about the property). It also forces the (otherwise lazy-loaded) child collection to load.

I'd like to do #2, that would get rid of the problems of clearing the IsPrimary indicator on the old child when a new one becomes primary (and also preventing multiples from being marked primary). Unfortunately, we got into NHibernate after our base schema was virtually set in stone, and I doubt my bosses will go for changing the way we track primary children in the database :(


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 9:54 pm 
Newbie

Joined: Sun May 30, 2004 7:38 am
Posts: 7
Did you ever resolve this problem?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 08, 2006 12:22 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
We never resolved it, although if you have the same issue, I'd suggest solution #2. We just can't do it because it would require reworking the way we track primary children on dozens of tables in our application, and management will never go for it ...


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.