Skip to content

NH-2888 - Add support to ICriteria to eager fetch lazy properties #264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/NHibernate.Test/Criteria/FetchAllProperties/Article.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Collections.Generic;

namespace NHibernate.Test.Criteria.FetchAllProperties
{
public class Article
{
public Article()
{
Comments = new HashSet<Comment>();
}

public virtual long Id { get; set; }

public virtual string Title { get; set; }

public virtual string Description { get; set; }

public virtual ISet<Comment> Comments { get; set; }

public virtual ArticleExtension ArticleExtension { get; set; }
}

public class ArticleExtension
{
public virtual long Id { get; set; }

public virtual Article Article { get; set; }

public virtual int Rating { get; set; }

public virtual string Notes { get; set; }
}

public class Comment
{
public virtual long Id { get; set; }

public virtual string Subject { get; set; }

public virtual string Text { get; set; }

public virtual Article Article { get; set; }
}
}
44 changes: 44 additions & 0 deletions src/NHibernate.Test/Criteria/FetchAllProperties/Article.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="NHibernate.Test"
namespace="NHibernate.Test.Criteria.FetchAllProperties"
default-access="property"
default-lazy="true">

<class name="Article" table="Article">
<id name="Id" type="Int64">
<generator class="native"/>
</id>
<property name="Title" />
<property name="Description" lazy="true"/>
<set name="Comments" inverse="true" cascade="all-delete-orphan">
<key column="article_id"/>
<one-to-many class="Comment"/>
</set>

<one-to-one name="ArticleExtension" class="ArticleExtension" cascade="all" lazy="false" />
</class>

<class name="ArticleExtension" table="ArticleExtension">
<id name="Id" type="Int64">
<generator class="foreign">
<param name="property">Article</param>
</generator>
</id>

<property name="Rating" />
<property name="Notes" lazy="true" />

<one-to-one name="Article" class="Article" constrained="true" />
</class>

<class name="Comment" table="Comment">
<id name="Id" type="Int64">
<generator class="native" />
</id>
<property name="Subject" />
<property name="Text" lazy="true"/>
<many-to-one name="Article" column="article_id"/>
</class>

</hibernate-mapping>
11 changes: 11 additions & 0 deletions src/NHibernate.Test/Criteria/FetchAllProperties/BlogPost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace NHibernate.Test.Criteria.FetchAllProperties
{
public class BlogPost
{
public virtual long Id { get; set; }

public virtual string Title { get; set; }

public virtual string Description { get; set; }
}
}
16 changes: 16 additions & 0 deletions src/NHibernate.Test/Criteria/FetchAllProperties/BlogPost.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="NHibernate.Test"
namespace="NHibernate.Test.Criteria.FetchAllProperties"
default-access="property"
default-lazy="true">

<class name="BlogPost" table="BlogPost">
<id name="Id" type="Int64">
<generator class="native"/>
</id>
<property name="Title" />
<property name="Description" lazy="true"/>
</class>

</hibernate-mapping>
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System;
using System.Collections;
using NUnit.Framework;

namespace NHibernate.Test.Criteria.FetchAllProperties
{
[TestFixture]
public class FetchAllPropertiesTests : TestCase
{
#region Overrides of TestCase

protected override string MappingsAssembly
{
get { return "NHibernate.Test"; }
}

protected override IList Mappings
{
get
{
return new[]
{
"Criteria.FetchAllProperties.BlogPost.hbm.xml"
};
}
}

protected override void OnSetUp()
{
using (ISession session = OpenSession())
{
ITransaction t = session.BeginTransaction();

BlogPost blogPost = new BlogPost();
blogPost.Title = "ICriteria supports eager property fetch!";
blogPost.Description = "It really does!";
session.Save(blogPost);

t.Commit();
}
}

protected override void OnTearDown()
{
using (ISession session = sessions.OpenSession())
{
session.Delete("from System.Object");
session.Flush();
}
}

#endregion

[Test]
public void DidLoadDescription()
{
BlogPost result;
using (ISession session = sessions.OpenSession())
{
result = session.CreateCriteria(typeof(BlogPost))
.SetLazyPropertyFetchMode(LazyPropertyFetchMode.Select)
.UniqueResult<BlogPost>();


}

Assert.DoesNotThrow(
delegate
{
Assert.That(String.IsNullOrEmpty(result.Description), Is.False, "Should have been a value in Description");
}, "Should not have thrown an exception accessing the Description property");
}

[Test]
public void ShouldThrowExceptionWhenAccessingLazyPropertyOutsideSession()
{
BlogPost result;
using (ISession session = sessions.OpenSession())
{
result = session.CreateCriteria(typeof(BlogPost))
.UniqueResult<BlogPost>();


}

Assert.Throws<LazyInitializationException>(
delegate
{
Assume.That(String.IsNullOrEmpty(result.Description), Is.True);
}, "Should have thrown an exception accessing the Description property");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
using System;
using System.Collections;
using System.Linq;
using NUnit.Framework;

namespace NHibernate.Test.Criteria.FetchAllProperties
{
[TestFixture]
public class FetchAllPropertiesTestsWithRelationships : TestCase
{
#region Overrides of TestCase

protected override string MappingsAssembly
{
get { return "NHibernate.Test"; }
}

protected override IList Mappings
{
get
{
return new[]
{
"Criteria.FetchAllProperties.Article.hbm.xml"
};
}
}

protected override void OnSetUp()
{
using (ISession session = OpenSession())
{
ITransaction t = session.BeginTransaction();

var article = new Article
{
Title = "Article one",
Description = "The descripition for article one",
};
article.Comments.Add(new Comment { Article = article, Subject = "Cool stuff", Text = "Cool stuff comment" });
article.Comments.Add(new Comment { Article = article, Subject = "Could it be true?", Text = "Fetch all in ICriteria rules" });
article.ArticleExtension = new ArticleExtension { Article = article, Rating = 3, Notes = "A few notes" };
session.Save(article);

var article2 = new Article
{
Title = "Article two",
Description = "The descripition for article two",
};
article2.Comments.Add(new Comment { Article = article, Subject = "The beatles", Text = "Are a great band" });
article2.Comments.Add(new Comment { Article = article, Subject = "See you thursday", Text = "Can't wait to grab beers at the new bar!" });
article2.ArticleExtension = new ArticleExtension { Article = article2, Rating = 5, Notes = "I don't like notes" };
session.Save(article2);

t.Commit();
}
}

protected override void OnTearDown()
{
using (ISession session = sessions.OpenSession())
{
session.Delete("from System.Object");
session.Flush();
}
}

#endregion

[Test]
public void CanEagerFetchRelatedPropertiesUsingSubcriteria()
{
Article result;
using (ISession session = sessions.OpenSession())
{
result = session.CreateCriteria(typeof(Article))
.SetLazyPropertyFetchMode(LazyPropertyFetchMode.Select)
.SetMaxResults(1)
.CreateCriteria("ArticleExtension")
.SetLazyPropertyFetchMode(LazyPropertyFetchMode.Select)
.UniqueResult<Article>();
}

Assert.DoesNotThrow(
delegate
{
Assert.That(String.IsNullOrEmpty(result.Description), Is.False, "Should have been a value in Description");
}, "Should not have thrown an exception accessing the Description property");

Assert.DoesNotThrow(
delegate
{
Assert.That(String.IsNullOrEmpty(result.ArticleExtension.Notes), Is.False, "Should have been a value in ArticleExtension.Notes");
}, "Should not have thrown an exception accessing the ArticleExtension.Notes property");
}

[Test]
public void CanEagerFetchRelatedPropertiesUsingAssociationPath()
{
Article result;
using (ISession session = sessions.OpenSession())
{
result = session.CreateCriteria(typeof(Article))
.SetLazyPropertyFetchMode(LazyPropertyFetchMode.Select)
.SetLazyPropertyFetchMode("ArticleExtension", LazyPropertyFetchMode.Select)
.SetMaxResults(1)
.UniqueResult<Article>();
}

Assert.DoesNotThrow(
delegate
{
Assert.That(String.IsNullOrEmpty(result.Description), Is.False, "Should have been a value in Description");
}, "Should not have thrown an exception accessing the Description property");

Assert.DoesNotThrow(
delegate
{
Assert.That(String.IsNullOrEmpty(result.ArticleExtension.Notes), Is.False, "Should have been a value in ArticleExtension.Notes");
}, "Should not have thrown an exception accessing the ArticleExtension.Notes property");
}

[Test]
public void CanEagerFetchChildAndNotParentProperties()
{
Article result;
using (ISession session = sessions.OpenSession())
{
result = session.CreateCriteria(typeof(Article))
.SetMaxResults(1)
.CreateCriteria("ArticleExtension")
.SetLazyPropertyFetchMode(LazyPropertyFetchMode.Select)
.UniqueResult<Article>();
}

Assert.Throws<LazyInitializationException>(
delegate
{
Assert.That(String.IsNullOrEmpty(result.Description), Is.True);
}, "Should have thrown an exception accessing the Description property");

Assert.DoesNotThrow(
delegate
{
Assert.That(String.IsNullOrEmpty(result.ArticleExtension.Notes), Is.False, "Should have been a value in ArticleExtension.Notes");
}, "Should not have thrown an exception accessing the ArticleExtension.Notes property");
}

[Test]
public void CanEagerFetchParentAndNotChildProperties()
{
Article result;
using (ISession session = sessions.OpenSession())
{
result = session.CreateCriteria(typeof(Article))
.SetMaxResults(1)
.SetLazyPropertyFetchMode(LazyPropertyFetchMode.Select)
.CreateCriteria("ArticleExtension")
.UniqueResult<Article>();
}

Assert.DoesNotThrow(
delegate
{
Assert.That(String.IsNullOrEmpty(result.Description), Is.False);
}, "Should not have thrown an exception accessing the Description property");

Assert.Throws<LazyInitializationException>(
delegate
{
Assert.That(String.IsNullOrEmpty(result.ArticleExtension.Notes), Is.True);
}, "Should have thrown an exception accessing the ArticleExtension.Notes property");
}
}
}
Loading