diff --git a/Src/NHibernate.Envers.Tests/Async/NetSpecific/Integration/ManyToOne/LazyProperty/Bidirectional/LazyPropertyTest.cs b/Src/NHibernate.Envers.Tests/Async/NetSpecific/Integration/ManyToOne/LazyProperty/Bidirectional/LazyPropertyTest.cs new file mode 100644 index 00000000..ecc70e78 --- /dev/null +++ b/Src/NHibernate.Envers.Tests/Async/NetSpecific/Integration/ManyToOne/LazyProperty/Bidirectional/LazyPropertyTest.cs @@ -0,0 +1,41 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by AsyncGenerator. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + + +using System.Linq; +using NUnit.Framework; +using SharpTestsEx; + +namespace NHibernate.Envers.Tests.NetSpecific.Integration.ManyToOne.LazyProperty.Bidirectional +{ + using System.Threading.Tasks; + public partial class LazyPropertyBidirectionalTest : TestBase + { + + [Test] + public async Task SavePersonProxyForFieldInterceptorAsync() + { + long carId; + using (var tx = Session.BeginTransaction()) + { + var pers = Session.Query().Single(x => x.Id == id_pers1); + var car = new Car + { + Owner = pers + }; + pers.Cars.Add(car); + carId = (long) await (Session.SaveAsync(car)).ConfigureAwait(false); + await (tx.CommitAsync()).ConfigureAwait(false); + } + + (await (AuditReader().FindAsync(carId, 2)).ConfigureAwait(false)).Owner.Name + .Should().Be.EqualTo("Hernan"); + } + } +} \ No newline at end of file diff --git a/Src/NHibernate.Envers.Tests/NetSpecific/Integration/ManyToOne/LazyProperty/Bidirectional/Car.cs b/Src/NHibernate.Envers.Tests/NetSpecific/Integration/ManyToOne/LazyProperty/Bidirectional/Car.cs new file mode 100644 index 00000000..b06882be --- /dev/null +++ b/Src/NHibernate.Envers.Tests/NetSpecific/Integration/ManyToOne/LazyProperty/Bidirectional/Car.cs @@ -0,0 +1,12 @@ +using NHibernate.Envers.Configuration.Attributes; + +namespace NHibernate.Envers.Tests.NetSpecific.Integration.ManyToOne.LazyProperty.Bidirectional +{ + [Audited] + public class Car + { + public virtual long Id { get; set; } + public virtual int Number { get; set; } + public virtual Person Owner { get; set; } + } +} \ No newline at end of file diff --git a/Src/NHibernate.Envers.Tests/NetSpecific/Integration/ManyToOne/LazyProperty/Bidirectional/LazyPropertyTest.cs b/Src/NHibernate.Envers.Tests/NetSpecific/Integration/ManyToOne/LazyProperty/Bidirectional/LazyPropertyTest.cs new file mode 100644 index 00000000..f59262e2 --- /dev/null +++ b/Src/NHibernate.Envers.Tests/NetSpecific/Integration/ManyToOne/LazyProperty/Bidirectional/LazyPropertyTest.cs @@ -0,0 +1,46 @@ +using System.Linq; +using NUnit.Framework; +using SharpTestsEx; + +namespace NHibernate.Envers.Tests.NetSpecific.Integration.ManyToOne.LazyProperty.Bidirectional +{ + public partial class LazyPropertyBidirectionalTest : TestBase + { + private long id_pers1; + + public LazyPropertyBidirectionalTest(AuditStrategyForTest strategyType) : base(strategyType) + { + } + + protected override void Initialize() + { + var pers1 = new Person {Name = "Hernan"}; + + using (var tx = Session.BeginTransaction()) + { + id_pers1 = (long) Session.Save(pers1); + tx.Commit(); + } + } + + [Test] + public void SavePersonProxyForFieldInterceptor() + { + long carId; + using (var tx = Session.BeginTransaction()) + { + var pers = Session.Query().Single(x => x.Id == id_pers1); + var car = new Car + { + Owner = pers + }; + pers.Cars.Add(car); + carId = (long) Session.Save(car); + tx.Commit(); + } + + AuditReader().Find(carId, 2).Owner.Name + .Should().Be.EqualTo("Hernan"); + } + } +} \ No newline at end of file diff --git a/Src/NHibernate.Envers.Tests/NetSpecific/Integration/ManyToOne/LazyProperty/Bidirectional/Mapping.hbm.xml b/Src/NHibernate.Envers.Tests/NetSpecific/Integration/ManyToOne/LazyProperty/Bidirectional/Mapping.hbm.xml new file mode 100644 index 00000000..9bd70ede --- /dev/null +++ b/Src/NHibernate.Envers.Tests/NetSpecific/Integration/ManyToOne/LazyProperty/Bidirectional/Mapping.hbm.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Src/NHibernate.Envers.Tests/NetSpecific/Integration/ManyToOne/LazyProperty/Bidirectional/Person.cs b/Src/NHibernate.Envers.Tests/NetSpecific/Integration/ManyToOne/LazyProperty/Bidirectional/Person.cs new file mode 100644 index 00000000..82287184 --- /dev/null +++ b/Src/NHibernate.Envers.Tests/NetSpecific/Integration/ManyToOne/LazyProperty/Bidirectional/Person.cs @@ -0,0 +1,13 @@ +using NHibernate.Envers.Configuration.Attributes; +using System.Collections.Generic; + +namespace NHibernate.Envers.Tests.NetSpecific.Integration.ManyToOne.LazyProperty.Bidirectional +{ + [Audited] + public class Person + { + public virtual long Id { get; set; } + public virtual string Name { get; set; } + public virtual ISet Cars { get; set; } + } +} \ No newline at end of file diff --git a/Src/NHibernate.Envers/Event/AuditEventListener.cs b/Src/NHibernate.Envers/Event/AuditEventListener.cs index c9306b4e..81642f6a 100644 --- a/Src/NHibernate.Envers/Event/AuditEventListener.cs +++ b/Src/NHibernate.Envers/Event/AuditEventListener.cs @@ -81,20 +81,17 @@ private void addCollectionChangeWorkUnit(AuditProcess auditProcess, ISessionImpl { // relDesc.getToEntityName() doesn't always return the entity name of the value - in case // of subclasses, this will be root class, no the actual class. So it can't be used here. - string toEntityName; object id; + var toEntityName = session.BestGuessEntityName(value); if (value is INHibernateProxy newValueAsProxy) { - toEntityName = session.BestGuessEntityName(value); id = newValueAsProxy.HibernateLazyInitializer.Identifier; // We've got to initialize the object from the proxy to later read its state. value = Toolz.GetTargetFromProxy(session, newValueAsProxy); } else { - toEntityName = session.GuessEntityName(value); - var idMapper = VerCfg.EntCfg[toEntityName].IdMapper; id = idMapper.MapToIdFromEntity(value); } @@ -223,10 +220,10 @@ private void generateBidirectionalCollectionChangeWorkUnits(AuditProcess auditPr var toPropertyName = toPropertyNames.First(); auditProcess.AddWorkUnit(new CollectionChangeWorkUnit(evt.Session, - evt.Session.BestGuessEntityName(relatedObj), + evt.Session.BestGuessEntityName(relatedObj), toPropertyName, - VerCfg, - relatedId, + VerCfg, + relatedId, relatedObj)); } } @@ -299,7 +296,7 @@ private void onCollectionAction(AbstractCollectionEvent evt, } else { - var workUnit = new PersistentCollectionChangeWorkUnit(evt.Session, entityName, VerCfg, newColl, + var workUnit = new PersistentCollectionChangeWorkUnit(evt.Session, entityName, VerCfg, newColl, collectionEntry, oldColl, evt.AffectedOwnerIdOrNull, referencingPropertyName); verSync.AddWorkUnit(workUnit); @@ -400,7 +397,7 @@ public virtual void Initialize(Cfg.Configuration cfg) private static void checkIfTransactionInProgress(ISessionImplementor session) { - if (!session.TransactionInProgress && session.TransactionContext==null) + if (!session.TransactionInProgress && session.TransactionContext == null) { // Historical data would not be flushed to audit tables if outside of active transaction // (AuditProcess#doBeforeTransactionCompletion(SessionImplementor) not executed). @@ -412,7 +409,7 @@ private bool shouldGenerateRevision(AbstractCollectionEvent evt) { var entityName = evt.GetAffectedOwnerEntityName(); return VerCfg.GlobalCfg.GenerateRevisionsForCollections - && VerCfg.EntCfg.IsVersioned(entityName); + && VerCfg.EntCfg.IsVersioned(entityName); } private static object initializeCollection(AbstractCollectionEvent evt)