Skip to content

Commit 55f01c6

Browse files
authoredFeb 21, 2022
Fix dynamic insert for trigger-identity id generator (nhibernate#3013)
Fixes nhibernate#1062
1 parent e85c935 commit 55f01c6

File tree

6 files changed

+125
-2
lines changed

6 files changed

+125
-2
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using NHibernate.Dialect;
12+
using NUnit.Framework;
13+
14+
namespace NHibernate.Test.NHSpecificTest.GH1062
15+
{
16+
using System.Threading.Tasks;
17+
//NH-1893
18+
[TestFixture]
19+
public class TriggerIdentityDinamicInsertFixtureAsync : BugTestCase
20+
{
21+
protected override bool AppliesTo(Dialect.Dialect dialect)
22+
{
23+
return dialect is Oracle8iDialect;
24+
}
25+
26+
[Test]
27+
public async Task CanSaveEnityAsync()
28+
{
29+
using (var session = OpenSession())
30+
using (var tran = session.BeginTransaction())
31+
{
32+
var e = new MyEntity { Name = "entity-1" };
33+
await (session.SaveAsync(e));
34+
35+
Assert.AreEqual(1, e.Id, "id not generated through forced insertion");
36+
37+
await (session.DeleteAsync(e));
38+
await (tran.CommitAsync());
39+
session.Close();
40+
}
41+
}
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace NHibernate.Test.NHSpecificTest.GH1062
4+
{
5+
public class MyEntity
6+
{
7+
public virtual int Id { get; protected set; }
8+
9+
public virtual string Name { get; set; }
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using NHibernate.Dialect;
2+
using NUnit.Framework;
3+
4+
namespace NHibernate.Test.NHSpecificTest.GH1062
5+
{
6+
//NH-1893
7+
[TestFixture]
8+
public class TriggerIdentityDinamicInsertFixture : BugTestCase
9+
{
10+
protected override bool AppliesTo(Dialect.Dialect dialect)
11+
{
12+
return dialect is Oracle8iDialect;
13+
}
14+
15+
[Test]
16+
public void CanSaveEnity()
17+
{
18+
using (var session = OpenSession())
19+
using (var tran = session.BeginTransaction())
20+
{
21+
var e = new MyEntity { Name = "entity-1" };
22+
session.Save(e);
23+
24+
Assert.AreEqual(1, e.Id, "id not generated through forced insertion");
25+
26+
session.Delete(e);
27+
tran.Commit();
28+
session.Close();
29+
}
30+
}
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
3+
assembly="NHibernate.Test"
4+
namespace="NHibernate.Test.NHSpecificTest.GH1062"
5+
default-access="backfield">
6+
7+
<class name="MyEntity" table="my_entity" dynamic-insert="true">
8+
<id name="Id">
9+
<generator class="trigger-identity"/>
10+
</id>
11+
<property name="Name"/>
12+
</class>
13+
14+
<database-object>
15+
<create>
16+
<![CDATA[CREATE SEQUENCE NH_SEQ START WITH 1 CACHE 20]]>
17+
</create>
18+
<drop>
19+
<![CDATA[DROP SEQUENCE NH_SEQ]]>
20+
</drop>
21+
</database-object>
22+
23+
<database-object>
24+
<create>
25+
<![CDATA[CREATE OR REPLACE TRIGGER T_BI_my_entity
26+
BEFORE INSERT ON my_entity
27+
FOR EACH ROW
28+
BEGIN
29+
select NH_SEQ.nextval into :new.ID from DUAL;
30+
END;]]>
31+
</create>
32+
<drop>
33+
<![CDATA[DROP TRIGGER T_BI_my_entity]]>
34+
</drop>
35+
</database-object>
36+
37+
</hibernate-mapping>

‎src/NHibernate/Async/Persister/Entity/AbstractEntityPersister.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,7 @@ public async Task<object> InsertAsync(object[] fields, object obj, ISessionImple
10251025
{
10261026
// For the case of dynamic-insert="true", we need to generate the INSERT SQL
10271027
bool[] notNull = GetPropertiesToInsert(fields);
1028-
id = await (InsertAsync(fields, notNull, GenerateInsertString(true, notNull), obj, session, cancellationToken)).ConfigureAwait(false);
1028+
id = await (InsertAsync(fields, notNull, GenerateIdentityInsertString(notNull), obj, session, cancellationToken)).ConfigureAwait(false);
10291029
for (int j = 1; j < span; j++)
10301030
{
10311031
await (InsertAsync(id, fields, notNull, j, GenerateInsertString(notNull, j), obj, session, cancellationToken)).ConfigureAwait(false);

‎src/NHibernate/Persister/Entity/AbstractEntityPersister.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3494,7 +3494,7 @@ public object Insert(object[] fields, object obj, ISessionImplementor session)
34943494
{
34953495
// For the case of dynamic-insert="true", we need to generate the INSERT SQL
34963496
bool[] notNull = GetPropertiesToInsert(fields);
3497-
id = Insert(fields, notNull, GenerateInsertString(true, notNull), obj, session);
3497+
id = Insert(fields, notNull, GenerateIdentityInsertString(notNull), obj, session);
34983498
for (int j = 1; j < span; j++)
34993499
{
35003500
Insert(id, fields, notNull, j, GenerateInsertString(notNull, j), obj, session);

0 commit comments

Comments
 (0)