Skip to content

Fix System.InvalidCastException when merge collection with LazyProperty #1992

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 5 commits into from
Jan 24, 2019
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
53 changes: 53 additions & 0 deletions src/NHibernate.Test/Async/LazyProperty/LazyPropertyFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@


using System.Collections;
using System.Collections.Generic;
using System.Linq;
using NHibernate.Cfg;
using NHibernate.Intercept;
Expand Down Expand Up @@ -81,6 +82,7 @@ protected override void OnTearDown()
using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
s.CreateQuery("delete from Word").ExecuteUpdate();
s.CreateQuery("delete from Book").ExecuteUpdate();
tx.Commit();
}
Expand Down Expand Up @@ -345,5 +347,56 @@ public async Task CacheShouldNotContainLazyPropertiesAsync()
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False);
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Image"), Is.False);
}

[Test]
public async Task CanMergeTransientWithLazyPropertyInCollectionAsync()
{
Book book;

using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
book = new Book
{
Name = "some name two",
Id = 3,
ALotOfText = "a lot of text two..."
};
// This should insert a new entity.
await (s.MergeAsync(book));
await (tx.CommitAsync());
}

using (var s = OpenSession())
{
book = await (s.GetAsync<Book>(3));
Assert.That(book, Is.Not.Null);
Assert.That(book.Name, Is.EqualTo("some name two"));
Assert.That(book.ALotOfText, Is.EqualTo("a lot of text two..."));

}
using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
book.Words = new List<Word>();
var word = new Word
{
Id = 2,
Parent = book,
Content = new byte[1] {0}
};

book.Words.Add(word);
await (s.MergeAsync(book));
await (tx.CommitAsync());
}

using (var s = OpenSession())
{
book = await (s.GetAsync<Book>(3));
Assert.That(book.Words.Any(), Is.True);
Assert.That(book.Words.First().Content, Is.EqualTo(new byte[1] { 0 }));
}
}
}
}
6 changes: 5 additions & 1 deletion src/NHibernate.Test/LazyProperty/Book.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace NHibernate.Test.LazyProperty
using System.Collections.Generic;

namespace NHibernate.Test.LazyProperty
{
public class Book
{
Expand All @@ -16,5 +18,7 @@ public virtual string ALotOfText
public virtual byte[] Image { get; set; }

public virtual string FieldInterceptor { get; set; }

public virtual IList<Word> Words { get; set; }
}
}
53 changes: 53 additions & 0 deletions src/NHibernate.Test/LazyProperty/LazyPropertyFixture.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using NHibernate.Cfg;
using NHibernate.Intercept;
Expand Down Expand Up @@ -68,6 +69,7 @@ protected override void OnTearDown()
using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
s.CreateQuery("delete from Word").ExecuteUpdate();
s.CreateQuery("delete from Book").ExecuteUpdate();
tx.Commit();
}
Expand Down Expand Up @@ -338,5 +340,56 @@ public void CacheShouldNotContainLazyProperties()
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False);
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Image"), Is.False);
}

[Test]
public void CanMergeTransientWithLazyPropertyInCollection()
{
Book book;

using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
book = new Book
{
Name = "some name two",
Id = 3,
ALotOfText = "a lot of text two..."
};
// This should insert a new entity.
s.Merge(book);
tx.Commit();
}

using (var s = OpenSession())
{
book = s.Get<Book>(3);
Assert.That(book, Is.Not.Null);
Assert.That(book.Name, Is.EqualTo("some name two"));
Assert.That(book.ALotOfText, Is.EqualTo("a lot of text two..."));

}
using (var s = OpenSession())
using (var tx = s.BeginTransaction())
{
book.Words = new List<Word>();
var word = new Word
{
Id = 2,
Parent = book,
Content = new byte[1] {0}
};

book.Words.Add(word);
s.Merge(book);
tx.Commit();
}

using (var s = OpenSession())
{
book = s.Get<Book>(3);
Assert.That(book.Words.Any(), Is.True);
Assert.That(book.Words.First().Content, Is.EqualTo(new byte[1] { 0 }));
}
}
}
}
12 changes: 12 additions & 0 deletions src/NHibernate.Test/LazyProperty/Mappings.hbm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@
<property name="ALotOfText" lazy="true" />
<property name="Image" lazy="true" />
<property name="FieldInterceptor" />
<bag name="Words" inverse="true" generic="true" cascade="all-delete-orphan" lazy="true" >
<key column="ParentId" />
<one-to-many class="Word" />
</bag>
</class>

<class name="Word">
<id name="Id" column="Id">
<generator class="assigned" />
</id>
<property name="Content" lazy="true" />
<many-to-one name="Parent" class="Book" column="ParentId" />
</class>

</hibernate-mapping>
9 changes: 9 additions & 0 deletions src/NHibernate.Test/LazyProperty/Word.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace NHibernate.Test.LazyProperty
{
public class Word
{
public virtual int Id { get; set; }
public virtual byte[] Content { get; set; }
public virtual Book Parent { get; set; }
}
}
21 changes: 20 additions & 1 deletion src/NHibernate/Async/Type/TypeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,25 @@ public static async Task<object[]> ReplaceAsync(object[] original, object[] targ
{
copied[i] = target[i];
}
else if (target[i] == LazyPropertyInitializer.UnfetchedProperty)
{
// Should be no need to check for target[i] == PropertyAccessStrategyBackRefImpl.UNKNOWN
// because PropertyAccessStrategyBackRefImpl.get( object ) returns
// PropertyAccessStrategyBackRefImpl.UNKNOWN, so target[i] == original[i].
//
// We know from above that original[i] != LazyPropertyInitializer.UNFETCHED_PROPERTY &&
// original[i] != PropertyAccessStrategyBackRefImpl.UNKNOWN;
// This is a case where the entity being merged has a lazy property
// that has been initialized. Copy the initialized value from original.
if (types[i].IsMutable)
{
copied[i] = types[i].DeepCopy(original[i], session.Factory);
}
else
{
copied[i] = original[i];
}
}
else
copied[i] = await (types[i].ReplaceAsync(original[i], target[i], session, owner, copyCache, foreignKeyDirection, cancellationToken)).ConfigureAwait(false);
}
Expand Down Expand Up @@ -343,4 +362,4 @@ public static async Task<int[]> FindModifiedAsync(StandardProperty[] properties,
}
}
}
}
}
21 changes: 20 additions & 1 deletion src/NHibernate/Type/TypeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,25 @@ public static object[] Replace(object[] original, object[] target, IType[] types
{
copied[i] = target[i];
}
else if (target[i] == LazyPropertyInitializer.UnfetchedProperty)
{
// Should be no need to check for target[i] == PropertyAccessStrategyBackRefImpl.UNKNOWN
// because PropertyAccessStrategyBackRefImpl.get( object ) returns
// PropertyAccessStrategyBackRefImpl.UNKNOWN, so target[i] == original[i].
//
// We know from above that original[i] != LazyPropertyInitializer.UNFETCHED_PROPERTY &&
// original[i] != PropertyAccessStrategyBackRefImpl.UNKNOWN;
// This is a case where the entity being merged has a lazy property
// that has been initialized. Copy the initialized value from original.
if (types[i].IsMutable)
{
copied[i] = types[i].DeepCopy(original[i], session.Factory);
}
else
{
copied[i] = original[i];
}
}
else
copied[i] = types[i].Replace(original[i], target[i], session, owner, copyCache, foreignKeyDirection);
}
Expand Down Expand Up @@ -342,4 +361,4 @@ public static int[] FindModified(StandardProperty[] properties,
}
}
}
}
}