-->
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.  [ 11 posts ] 
Author Message
 Post subject: Many-to-one relation with default item
PostPosted: Wed Jan 28, 2009 6:19 am 
Newbie

Joined: Fri Jun 20, 2008 8:24 am
Posts: 7
Hello
My question is about many-to-one or one-to-one mapping.
I have a Profile and BillingInfo classes.

Code:
  <class name="Profile" table="Profile">       
      <id name="Id">
         <column name="Id" not-null="true" />
         <generator class="native" />
      </id>
      <set name="BillingInfos" cascade="delete" inverse="true" lazy="true">
         <key column="ProfileId" on-delete="cascade" />
         <one-to-many class="BillingInfo" />
      </set>
   </class>

Code:
  <class name="BillingInfo" table="BillingInfo">     
      <id name="Id">
         <column name="Id" not-null="true" />
         <generator class="native" />
      </id>
      <property name="Primary" column="[Primary]" not-null="true" />
      <many-to-one name="Profile" class="Profile" column="ProfileId" not-null="true" cascade="none" update="false" fetch="select"/>
   </class>


One Profile can have many BillingInfo, but only one of them is the default BillingInfo. The first point (simple one-to-many) is easy to implement.
The question is how to implement the second point, i.e. Profile can have only one BillingInfo.

Thank you in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 28, 2009 8:42 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You can use an additional one-to-one association for the default. As an alternative, if you have a field on BillingInfo that indicates the default, you can just define a property and look up the default entry in the BiliingsInfo bag.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 28, 2009 9:07 am 
Newbie

Joined: Fri Jun 20, 2008 8:24 am
Posts: 7
Thank you for fast response.
Could you post an example, please, of how to implement it via one-to-one? I've already tried it but it wasn't successful (maybe I've done something wrong).


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 28, 2009 9:50 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Try:

<one-to-one name="DefaultBillingInfo" property-ref="Profile"/>

But have in mind that the DefaultBillingInfo will also be an item in the bag. So probably it will be easier if you put an indicator on BillingInfo or use index 0 as default.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 28, 2009 10:25 am 
Newbie

Joined: Fri Jun 20, 2008 8:24 am
Posts: 7
I've already tried it, but if there are more then one entities having the same ProfileId (i.e. if there are more then one items corresponding the current profile) NHibernate throws exception saying that there are several entities with the same ProfileId, so, I guess, we cannot map DefaultBillingInfo property in this way without some additional actions (unfortunately, I don't know them).

For now I've solved this issue via implementing it in the following way(not the best solution, but it works):
Code:
      <set name="DefaultBillingInfo" where="[Primary] = 'true'">
         <key column="ProfileId"/>
         <one-to-many class="BillingInfo" />
      </set>
and taking the first item from that set.

I would appreciate a lot if someone show more elegant solution.

Thank you for help.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 28, 2009 10:38 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
(headbang) ... sorry, my fault ... wasn't thinking thouroughly. Is primary also mapped to your class ? If that's the case, I would use that in the class to get the default item from the set instead of mapping the default.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 28, 2009 1:31 pm 
Newbie

Joined: Fri Jun 20, 2008 8:24 am
Posts: 7
Primary is a property of BillingInfo class and maps the [Primary] column in corresponding table (see mappings in my first post).
As for your last post, I'm sorry but I don't quite see what you mean. Did you mean to iterate through BillingInfos set to find item with Primary set to True?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 28, 2009 5:27 pm 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Yes ... eihter that or always use index 0 for the default.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 29, 2009 7:28 am 
Newbie

Joined: Fri Jun 20, 2008 8:24 am
Posts: 7
I've implemented DefaultBillingInfo as shown below:
Code:
      <bag name="DefaultBillingInfo" access="field.camelcase" where="[Primary] = 'true'">
         <key column="ProfileId"/>
         <one-to-many class="BillingInfo" />
      </bag>

Code:
      public MemberUserBillingInfo DefaultBillingInfo
      {
         get
         {
            return defaultBillingInfo[0];
         }
         set
         {
            defaultBillingInfo[0] = value;
         }
      }


What I really want is something like that:
http://www.onjava.com/pub/a/onjava/2005 ... tml?page=4

I've tried to implement my DefaultBillingInfo in that way but had no success. Is it possible to do with NHibernate? And if it is I wonder how it may look like.

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 29, 2009 8:20 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
I've never seen this syntac x before. It's probably not yet ported.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 29, 2009 9:29 am 
Newbie

Joined: Fri Jun 20, 2008 8:24 am
Posts: 7
OK. Thank you for help:)


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