Skip to content

Fix Refresh and Load event listeners for entity proxy #2949

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

Merged
merged 2 commits into from
Dec 7, 2021
Merged
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
97 changes: 97 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH2727/FixtureByCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------


using System;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH2727
{
using System.Threading.Tasks;
/// <summary>
/// Fixture using 'by code' mappings
/// </summary>
[TestFixture]
public class LazyPropByCodeFixtureAsync : TestCaseMappingByCode
{
private Guid id1;
private Guid id2;

protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();
mapper.Class<Entity>(
rc =>
{
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
rc.Property(x => x.Name);
rc.Property(x => x.LazyProp, m => m.Lazy(true));
});

return mapper.CompileMappingForAllExplicitlyAddedEntities();
}

protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var e1 = new Entity { Name = "Bob" };
session.Save(e1);

var e2 = new Entity { Name = "Sally" };
session.Save(e2);

transaction.Commit();
id1 = e1.Id;
id2 = e2.Id;
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
// The HQL delete does all the job inside the database without loading the entities, but it does
// not handle delete order for avoiding violating constraints if any. Use
// session.Delete("from System.Object");
// instead if in need of having NHbernate ordering the deletes, but this will cause
// loading the entities in the session.
session.CreateQuery("delete from System.Object").ExecuteUpdate();

transaction.Commit();
}
}

[Test]
public async Task CanLoadUsingInstanceAsync()
{
using (var s = OpenSession())
{
var e1 = await (s.LoadAsync<Entity>(id1));
await (s.LoadAsync(e1, id2));
}
}

[Test(Description = "GH-2928")]
public async Task CanSessionRefreshEntityWithLazyPropertiesAsync()
{
using (var s = OpenSession())
{
var e1 = await (s.GetAsync<Entity>(id1));
await (s.RefreshAsync(e1));
s.Clear();
await (s.RefreshAsync(e1));
}
}
}
}
11 changes: 11 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2727/Entity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace NHibernate.Test.NHSpecificTest.GH2727
{
public class Entity
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
public virtual string LazyProp { get; set; }
}
}
86 changes: 86 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2727/FixtureByCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH2727
{
/// <summary>
/// Fixture using 'by code' mappings
/// </summary>
[TestFixture]
public class LazyPropByCodeFixture : TestCaseMappingByCode
{
private Guid id1;
private Guid id2;

protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();
mapper.Class<Entity>(
rc =>
{
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
rc.Property(x => x.Name);
rc.Property(x => x.LazyProp, m => m.Lazy(true));
});

return mapper.CompileMappingForAllExplicitlyAddedEntities();
}

protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var e1 = new Entity { Name = "Bob" };
session.Save(e1);

var e2 = new Entity { Name = "Sally" };
session.Save(e2);

transaction.Commit();
id1 = e1.Id;
id2 = e2.Id;
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
// The HQL delete does all the job inside the database without loading the entities, but it does
// not handle delete order for avoiding violating constraints if any. Use
// session.Delete("from System.Object");
// instead if in need of having NHbernate ordering the deletes, but this will cause
// loading the entities in the session.
session.CreateQuery("delete from System.Object").ExecuteUpdate();

transaction.Commit();
}
}

[Test]
public void CanLoadUsingInstance()
{
using (var s = OpenSession())
{
var e1 = s.Load<Entity>(id1);
s.Load(e1, id2);
}
}

[Test(Description = "GH-2928")]
public void CanSessionRefreshEntityWithLazyProperties()
{
using (var s = OpenSession())
{
var e1 = s.Get<Entity>(id1);
s.Refresh(e1);
s.Clear();
s.Refresh(e1);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public virtual async Task OnLoadAsync(LoadEvent @event, LoadType loadType, Cance
IEntityPersister persister;
if (@event.InstanceToLoad != null)
{
persister = source.GetEntityPersister(null, @event.InstanceToLoad); //the load() which takes an entity does not pass an entityName
@event.EntityClassName = @event.InstanceToLoad.GetType().FullName;
@event.EntityClassName = source.BestGuessEntityName(@event.InstanceToLoad); //the load() which takes an entity does not pass an entityName
persister = source.GetEntityPersister(@event.EntityClassName, @event.InstanceToLoad);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public virtual async Task OnRefreshAsync(RefreshEvent @event, IDictionary refres

if (e == null)
{
persister = source.GetEntityPersister(null, obj); //refresh() does not pass an entityName
persister = source.GetEntityPersister(source.BestGuessEntityName(obj), obj); //refresh() does not pass an entityName
id = persister.GetIdentifier(obj);
if (log.IsDebugEnabled())
{
Expand Down
4 changes: 2 additions & 2 deletions src/NHibernate/Event/Default/DefaultLoadEventListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public virtual void OnLoad(LoadEvent @event, LoadType loadType)
IEntityPersister persister;
if (@event.InstanceToLoad != null)
{
persister = source.GetEntityPersister(null, @event.InstanceToLoad); //the load() which takes an entity does not pass an entityName
@event.EntityClassName = @event.InstanceToLoad.GetType().FullName;
@event.EntityClassName = source.BestGuessEntityName(@event.InstanceToLoad); //the load() which takes an entity does not pass an entityName
persister = source.GetEntityPersister(@event.EntityClassName, @event.InstanceToLoad);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public virtual void OnRefresh(RefreshEvent @event, IDictionary refreshedAlready)

if (e == null)
{
persister = source.GetEntityPersister(null, obj); //refresh() does not pass an entityName
persister = source.GetEntityPersister(source.BestGuessEntityName(obj), obj); //refresh() does not pass an entityName
id = persister.GetIdentifier(obj);
if (log.IsDebugEnabled())
{
Expand Down