Skip to content
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
2 changes: 2 additions & 0 deletions src/NHibernate.Test/NHibernate.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -1089,13 +1089,15 @@
<Compile Include="ReadOnly\VersionedNode.cs" />
<Compile Include="Stateless\Contact.cs" />
<Compile Include="Stateless\Country.cs" />
<Compile Include="Stateless\FetchingLazyCollections\TreeFetchTests.cs" />
<Compile Include="Stateless\FetchingLazyCollections\LazyCollectionFetchTests.cs" />
<Compile Include="Stateless\Fetching\Resource.cs" />
<Compile Include="Stateless\Fetching\StatelessSessionFetchingTest.cs" />
<Compile Include="Stateless\Fetching\Task.cs" />
<Compile Include="Stateless\Fetching\User.cs" />
<Compile Include="Stateless\Org.cs" />
<Compile Include="Stateless\StatelessSessionQueryFixture.cs" />
<Compile Include="Stateless\TreeNode.cs" />
<Compile Include="Subselect\ClassSubselectFixture.cs" />
<Compile Include="Subselect\Domain.cs" />
<Compile Include="TestCaseMappingByCode.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Iesi.Collections.Generic;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
using NHibernate.Mapping.ByCode.Conformist;
using NHibernate.Linq;
using NUnit.Framework;
using SharpTestsEx;

Expand Down Expand Up @@ -130,6 +132,24 @@ public void ShouldWorkLoadingComplexEntities()
tx.Commit();
}

using (IStatelessSession s = sessions.OpenStatelessSession())
using (ITransaction tx = s.BeginTransaction())
{
IList<Family<Human>> hf = s.Query<Family<Human>>().FetchMany(f => f.Childs).ToList();
Assert.That(hf.Count, Is.EqualTo(1));
Assert.That(hf[0].Father.Name, Is.EqualTo(humanFather));
Assert.That(hf[0].Mother.Name, Is.EqualTo(humanMother));
NHibernateUtil.IsInitialized(hf[0].Childs).Should("Lazy collection should be initialized").Be.True();

IList<Family<Reptile>> rf = s.Query<Family<Reptile>>().FetchMany(f => f.Childs).ToList();
Assert.That(rf.Count, Is.EqualTo(1));
Assert.That(rf[0].Father.Description, Is.EqualTo(crocodileFather));
Assert.That(rf[0].Mother.Description, Is.EqualTo(crocodileMother));
NHibernateUtil.IsInitialized(hf[0].Childs).Should("Lazy collection should be initialized").Be.True();

tx.Commit();
}

using (ISession s = sessions.OpenSession())
using (ITransaction tx = s.BeginTransaction())
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Iesi.Collections.Generic;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
using NHibernate.Mapping.ByCode.Conformist;
using NHibernate.Linq;
using NUnit.Framework;
using SharpTestsEx;

namespace NHibernate.Test.Stateless.FetchingLazyCollections
{
public class TreeFetchTests : TestCaseMappingByCode
{
protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();
mapper.BeforeMapClass += (mi, t, cm) => cm.Id(im => im.Generator(Generators.HighLow));
mapper.Class<TreeNode>(
mc =>
{
mc.Id(x => x.Id);
mc.Property(x => x.Content);
mc.Set(x => x.Children, cam =>
{
cam.Key(km => km.Column("parentId"));
cam.Cascade(Mapping.ByCode.Cascade.All);
}, rel => rel.OneToMany());
});
var mappings = mapper.CompileMappingForAllExplicitlyAddedEntities();
return mappings;
}

[Test]
public void FetchMultipleHierarchies()
{
using (ISession s = sessions.OpenSession())
using (ITransaction tx = s.BeginTransaction())
{
var root = new TreeNode { Content = "Root" };
var child1 = new TreeNode { Content = "Child1" };
root.Children.Add(child1);
root.Children.Add(new TreeNode { Content = "Child2" });
child1.Children.Add(new TreeNode { Content = "Child1Child1" });
child1.Children.Add(new TreeNode { Content = "Child1Child2" });
s.Save(root);
tx.Commit();
}

using (IStatelessSession s = sessions.OpenStatelessSession())
using (ITransaction tx = s.BeginTransaction())
{
IList<TreeNode> rootNodes = s.Query<TreeNode>().Where(t => t.Content == "Root")
.FetchMany(f => f.Children)
.ThenFetchMany(f => f.Children).ToList();
Assert.That(rootNodes.Count, Is.EqualTo(1));
Assert.That(rootNodes.First().Children.Count, Is.EqualTo(2));

tx.Commit();
}

using (ISession s = sessions.OpenSession())
using (ITransaction tx = s.BeginTransaction())
{
s.Delete("from TreeNode");
tx.Commit();
}
}
}
}
23 changes: 23 additions & 0 deletions src/NHibernate.Test/Stateless/TreeNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Iesi.Collections.Generic;

namespace NHibernate.Test.Stateless
{
public class TreeNode
{
private ISet<TreeNode> children = new HashedSet<TreeNode>();

public virtual int Id { get; protected set; }

public virtual string Content { get; set; }

public virtual ISet<TreeNode> Children
{
get { return children; }
protected set { children = value; }
}
}
}
8 changes: 7 additions & 1 deletion src/NHibernate/Impl/StatelessSessionImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,13 @@ public override bool IsEventSource
public override object GetEntityUsingInterceptor(EntityKey key)
{
CheckAndUpdateSessionStatus();
return null;
// while a pending Query we should use existing temporary entities so a join fetch does not create multiple instances
// of the same parent item
object obj;
if (temporaryPersistenceContext.EntitiesByKey.TryGetValue(key, out obj))
return obj;
else
return null;
}

public override IPersistenceContext PersistenceContext
Expand Down