This is because the SetMaxResults method works at a low level. I mean, SetMaxResults calls the SQL statement that NHibernate generates to contain the TOP or LIMIT .
Of course, this leads to those weird results when you use impatient fetching, since impatient fetchmode leads to JOIN.
So, if you have two tables that are joined together in a one-to-many relationship, the record set returned by NHibernate looks like this, for example
1 recordA-fromTable1 1 recordX-fromTable2 2 recordA-fromTable1 2 recordY-fromTable2 3 recordB-fromTable1 3 record2-fromTable2
If "TOP 2" is executed in the above record set, NHibernate will be able to extract only one entity from it, since the first 2 records in the result set are actually intended for the same object.
I resolved this using a separate subquery containing the TOP clause, and gets the identifier of the objects to be received.
source share