Hibernate version:
NHibernate 1.2.1GA
I have a one-to-one relation between two tables/classes. The important parts of the mapping files are the following:
class1.hbm.xml
Code:
<one-to-one name="Two" class="Class2" property-ref="Class1Id" fetch="join" />
class2.hbm.xml
(here I have to take a many-to-one because the culumn for the relation is not the primary key)
Code:
<many-to-one name="One" class="Class1" column="CLASS1_ID" fetch="join" />
The code to fetch a list of Class1 objects:
Code:
return session.CreateCriteria(typeof(Class1))
.SetFetchMode("Two", FetchMode.Join)
.Add(Expression.Eq("Enabled", 1))
.AddOrder(Order.Asc("Name"))
.CreateCriteria("Two").Add(Expression.Gt("Enabled", 1))
.List<Class1>();
The result list is the expected, but the type of fetching is strange:
The first select NHibernate creates fetches all data which is required (from both tables with a join!). I thought that NHibernate would stop here and all be fine! The strange thing is that for every line in the resulted list a additional select to fetch the data from the second table (class2) is fired. I don't understand why this happens because the required data is complete after the first select.
Has anybody an idea how to solve this problem so that only one select runs?
Thank you!! [/code]