0

here's the table schema

UserLogin 1---* UserMessages 1---* UserMessagesReceiver

mappings

public class UserLoginMap : ClassMap<UserLogin>
{
    public UserLoginMap()
    {
        Table("UserLogin");
        Id(x => x.Id).GeneratedBy.Assigned();

        HasMany(x => x.UserMessages).KeyColumn("UserLogin_id").LazyLoad().Cascade.DeleteOrphan().Inverse();
        HasMany(x => x.UserMessagesReceivers).KeyColumn("UserLogin_id").LazyLoad().Cascade.DeleteOrphan().Inverse();
    }
}
public class UserMessageMap : ClassMap<UserMessage>
{
    public UserMessageMap()
    {
        Table("UserMessages");
        Id(x => x.Id).Column("Id");

        References(e => e.UserLogin, "UserLogin_id").Not.LazyLoad();
        HasMany(x => x.UserMessagesReceivers).KeyColumn("UserMessages_id").LazyLoad().Cascade.DeleteOrphan().Inverse();
    }
}
public class UserMessagesReceiverMap : ClassMap<UserMessagesReceiver>
{
    public UserMessagesReceiverMap()
    {
        Table("UserMessagesReceiver");
        Id(x => x.Id).GeneratedBy.Assigned();

        References(e => e.UserLogin, "UserLogin_id").Not.LazyLoad();
        References(e => e.UserMessage, "UserMessages_id").Not.LazyLoad();
    }
}

then I want to execute that code:

var result = session.QueryOver<UserMessage>()
                    .Where(x => x.UserLogin.Id == 1 //he's the message sender
                                ||
                                x.UserMessagesReceivers.Count(y => y.UserLogin.Id == 1) > 0) //he's the one of the message receivers
                    .List();

and I get the error message:

to the variable "x" type "myProject.UserMessage" there is a reference in the field "," but it is not defined

what's wrong ?

Tony
  • 12,405
  • 36
  • 126
  • 226

1 Answers1

1

You can't do the kinds of queries you are asking to do in QueryOver. If you want to do something like that you would be using the NHibernate Linq provider instead:

var result = session.Query<UserMessage>()
                    .Where(x => x.UserLogin.Id == 1 ||
                                x.UserMessagesReceivers.Count(y => y.UserLogin.Id == 1) > 0) //he's the one of the message receivers
                    .List();

.Query() is the key above

Cole W
  • 15,123
  • 6
  • 51
  • 85
  • it works, but what if I need to get the distinct objects ? The Query<> will load all elements that match the criteria and then do the distint operation. The better approach would be, if the distinct would be placed inside the generated sql query – Tony Feb 19 '15 at 15:33