Skip to content

Unit test for issue #1766 #1768

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

Closed
wants to merge 1 commit into from
Closed
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
80 changes: 80 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH1766/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//------------------------------------------------------------------------------
// <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.Collections.Generic;
using System.Linq;

using NUnit.Framework;
using NHibernate.Linq;

namespace NHibernate.Test.NHSpecificTest.GH1766
{
using System.Threading.Tasks;
[TestFixture]
public class FixtureAsync : BugTestCase
{
protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var parent = new A
{
Items = new List<B>
{
new B(),
new B(),
},
};

foreach (var child in parent.Items)
{
child.A = parent;
Copy link
Member

@fredericDelaporte fredericDelaporte Jun 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The index has to be set here, like the parent. When a list is mapped as inverse, no update occurs from it. It may at most cascade the update/save operation if cascade is enabled, but it will not set the appropriate parent and index.

From here:

Changes made only to the inverse end of the association are not persisted.

The documentation also implies it with:

If there is a property of the child class that maps to the index column you can use inverse="true" on the collection mapping
...
If there is no such property on the child class, the association cannot be considered truly bidirectional. That is, there is information available at one end of the association that is not available at the other end. In this case, you cannot map the collection inverse="true".

This is so because once the collection is mapped as inverse, the index must be handled from the other side.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, ok. So it was a misunderstanding on my side. Would it be possible to update/clarify the documentation for this use case and/or write a unit test that shows this behavior? I did expect the index column to always be set/updated, because it's an integral part of the list.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is also:

The non-inverse side is used to save the in-memory representation to the database.

Anyway, a consequent update is also ongoing, awaiting review (see #1761). It notably adds some more examples and guidance, including some about lists.

I do not think we need to add more.

}

session.Persist(parent);

transaction.Commit();
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.Delete("from System.Object");
transaction.Commit();
}
}

[Test]
public async Task ShouldSetListIndexForInverseListAsync()
{
using (var session = OpenSession())
using (session.BeginTransaction())
{
var result = await (session.Query<A>().ToListAsync());
Assert.That(result, Has.Count.EqualTo(1));

var root = result.Single();
Assert.That(root.Items, Is.Not.Null);
Assert.That(root.Items, Has.Count.EqualTo(2));

var expectedIndex = 0;
foreach (var child in root.Items)
{
Assert.That(child.ListIndex, Is.EqualTo(expectedIndex));
++expectedIndex;
}
}
}
}
}
18 changes: 18 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1766/Entity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;

namespace NHibernate.Test.NHSpecificTest.GH1766
{
public class A
{
public virtual Guid Id { get; set; }
public virtual IList<B> Items { get; set; }
}

public class B
{
public virtual Guid Id { get; set; }
public virtual A A { get; set; }
public virtual int ListIndex { get; set; }
}
}
68 changes: 68 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1766/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System.Collections.Generic;
using System.Linq;

using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH1766
{
[TestFixture]
public class Fixture : BugTestCase
{
protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var parent = new A
{
Items = new List<B>
{
new B(),
new B(),
},
};

foreach (var child in parent.Items)
{
child.A = parent;
}

session.Persist(parent);

transaction.Commit();
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.Delete("from System.Object");
transaction.Commit();
}
}

[Test]
public void ShouldSetListIndexForInverseList()
{
using (var session = OpenSession())
using (session.BeginTransaction())
{
var result = session.Query<A>().ToList();
Assert.That(result, Has.Count.EqualTo(1));

var root = result.Single();
Assert.That(root.Items, Is.Not.Null);
Assert.That(root.Items, Has.Count.EqualTo(2));

var expectedIndex = 0;
foreach (var child in root.Items)
{
Assert.That(child.ListIndex, Is.EqualTo(expectedIndex));
++expectedIndex;
}
}
}
}
}
20 changes: 20 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1766/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test"
namespace="NHibernate.Test.NHSpecificTest.GH1766">

<class name="A" table="a">
<id name="Id" generator="guid.comb" />
<list name="Items" generic="true" inverse="true" cascade="all">
<key column="aid" />
<list-index base="1" column="listindex" />
<one-to-many class="B"/>
</list>
</class>

<class name="B" table="b">
<id name="Id" generator="guid.comb" />
<many-to-one name="A" column="aid" />
<property name="ListIndex" column="listindex" />
</class>

</hibernate-mapping>