Skip to content

NH-3596 - DistinctRootEntityResultTransformer breaks Cache #383

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 6 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
using System.Collections.Generic;
using System.Linq;
using NHibernate.Cache;
using NHibernate.Cfg;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
using NHibernate.Mapping.ByCode.Conformist;
using NHibernate.Transform;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.NH3596
{
public class EntityBase
{
public virtual int Id { get; set; }
}

public class Role : EntityBase
{
public virtual string Name { get; set; }
public virtual Role Parent { get; set; }
public virtual IList<Role> Children { get; set; }
}

public class RoleMap : ClassMapping<Role>
{
public RoleMap()
{
Cache(x => x.Usage(CacheUsage.ReadOnly));
Id(x => x.Id, x => x.Generator(Generators.Identity));
Property(x => x.Name);
ManyToOne(x => x.Parent, x =>
{
x.Column("ParentId");
x.NotNullable(false);
});
Bag(x => x.Children, x =>
{
x.Cascade(Mapping.ByCode.Cascade.All);
x.Key(y => y.Column("ParentId"));
}, x => x.OneToMany());
}
}

public class CachingWithTransformerTests: TestCaseMappingByCode
{
protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();
mapper.AddMapping<RoleMap>();
var mapping = mapper.CompileMappingForAllExplicitlyAddedEntities();
return mapping;
}

protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var parentRoles = new List<Role>()
{
new Role() { Name = "Admin", Children = null, Parent = null },
new Role() { Name = "Manager", Children = null, Parent = null },
new Role() { Name = "Support", Children = null, Parent = null }
};

var childRoles = new List<Role>()
{
new Role() { Name = "Manager-Secretary", Children = null, Parent = parentRoles.FirstOrDefault(x => x.Name == "Manager") },
new Role() { Name = "Superviser", Children = null, Parent = parentRoles.FirstOrDefault(x => x.Name == "Manager") }
};

foreach (var parentRole in parentRoles)
{
parentRole.Children = new List<Role>(childRoles.Where(x => x.Parent == parentRole));

session.Save(parentRole);
}

session.Flush();
transaction.Commit();
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.Delete("from System.Object");

session.Flush();
transaction.Commit();
}
}

protected override void Configure(Cfg.Configuration configuration)
{
base.Configure(configuration);

configuration.Cache(x =>
{
x.Provider<HashtableCacheProvider>();
x.UseQueryCache = true;
});
}

[Test]
public void UsingQueryOverToFutureWithCacheAndTransformerDoesntThrow()
{
//NH-3596
using (var session = this.OpenSession())
using (session.BeginTransaction())
{
Role children = null;
Role parent = null;

//store values in cache
session
.QueryOver<Role>()
.Where(x => x.Parent == null)
.Left.JoinAlias(x => x.Children, () => children)
.Left.JoinAlias(x => x.Parent, () => parent)
.TransformUsing(Transformers.DistinctRootEntity)
.Cacheable()
.CacheMode(CacheMode.Normal)
.Future();

//get values from cache
var roles = session
.QueryOver<Role>()
.Where(x => x.Parent == null)
.Left.JoinAlias(x => x.Children, () => children)
.Left.JoinAlias(x => x.Parent, () => parent)
.TransformUsing(Transformers.DistinctRootEntity)
.Cacheable()
.CacheMode(CacheMode.Normal)
.Future();

var result = roles.ToList();

Assert.AreEqual(3, result.Count);
}
}

[Test]
public void UsingHqlToFutureWithCacheAndTransformerDoesntThrow()
{
//NH-3596
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is NH-2867

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the same problem

using (var session = this.OpenSession())
using (session.BeginTransaction())
{
//store values in cache
session
.CreateQuery("select r from Role r left join r.Children left join r.Parent where r.Parent is null")
.SetResultTransformer(Transformers.DistinctRootEntity)
.SetCacheable(true)
.SetCacheMode(CacheMode.Normal)
.Future<Role>();

//get values from cache
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure that this will be from cache?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be, shouldn't it? It's the same query, no parameters...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget that they are future queries.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The query cache key will be

"select role0_.Id as Id0_0_, children1_.Id as Id0_1_, role2_.Id as Id0_2_, role0_.Name as Name0_0_, role0_.ParentId as ParentId0_0_, children1_.Name as Name0_1_, children1_.ParentId as ParentId0_1_, children1_.ParentId as ParentId0__, children1_.Id as Id0__, role2_.Name as Name0_2_, role2_.ParentId as ParentId0_2_ from Role role0_ left outer join Role children1_ on role0_.Id=children1_.ParentId left outer join Role role2_ on role0_.ParentId=role2_.Id where role0_.ParentId is null;\r\nselect role0_.Id as Id0_0_, children1_.Id as Id0_1_, role2_.Id as Id0_2_, role0_.Name as Name0_0_, role0_.ParentId as ParentId0_0_, children1_.Name as Name0_1_, children1_.ParentId as ParentId0_1_, children1_.ParentId as ParentId0__, children1_.Id as Id0__, role2_.Name as Name0_2_, role2_.ParentId as ParentId0_2_ from Role role0_ left outer join Role children1_ on role0_.Id=children1_.ParentId left outer join Role role2_ on role0_.ParentId=role2_.Id where role0_.ParentId is null;\r\n"

Which means that both queries' results will go to the same cache item...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In other words the first future is pending and not executed before the second one gets executed. So both queries (although being the same) ends-up batched together in a single round-trip to database. So it has nothing to retrieve from cache, since only one actual sql-query is performed.

The previous test has the same issue.

var roles = session
.CreateQuery("select r from Role r left join r.Children left join r.Parent where r.Parent is null")
.SetResultTransformer(Transformers.DistinctRootEntity)
.SetCacheable(true)
.SetCacheMode(CacheMode.Normal)
.Future<Role>();

var result = roles.ToList();

Assert.AreEqual(3, result.Count);
}
}
}
}
1 change: 1 addition & 0 deletions src/NHibernate.Test/NHibernate.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,7 @@
<Compile Include="NHSpecificTest\NH3570\BiFixture.cs" />
<Compile Include="NHSpecificTest\NH3570\Model.cs" />
<Compile Include="NHSpecificTest\NH3570\UniFixture.cs" />
<Compile Include="NHSpecificTest\NH3596\CachingWithTransformerTests.cs" />
<Compile Include="NHSpecificTest\NH3666\Entity.cs" />
<Compile Include="NHSpecificTest\NH3666\Fixture.cs" />
<Compile Include="NHSpecificTest\NH3731\Entity.cs" />
Expand Down
29 changes: 23 additions & 6 deletions src/NHibernate/Impl/MultipleQueriesCacheAssembler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using NHibernate.Cache;
using NHibernate.Engine;
using NHibernate.Type;
Expand Down Expand Up @@ -28,13 +29,29 @@ public object Disassemble(object value, ISessionImplementor session, object owne
var singleQueryCached = new List<object>();
foreach (object objToCache in itemList)
{
if (assemblers.Length == 1)
{
singleQueryCached.Add(assemblers[0].Disassemble(objToCache, session, owner));
}
else
if (objToCache != null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If #1783 is merged, this fix will be fixing an obsoleted class.

The future case is maybe already no more failing or failing in another way, since it does no more use multi-query/multi-criteria. (See #1718.)

{
singleQueryCached.Add(TypeHelper.Disassemble((object[]) objToCache, assemblers, null, session, null));
var valuesToCache = objToCache as object[];
var assemblersToCache = assemblers;

if (valuesToCache != null)
{
assemblersToCache = assemblers.Where((x, index) => valuesToCache[index] != null).ToArray();
valuesToCache = valuesToCache.Where(x => x != null).ToArray();
}
else
{
valuesToCache = new object[] { objToCache };
}

if (valuesToCache.Length == 1)
{
singleQueryCached.Add(assemblers[0].Disassemble(valuesToCache[0], session, owner));
}
else
{
singleQueryCached.Add(TypeHelper.Disassemble(valuesToCache, assemblersToCache, null, session, null));
}
}
}
cacheable.Add(singleQueryCached);
Expand Down