Skip to content

Skip initialization of lazy properties when setting one #1943

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 4 commits into from
Dec 25, 2018
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
17 changes: 16 additions & 1 deletion src/NHibernate.Test/Async/GhostProperty/GhostPropertyFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,21 @@ public async Task WhenGetThenLoadOnlyNoLazyPlainPropertiesAsync()
}
Assert.That(NHibernateUtil.IsPropertyInitialized(order, "ALazyProperty"), Is.True);
}
}
}

[Test]
public async Task AcceptPropertySetWithTransientObjectAsync()
{
Order order;
using (var s = OpenSession())
{
order = await (s.GetAsync<Order>(1));
}

var newPayment = new WireTransfer();
order.Payment = newPayment;

Assert.That(order.Payment, Is.EqualTo(newPayment));
}
}
}
97 changes: 97 additions & 0 deletions src/NHibernate.Test/Async/LazyProperty/LazyPropertyFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@

using System.Collections;
using System.Linq;
using NHibernate.Cfg;
using NHibernate.Intercept;
using NHibernate.Tuple.Entity;
using NUnit.Framework;
using NUnit.Framework.Constraints;
using NHibernate.Linq;

namespace NHibernate.Test.LazyProperty
{
using System.Threading.Tasks;
using System.Threading;
[TestFixture]
public class LazyPropertyFixtureAsync : TestCase
{
Expand All @@ -43,6 +46,11 @@ protected override DebugSessionFactory BuildSessionFactory()
}
}

protected override void Configure(Configuration configuration)
{
configuration.SetProperty(Environment.GenerateStatistics, "true");
}

protected override void OnSetUp()
{
Assert.That(
Expand All @@ -58,6 +66,7 @@ protected override void OnSetUp()
Name = "some name",
Id = 1,
ALotOfText = "a lot of text ...",
Image = new byte[10],
FieldInterceptor = "Why not that name?"
});
tx.Commit();
Expand Down Expand Up @@ -122,6 +131,94 @@ public async Task CanGetValueForLazyPropertyAsync()
}
}

[Test]
public async Task CanSetValueForLazyPropertyAsync()
{
Book book;
using (ISession s = OpenSession())
{
book = await (s.GetAsync<Book>(1));
}

book.ALotOfText = "text";

Assert.That(book.ALotOfText, Is.EqualTo("text"));
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.True);
}

[TestCase(false)]
[TestCase(true)]
public async Task CanUpdateValueForLazyPropertyAsync(bool initializeAfterSet, CancellationToken cancellationToken = default(CancellationToken))
{
Book book;
using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
book = await (s.GetAsync<Book>(1, cancellationToken));
book.ALotOfText = "update-text";
if (initializeAfterSet)
{
var image = book.Image;
}

await (tx.CommitAsync(cancellationToken));
}

using (var s = OpenSession())
{
book = await (s.GetAsync<Book>(1, cancellationToken));
var text = book.ALotOfText;
}

Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.True);
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Image"), Is.True);
Assert.That(book.ALotOfText, Is.EqualTo("update-text"));
Assert.That(book.Image, Has.Length.EqualTo(10));
}

[TestCase(false)]
[TestCase(true)]
public async Task UpdateValueForLazyPropertyToSameValueAsync(bool initializeAfterSet, CancellationToken cancellationToken = default(CancellationToken))
{
Book book;
string text;

using (var s = OpenSession())
{
book = await (s.GetAsync<Book>(1, cancellationToken));
text = book.ALotOfText;
}

Sfi.Statistics.Clear();

using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
book = await (s.GetAsync<Book>(1, cancellationToken));
book.ALotOfText = text;
if (initializeAfterSet)
{
var image = book.Image;
}

await (tx.CommitAsync(cancellationToken));
}

Assert.That(Sfi.Statistics.EntityUpdateCount, Is.EqualTo(initializeAfterSet ? 0 : 1));
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.True);
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Image"), initializeAfterSet ? (Constraint) Is.True : Is.False);
Assert.That(book.ALotOfText, Is.EqualTo(text));

using (var s = OpenSession())
{
book = await (s.GetAsync<Book>(1, cancellationToken));
text = book.ALotOfText;
}

Assert.That(book.Image, Has.Length.EqualTo(10));
Assert.That(book.ALotOfText, Is.EqualTo(text));
}

[Test]
public async Task CanGetValueForNonLazyPropertyAsync()
{
Expand Down
17 changes: 16 additions & 1 deletion src/NHibernate.Test/GhostProperty/GhostPropertyFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,21 @@ public void WhenGetThenLoadOnlyNoLazyPlainProperties()
}
Assert.That(NHibernateUtil.IsPropertyInitialized(order, "ALazyProperty"), Is.True);
}
}
}

[Test]
public void AcceptPropertySetWithTransientObject()
{
Order order;
using (var s = OpenSession())
{
order = s.Get<Order>(1);
}

var newPayment = new WireTransfer();
order.Payment = newPayment;

Assert.That(order.Payment, Is.EqualTo(newPayment));
}
}
}
2 changes: 2 additions & 0 deletions src/NHibernate.Test/LazyProperty/Book.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public virtual string ALotOfText
set { _aLotOfText = value; }
}

public virtual byte[] Image { get; set; }

public virtual string FieldInterceptor { get; set; }
}
}
96 changes: 96 additions & 0 deletions src/NHibernate.Test/LazyProperty/LazyPropertyFixture.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System.Collections;
using System.Linq;
using NHibernate.Cfg;
using NHibernate.Intercept;
using NHibernate.Tuple.Entity;
using NUnit.Framework;
using NUnit.Framework.Constraints;

namespace NHibernate.Test.LazyProperty
{
Expand Down Expand Up @@ -31,6 +33,11 @@ protected override DebugSessionFactory BuildSessionFactory()
}
}

protected override void Configure(Configuration configuration)
{
configuration.SetProperty(Environment.GenerateStatistics, "true");
}

protected override void OnSetUp()
{
Assert.That(
Expand All @@ -46,6 +53,7 @@ protected override void OnSetUp()
Name = "some name",
Id = 1,
ALotOfText = "a lot of text ...",
Image = new byte[10],
FieldInterceptor = "Why not that name?"
});
tx.Commit();
Expand Down Expand Up @@ -116,6 +124,94 @@ public void CanGetValueForLazyProperty()
}
}

[Test]
public void CanSetValueForLazyProperty()
{
Book book;
using (ISession s = OpenSession())
{
book = s.Get<Book>(1);
}

book.ALotOfText = "text";

Assert.That(book.ALotOfText, Is.EqualTo("text"));
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.True);
}

[TestCase(false)]
[TestCase(true)]
public void CanUpdateValueForLazyProperty(bool initializeAfterSet)
{
Book book;
using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
book = s.Get<Book>(1);
book.ALotOfText = "update-text";
if (initializeAfterSet)
{
var image = book.Image;
}

tx.Commit();
}

using (var s = OpenSession())
{
book = s.Get<Book>(1);
var text = book.ALotOfText;
}

Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.True);
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Image"), Is.True);
Assert.That(book.ALotOfText, Is.EqualTo("update-text"));
Assert.That(book.Image, Has.Length.EqualTo(10));
}

[TestCase(false)]
[TestCase(true)]
public void UpdateValueForLazyPropertyToSameValue(bool initializeAfterSet)
{
Book book;
string text;

using (var s = OpenSession())
{
book = s.Get<Book>(1);
text = book.ALotOfText;
}

Sfi.Statistics.Clear();

using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
book = s.Get<Book>(1);
book.ALotOfText = text;
if (initializeAfterSet)
{
var image = book.Image;
}

tx.Commit();
}

Assert.That(Sfi.Statistics.EntityUpdateCount, Is.EqualTo(initializeAfterSet ? 0 : 1));
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.True);
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Image"), initializeAfterSet ? (Constraint) Is.True : Is.False);
Assert.That(book.ALotOfText, Is.EqualTo(text));

using (var s = OpenSession())
{
book = s.Get<Book>(1);
text = book.ALotOfText;
}

Assert.That(book.Image, Has.Length.EqualTo(10));
Assert.That(book.ALotOfText, Is.EqualTo(text));
}

[Test]
public void CanGetValueForNonLazyProperty()
{
Expand Down
1 change: 1 addition & 0 deletions src/NHibernate.Test/LazyProperty/Mappings.hbm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</id>
<property name="Name" />
<property name="ALotOfText" lazy="true" />
<property name="Image" lazy="true" />
<property name="FieldInterceptor" />
</class>

Expand Down
4 changes: 2 additions & 2 deletions src/NHibernate/Async/Engine/Cascade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ public async Task CascadeOnAsync(IEntityPersister persister, object parent, obje

IType[] types = persister.PropertyTypes;
CascadeStyle[] cascadeStyles = persister.PropertyCascadeStyles;
bool hasUninitializedLazyProperties = persister.HasUninitializedLazyProperties(parent);
var uninitializedLazyProperties = persister.GetUninitializedLazyProperties(parent);
for (int i = 0; i < types.Length; i++)
{
CascadeStyle style = cascadeStyles[i];
string propertyName = persister.PropertyNames[i];
if (hasUninitializedLazyProperties && persister.PropertyLaziness[i] && !action.PerformOnLazyProperty)
if (uninitializedLazyProperties.Contains(propertyName) && persister.PropertyLaziness[i] && !action.PerformOnLazyProperty)
{
//do nothing to avoid a lazy property initialization
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -876,8 +876,8 @@ public async Task UpdateAsync(object id, object[] fields, int[] dirtyFields, boo
// in the process of being deleted.
if (entry == null && !IsMutable)
throw new InvalidOperationException("Updating immutable entity that is not in session yet!");
if (entityMetamodel.IsDynamicUpdate && dirtyFields != null)

if (dirtyFields != null && (entityMetamodel.IsDynamicUpdate || HasDirtyLazyProperties(dirtyFields, obj)))
{
// For the case of dynamic-update="true", we need to generate the UPDATE SQL
propsToUpdate = GetPropertiesToUpdate(dirtyFields, hasDirtyCollection);
Expand Down
4 changes: 4 additions & 0 deletions src/NHibernate/Async/Persister/Entity/IEntityPersister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//------------------------------------------------------------------------------


using System;
using NHibernate.Cache;
using NHibernate.Cache.Entry;
using NHibernate.Engine;
Expand All @@ -16,6 +17,9 @@
using NHibernate.Tuple.Entity;
using NHibernate.Type;
using System.Collections;
using System.Collections.Generic;
using NHibernate.Intercept;
using NHibernate.Util;

namespace NHibernate.Persister.Entity
{
Expand Down
4 changes: 2 additions & 2 deletions src/NHibernate/Engine/Cascade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ public void CascadeOn(IEntityPersister persister, object parent, object anything

IType[] types = persister.PropertyTypes;
CascadeStyle[] cascadeStyles = persister.PropertyCascadeStyles;
bool hasUninitializedLazyProperties = persister.HasUninitializedLazyProperties(parent);
var uninitializedLazyProperties = persister.GetUninitializedLazyProperties(parent);
for (int i = 0; i < types.Length; i++)
{
CascadeStyle style = cascadeStyles[i];
string propertyName = persister.PropertyNames[i];
if (hasUninitializedLazyProperties && persister.PropertyLaziness[i] && !action.PerformOnLazyProperty)
if (uninitializedLazyProperties.Contains(propertyName) && persister.PropertyLaziness[i] && !action.PerformOnLazyProperty)
{
//do nothing to avoid a lazy property initialization
continue;
Expand Down
Loading