FluentNHibernate - ReferencesAny(), How to use QueryOver with filter?

194 views
Skip to first unread message

Cristian Eriomenco

unread,
Feb 21, 2014, 12:39:04 PM2/21/14
to fluent-n...@googlegroups.com

have an UserAccount class, which may belong to an Employee or a Client

I don't know how to QueryOver a object OwnerRefObject field, with a filter.

For Example:

public class UserAccount
{
   public virtual int Id {get;set;}
   public virtual string UserName {get;set;}
   public virtual string Password {get;set;}
   public virtual object OwnerRefObject {get;set;}
}

public class UserMap:<User>
{
   public UserMap()
   {
      Id(x => x.Id).GeneratedBy.Indentity();
      Map(x => x.UserName);
      Map(x => x.Password);
      ReferencesAny(x => x.OwnerRefObject)
        .IdentityType<int>()
        .EntityTypeColumn("OwnerObject_Type")
        .EntityIdentifierColumn("OwnerObject_Id")
        .AddMetaValue<Client>(typeof(Client).Name)
        .AddMetaValue<Employee>(typeof(Employee).Name);
   }
}

Inside service:

public UserAccount GetClientUserAccountByClientId(int clientId)
{
   var result = _userAccountRepository
       .QueryOver()
       .Where(x => x.OwnerRefObject is Client)
        // Here I want something like (x => x.OwnerRefObject.Id==clientId)
       .Future()
       .FirstOrDefault();

   return result;
}

Chris Bingham

unread,
Feb 22, 2014, 3:58:42 PM2/22/14
to fluent-n...@googlegroups.com

Use an interface rather than object?

--
You received this message because you are subscribed to the Google Groups "Fluent NHibernate" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fluent-nhibern...@googlegroups.com.
To post to this group, send email to fluent-n...@googlegroups.com.
Visit this group at http://groups.google.com/group/fluent-nhibernate.
For more options, visit https://groups.google.com/groups/opt_out.

Cristian Eriomenco

unread,
Feb 23, 2014, 4:48:13 AM2/23/14
to fluent-n...@googlegroups.com
Good idea I'll try it. Thanks

Cu stimă,                                                                                        Eriomenco Cristian
------------------


--
You received this message because you are subscribed to a topic in the Google Groups "Fluent NHibernate" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/fluent-nhibernate/K74kY4f2m4o/unsubscribe.
To unsubscribe from this group and all its topics, send an email to fluent-nhibern...@googlegroups.com.

Chris Bingham

unread,
Feb 23, 2014, 3:36:56 PM2/23/14
to fluent-n...@googlegroups.com
alternatively, if Client holds a reference to UserAcount then a simpler solution would be to query through the Client?

(using linq, sorry i tend to avoid QueryOver)
session.Query<Client>().Where(c => c.Id == {idToFind}).Select(c => c.UserAccount)

Cristian Eriomenco

unread,
Feb 24, 2014, 5:36:59 AM2/24/14
to fluent-n...@googlegroups.com
Yes, it's simpler when you want to answer the question : "What account has <this> client?" but actually i'm trying to answer the question: "Who is the owner of <this> user account" . However I've decided to change the mapping and refuse to "ReferencesAny" because as the nhforge.org says it has many limitations. Instead i've created intermediate tables such as "UserAccountToEmployee" and "UserAccountToClient", mapping at ClientMap, and EmployeeMap that they "HasOne(x => x.UserAccount)", and mapping at these intermediate tables that they "References(x => x.Employee/Client)". Thank  you for trying to help me. If you see something wrong with this approach please share :) Thank you!!


Cu stimă,                                                                                        Eriomenco Cristian
------------------


Reply all
Reply to author
Forward
0 new messages