Skip to content

Commit

Permalink
Code style cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
oskarb committed Oct 22, 2014
1 parent 19eb0d2 commit 62348a0
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -1,70 +1,67 @@
using System;
using System.Collections.Generic;
using System.Linq;
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;
}
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();
}
[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));
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();
}
tx.Commit();
}

using (ISession s = sessions.OpenSession())
using (ITransaction tx = s.BeginTransaction())
{
s.Delete("from TreeNode");
tx.Commit();
}
}
}
using (ISession s = sessions.OpenSession())
using (ITransaction tx = s.BeginTransaction())
{
s.Delete("from TreeNode");
tx.Commit();
}
}
}
}
22 changes: 11 additions & 11 deletions src/NHibernate.Test/Stateless/TreeNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@

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

public virtual int Id { get; protected set; }
public virtual int Id { get; protected set; }

public virtual string Content { get; set; }
public virtual string Content { get; set; }

public virtual ISet<TreeNode> Children
{
get { return children; }
protected set { children = value; }
}
}
public virtual ISet<TreeNode> Children
{
get { return _children; }
protected set { _children = value; }
}
}
}
14 changes: 7 additions & 7 deletions src/NHibernate/Impl/StatelessSessionImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,13 +316,13 @@ public override bool IsEventSource
public override object GetEntityUsingInterceptor(EntityKey key)
{
CheckAndUpdateSessionStatus();
// 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;
// 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;

return null;
}

public override IPersistenceContext PersistenceContext
Expand Down

0 comments on commit 62348a0

Please sign in to comment.