-->
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.  [ 1 post ] 
Author Message
 Post subject: I have many, but I only want one!
PostPosted: Thu Apr 08, 2010 6:23 pm 
Newbie

Joined: Tue Mar 23, 2010 10:41 pm
Posts: 9
Hello,

I have a User which can have many Emails. This is mapped through a List<Email> collection (exposed by IEnumerable<Email> Emails on the User).
For each User one of the Emails will be the Primary one ("Boolean IsPrimary" property on Email).

How can I get the primary Email from User without NHibernate loads every email for the User ?

I have the following two entities, with a corresponding table for each

Code:
   public class User
   {
      public virtual int Id { get; set; }
      public virtual IEnumerable<Email> Emails { get; set; }
      // public virtual Email PrimaryEmail { get; set; } - Possible somehow ?
   }
   public class Email
   {
      public virtual int Id { get; set; }
      public virtual String Address { get; set; }
      public virtual Boolean IsPrimary { get; set; }
      public virtual User User { get; set; }
   }


Can I map a "Email PrimaryEmail" property etc. on the User to the Email which have "IsPrimary=1" set somehow ?
Maybe using a Sql Formula ? a View ? a One-To-One relationship ? or another way ?

It should be possible to change the primary email to be one of the other emails, so i would like to keep them all in 1 table and just change the IsPrimary property.

Using a Sql Formula, is it be possible to keep the "PrimaryEmail" property on the User up-to-date, if I set the IsPrimary property on the current primary email to false, and then afterwards set the PrimaryEmail property to the email which should be the new primary email and set IsPrimary to true ? Will NHibernate track changes on the "old/current" primary Email loaded by the Sql Formula ?
What about the 1 level cache and the 2 level cache when using SqlFormula ?

I dont know if it could work by using a View ? Then i guess the Email could be mapped like a Component ? Will it work when updating the Email data when loaded from the View ?

Is there a better way ?

As I have a bi-directional relationship between User and Email I could in many cases of course query the primary Email and then use the "User" property on the Email to get the User (instead of the other way around - going from User to the primary Email)

In this case I could just enumerate all the Emails and find the one which have IsPrimary set true every time i need the primary email and dont care about thh extra load of querying all the emails (even though i will amost always only want the primary email), but I will have other situations like this one, with album/pictures instead of user/emails where i definatly dont want to load all of the pictures when i just need one specific one.


Hope someone can help ?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.