eyildiz
eyildiz

Reputation: 343

Fluent nhibernate map multiple tables

I have an UserEntity that mapped like and got Cannot simultaneously fetch multiple bags. error

public UserEntityMap : ClassMap<UserEntity>
{
   //Other properties
   HasMany(x => x.Addresses).KeyColumn("User_id").Fetch.Join();
   HasMany(x => x.Roles).KeyColumn("User_id").Fetch.Join();
}

I want to get both addresses and roles when i create a query for userentity. What should i do to see an output like

Select * from UserEntity u 
  join Addresses a on u.id=a.User_id 
  join Roles r on u.id=r.User_id where u.id=?

Upvotes: 3

Views: 928

Answers (1)

Radim K&#246;hler
Radim K&#246;hler

Reputation: 123901

There is no way how to generate such SELECT statement.

I would suggest to use batch fetching. See these for more details:

The adjusted mapping:

public UserEntityMap : ClassMap<UserEntity>
{
   //Other properties
   HasMany(x => x.Addresses)
       .KeyColumn("User_id").Fetch.Join()
       .BatchSize(100);
   HasMany(x => x.Roles)
       .KeyColumn("User_id").Fetch.Join()
       .BatchSize(100);
}

This will allow to query the root entity and with just few SELECTS get also their collections (no 1 + N issue)

Also check What is the solution for the N+1 issue in hibernate?

Upvotes: 2

Related Questions