NHibernate does extra select on each missing relations, even using an eager fetching.
Description
Environment
Attachments
duplicates
Activity
As per @Ricardo Peres
This appears to be similar to NH-1001.
Did you have any advice how to prevent this behaviour without changing the data in the database?
Ah, thanks I misunderstood. I've updated the test, and can now reproduce the issue you described (I will attach it later). Here is the relevant part of the log that occurs after the query is executed:
10:15:54,716 DEBUG TwoPhaseLoad:0 - resolving associations for [NHibernate.Test.NHSpecificTest.NH2784.Order#3]
10:15:54,717 DEBUG DefaultLoadEventListener:0 - loading entity: [NHibernate.Test.NHSpecificTest.NH2784.User#3]
10:15:54,718 DEBUG DefaultLoadEventListener:0 - creating new proxy for entity
10:15:54,808 DEBUG TwoPhaseLoad:0 - adding entity to second-level cache: [NHibernate.Test.NHSpecificTest.NH2784.Order#3]
10:15:54,809 DEBUG NonstrictReadWriteCache:0 - Caching: NHibernate.Test.NHSpecificTest.NH2784.Order#3
10:15:54,811 DEBUG TwoPhaseLoad:0 - done materializing entity [NHibernate.Test.NHSpecificTest.NH2784.Order#3]
10:15:54,814 DEBUG StatefulPersistenceContext:0 - initializing non-lazy collections
10:15:54,841 DEBUG SessionImpl:0 - initializing proxy: [NHibernate.Test.NHSpecificTest.NH2784.User#3]
10:15:54,852 DEBUG DefaultLoadEventListener:0 - attempting to resolve: [NHibernate.Test.NHSpecificTest.NH2784.User#3]
10:15:54,856 DEBUG NonstrictReadWriteCache:0 - Cache lookup: NHibernate.Test.NHSpecificTest.NH2784.User#3
10:15:54,858 DEBUG NonstrictReadWriteCache:0 - Cache miss
10:15:54,859 DEBUG DefaultLoadEventListener:0 - Entity cache miss: NHibernate.Test.NHSpecificTest.NH2784.User#3
10:15:54,860 DEBUG DefaultLoadEventListener:0 - object not resolved in any cache: [NHibernate.Test.NHSpecificTest.NH2784.User#3]
10:15:54,863 DEBUG AbstractEntityPersister:0 - Fetching entity: [NHibernate.Test.NHSpecificTest.NH2784.User#3]
10:15:54,870 DEBUG Loader:0 - loading entity: [NHibernate.Test.NHSpecificTest.NH2784.User#3]
10:15:54,898 DEBUG Int32Type:0 - binding '3' to parameter: 0
10:15:54,900 INFO Loader:0 - SELECT user0_.Id as Id1_0_, user0_.Name as Name1_0_ FROM [User] user0_ WHERE user0_.Id=@p0
10:15:54,902 DEBUG SQL:0 -
SELECT
user0_.Id as Id1_0_,
user0_.Name as Name1_0_
FROM
[User] user0_
WHERE
user0_.Id=@p0;
@p0 = 3 [Type: Int32 (0)]
When I do an eager join on a relation table, whenever the relation is missing it will generate an additional query for each missing relation.
Table layout / data:
TableName: Order
-------------------
Id
OrderName
UserId
-------------------
1
OrderA
1
2
OrderB
2
3
OrderC
3
-------------------------
TableName: User
-----------+
Id
UserName
-----------+
1
User1
2
User2
----------------
var orders = session.CreateCriteria<Order>().SetFetchMode("User",
FetchMode.Eager).List<Order>();
Console output:
NHibernate: SELECT this_.Id as Id0_1_, this_.OrderName as OrderName0_1_, this_.User_id as User3_0_1_, user2_.Id as Id1_0_, user2_.UserName as UserName1_0_ FROM[Order] this_ left outer join [User] user2_ on this_.User_id=user2_.Id
NHibernate: SELECT user0_.Id as Id1_0_, user0_.UserName as UserName1_0_ FROM [User] user0_ WHERE user0_.Id=@p0;@p0 = 3 [Type: Int32 (0)]
As you can see it tries to get UserID 3 (which doesn't exists) but it should already know it isn't there as I did a eager fetchmode on that table.