I posted this on the other forum, and was asked to put it in JIRA, which I just tried to do, but it seems to be down right now. So I hope nobody minds if I post it here since I verified it with a quick test project. I'll try to get it to JIRA when it comes back up.
Here's the two classes involved in the test:
Code:
using System;
using System.Collections;
namespace SetFetchModeIssue
{
public class Parent {
Guid _id;
string _name;
IList _children;
public Guid Id {
get { return _id; }
set { _id = value; }
}
public string Name {
get { return _name; }
set { _name = value; }
}
public IList Children {
get { return _children; }
set { _children = value; }
}
}
}
using System;
namespace SetFetchModeIssue
{
public class Child {
Guid _id;
string _name;
Parent _parent;
public Guid Id {
get { return _id; }
set { _id = value; }
}
public string Name {
get { return _name; }
set { _name = value; }
}
public Parent Parent {
get { return _parent; }
set { _parent = value; }
}
}
}
And the mapping files for them:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="SetFetchModeIssue.Child, SetFetchModeIssue" table="Child">
<id name="Id" type="Guid">
<generator class="guid" />
</id>
<property name="Name" column="[Name]" type="String" length="50"/>
<many-to-one name="Parent" not-null="true" />
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="SetFetchModeIssue.Parent, SetFetchModeIssue" table="Parent">
<id name="Id" type="Guid">
<generator class="guid" />
</id>
<property name="Name" column="[Name]" type="String" length="50"/>
<bag name="Children" inverse="true" lazy="true">
<key column="Parent" />
<one-to-many class="SetFetchModeIssue.Child, SetFetchModeIssue" />
</bag>
</class>
</hibernate-mapping>
And here's the actual test:
Code:
using System;
using System.Collections;
using NHibernate;
using NHibernate.Cfg;
using NUnit.Framework;
namespace SetFetchModeIssue
{
[TestFixture]
public class SetFetchModeTestFixture
{
ISessionFactory sessions;
[TestFixtureSetUp]
public void RunBeforeAllTests() {
Configuration cfg = new Configuration();
cfg.AddAssembly("SetFetchModeIssue");
this.sessions = cfg.BuildSessionFactory();
}
// This test works properly.
[Test]
public void GetParent() {
using(ISession session = sessions.OpenSession()) {
IList results = session.CreateCriteria(typeof(Parent)).List();
Assert.AreEqual(1, results.Count);
Parent parent = results[0] as Parent;
Assert.IsNotNull(parent);
Assert.AreEqual(4, parent.Children.Count);
}
}
// This test works properly in 0.7. But fails in 0.8 (0.8.2 tested).
// Instead of prefetching, the .SetFetchMode() call seems to basically
// replace the ICriteria's "PersistentClass", Parent in this case,
// with the Type of the relation, Child in this case.
[Test]
public void Prefetch() {
using(ISession session = sessions.OpenSession()) {
IList results = session.CreateCriteria(typeof(Parent))
.SetFetchMode("Children", FetchMode.Eager).List();
Assert.AreEqual(1, results.Count);
Parent parent = results[0] as Parent;
Assert.IsNotNull(parent);
Assert.AreEqual(4, parent.Children.Count);
}
}
}
}
Like the test comments say, I tried it with NHibernate .7 and it worked fine, but in .8 it's broken.
This is kinda a big deal for my current project, and time is very short, so I was thinking of reverting back to .7 until this is resolved. (Even though the release notes say .8 is a breaking change, I guess I'm not using Expressions in that manner since it was a drop-in replacement for me to goto .8).
EDIT: I'm sorry, I forgot to mention, this is with the MsSql2000Dialect. I'm not sure if that might make a difference.
EDIT2: JIRA seems to be back up, so I created issue NH-273 to cover this.