Skip to content
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

Inserting multiple associations of the same entity fails #3489

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
18 changes: 17 additions & 1 deletion src/NHibernate.Test/Async/BulkManipulation/HQLBulkOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,21 @@ public async Task SimpleDeleteAsync()
await (tx.CommitAsync());
}
}

[Test]
public async Task InsertFromSelectWithMultipleAssociationsAsync()
{
Assume.That(TestDialect.NativeGeneratorSupportsBulkInsertion,
"The dialect does not support a native generator compatible with bulk insertion.");

using var s = OpenSession();
using var tx = s.BeginTransaction();

await (s.CreateQuery("insert into Enrolment (Course, Student)" +
" select e.Course, e.Student from Enrolment e")
.ExecuteUpdateAsync());

await (tx.CommitAsync());
}
}
}
}
8 changes: 8 additions & 0 deletions src/NHibernate.Test/BulkManipulation/Course.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace NHibernate.Test.BulkManipulation
{
public class Course
{
public virtual long CourseId { get; set; }
public virtual string Description { get; set; }
}
}
12 changes: 12 additions & 0 deletions src/NHibernate.Test/BulkManipulation/Enrolment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace NHibernate.Test.BulkManipulation
{
[Serializable]
public class Enrolment
{
public virtual long EnrolmentId { get; set; }
public virtual Student Student { get; set; }
public virtual Course Course { get; set; }
}
}
29 changes: 29 additions & 0 deletions src/NHibernate.Test/BulkManipulation/Enrolment.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0"?>
<hibernate-mapping
xmlns="urn:nhibernate-mapping-2.2"
assembly="NHibernate.Test"
namespace="NHibernate.Test.BulkManipulation">

<class name="Course">
<id name="CourseId">
<generator class="native" />
</id>
<property name="Description" />
</class>

<class name="Student">
<id name="StudentId">
<generator class="native" />
</id>
<property name="Name" />
</class>

<class name="Enrolment">
<id name="EnrolmentId">
<generator class="native" />
</id>
<many-to-one name="Student" column="StudentId" class="Student" />
<many-to-one name="Course" column="CourseId" class="Course" />
</class>

</hibernate-mapping>
18 changes: 17 additions & 1 deletion src/NHibernate.Test/BulkManipulation/HQLBulkOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,21 @@ public void SimpleDelete()
tx.Commit();
}
}

[Test]
public void InsertFromSelectWithMultipleAssociations()
{
Assume.That(TestDialect.NativeGeneratorSupportsBulkInsertion,
"The dialect does not support a native generator compatible with bulk insertion.");

using var s = OpenSession();
using var tx = s.BeginTransaction();

s.CreateQuery("insert into Enrolment (Course, Student)" +
" select e.Course, e.Student from Enrolment e")
.ExecuteUpdate();

tx.Commit();
}
}
}
}
8 changes: 8 additions & 0 deletions src/NHibernate.Test/BulkManipulation/Student.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace NHibernate.Test.BulkManipulation
{
public class Student
{
public virtual long StudentId { get; set; }
public virtual string Name { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/NHibernate/Hql/Ast/ANTLR/Tree/SelectClause.cs
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ private void RenderNonScalarIdentifiers(
}

var node = (IASTNode) e;
if (processedElements.Add(fromElement))
if (Walker.IsShallowQuery && node.Type == SqlGenerator.DOT || processedElements.Add(fromElement))
Copy link
Member

Choose a reason for hiding this comment

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

The processedElements set serves a de-duplication purpose of selected entities. But, alone, it fails accounting for associations selected in shallow queries which will be seen as having the same fromElement although being actually different entities being selected.

Shallow queries may still have duplicated entities selected and cannot be entirely ignored for the de-duplication, if we want to keep the test HqlDuplicateEntitySelectionSubQuery as currently is. (I fail to understand why this tests is valid, why should we support the queries in this test, but I did not get answers about this here. And anyway, it has been release in 5.4, so, it is legacy.)

The DOT check allows to recognize cases in shallow queries where the same fromElement may be used by different expressions selecting different things, and a such, needing to be not falsely de-duplicated.

(I thought about checking if the expressions e where "the same" (having same Path) instead when sharing a common fromElement, but that would imply building a dictionary of sets instead of processedElements. I do not consider it is worth it.)

Copy link
Contributor

Choose a reason for hiding this comment

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

I fail to understand why this tests is valid, why should we support the queries in this test

To be honest, I don't remember why I've added such test. Looking back at it, I agree that the test shouldn't be added.

Perhaps we could add a TODO for removing test HqlDuplicateEntitySelectionSubQuery in version 5.5 and remove the node.Type == SqlGenerator.DOT part from the condition.

{
combinedFromElements.Add(fromElement);
RenderNonScalarIdentifiers(fromElement, inheritedExpressions.ContainsKey(e) ? null : e, appender);
Expand Down
Loading