-
Notifications
You must be signed in to change notification settings - Fork 933
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
Changes from all commits
0bed7d9
a545396
0e201b2
e1876e6
cfd3bad
50cbfcc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you sure that this will be from cache? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't forget that they are future queries. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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... There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} | ||
} | ||
} |
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; | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
{ | ||
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); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is NH-2867
There was a problem hiding this comment.
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