Skip to content

Fix dynamic insert for trigger-identity id generator #3013

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
Feb 21, 2022
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
43 changes: 43 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH1062/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//------------------------------------------------------------------------------
// <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 NHibernate.Dialect;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH1062
{
using System.Threading.Tasks;
//NH-1893
[TestFixture]
public class TriggerIdentityDinamicInsertFixtureAsync : BugTestCase
{
protected override bool AppliesTo(Dialect.Dialect dialect)
{
return dialect is Oracle8iDialect;
}

[Test]
public async Task CanSaveEnityAsync()
{
using (var session = OpenSession())
using (var tran = session.BeginTransaction())
{
var e = new MyEntity { Name = "entity-1" };
await (session.SaveAsync(e));

Assert.AreEqual(1, e.Id, "id not generated through forced insertion");

await (session.DeleteAsync(e));
await (tran.CommitAsync());
session.Close();
}
}
}
}
11 changes: 11 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1062/Entity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace NHibernate.Test.NHSpecificTest.GH1062
{
public class MyEntity
{
public virtual int Id { get; protected set; }

public virtual string Name { get; set; }
}
}
32 changes: 32 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1062/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using NHibernate.Dialect;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH1062
{
//NH-1893
[TestFixture]
public class TriggerIdentityDinamicInsertFixture : BugTestCase
{
protected override bool AppliesTo(Dialect.Dialect dialect)
{
return dialect is Oracle8iDialect;
}

[Test]
public void CanSaveEnity()
{
using (var session = OpenSession())
using (var tran = session.BeginTransaction())
{
var e = new MyEntity { Name = "entity-1" };
session.Save(e);

Assert.AreEqual(1, e.Id, "id not generated through forced insertion");

session.Delete(e);
tran.Commit();
session.Close();
}
}
}
}
37 changes: 37 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1062/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="NHibernate.Test"
namespace="NHibernate.Test.NHSpecificTest.GH1062"
default-access="backfield">

<class name="MyEntity" table="my_entity" dynamic-insert="true">
<id name="Id">
<generator class="trigger-identity"/>
</id>
<property name="Name"/>
</class>

<database-object>
<create>
<![CDATA[CREATE SEQUENCE NH_SEQ START WITH 1 CACHE 20]]>
</create>
<drop>
<![CDATA[DROP SEQUENCE NH_SEQ]]>
</drop>
</database-object>

<database-object>
<create>
<![CDATA[CREATE OR REPLACE TRIGGER T_BI_my_entity
BEFORE INSERT ON my_entity
FOR EACH ROW
BEGIN
select NH_SEQ.nextval into :new.ID from DUAL;
END;]]>
</create>
<drop>
<![CDATA[DROP TRIGGER T_BI_my_entity]]>
</drop>
</database-object>

</hibernate-mapping>
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,7 @@ public async Task<object> InsertAsync(object[] fields, object obj, ISessionImple
{
// For the case of dynamic-insert="true", we need to generate the INSERT SQL
bool[] notNull = GetPropertiesToInsert(fields);
id = await (InsertAsync(fields, notNull, GenerateInsertString(true, notNull), obj, session, cancellationToken)).ConfigureAwait(false);
id = await (InsertAsync(fields, notNull, GenerateIdentityInsertString(notNull), obj, session, cancellationToken)).ConfigureAwait(false);
for (int j = 1; j < span; j++)
{
await (InsertAsync(id, fields, notNull, j, GenerateInsertString(notNull, j), obj, session, cancellationToken)).ConfigureAwait(false);
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/Persister/Entity/AbstractEntityPersister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3494,7 +3494,7 @@ public object Insert(object[] fields, object obj, ISessionImplementor session)
{
// For the case of dynamic-insert="true", we need to generate the INSERT SQL
bool[] notNull = GetPropertiesToInsert(fields);
id = Insert(fields, notNull, GenerateInsertString(true, notNull), obj, session);
id = Insert(fields, notNull, GenerateIdentityInsertString(notNull), obj, session);
for (int j = 1; j < span; j++)
{
Insert(id, fields, notNull, j, GenerateInsertString(notNull, j), obj, session);
Expand Down