0

Let's say I have 2 classes, User and Message

public class User
{
    public virtual Guid Id { get; set; }
    public virtual string Name { get; set; }
}

public class UserMap : ClassMap<User>, IMappedEntity
{
    public UserMap()
    {
        Id(x => x.Id);
        Map(x => x.Name);
    }
}


public class Message
{
    public virtual Guid Id { get; set; }
    public virtual string Text { get; set; }
    public virtual User TheUser { get; set; }
}

public class MessageMap : ClassMap<Message>, IMappedEntity
{
    public MessageMap()
    {
        Id(x => x.Id);
        Map(x => x.Text);
        References<User>(x => x.TheUser)
    }
}

When I want to load some Messages, the referenced user has to be loaded as well. As far as I know, nHibernate usually does this by creating a new sql command for every user, causing N+1 problems.

After some research I found this solution which creates a join of the to tables:

_session.Query<Message>().Fetch(x => x.TheUser)

Unfortunately, I can not (easily) implement this, because the logic for accessing data lays inside a generic repository and is used by a lot of other classes as well.

So I would like to know whether there's any possibility to force a join inside of the mapping of my class Message.

Christopher
  • 2,005
  • 3
  • 24
  • 50
  • Why is the property of the class Message " public virtual string User { get; set; }" ...... why aren't you doing " public virtual User TheUser { get; set; } ........... Note, I use "The" prefix when proofing NHiberate to avoid ambiguity. – granadaCoder Oct 22 '13 at 20:48
  • PS. I'm "asking"...not "attacking". Maybe you have a legit reason. But it looks a little fishy to me. – granadaCoder Oct 22 '13 at 20:49
  • It's not only fishy, it's totally wrong ;) I simplified my real mapping and overlooked this. I edited the question, thanks for the hint! – Christopher Oct 22 '13 at 21:02
  • @granadaCoder: based on my correction, do you have any idea how to solve the problem? – Christopher Oct 23 '13 at 20:45
  • No, I looked at it. This one is beyond me. – granadaCoder Oct 24 '13 at 13:25

1 Answers1

0

Should be able to do something like this

References<User>(x => x.TheUser)
    .Fetch
    .Join()

or

References<User>(x => x.TheUser)
    .Fetch
    .Select()
Cole W
  • 15,123
  • 6
  • 51
  • 85