Home > Net >  Nhibernate PostgreSQL dialect does not use "returning" in INSERT statement
Nhibernate PostgreSQL dialect does not use "returning" in INSERT statement

Time:02-04

I have PG table, where PK is populated by trigger. In pgAdmin it works well. NH mappings is:

<class name="Dokument" schema="srk" table="`Dokumenty`" lazy="true" dynamic-update="true" dynamic-insert="true" optimistic-lock="dirty">
  <id name="IdDHash" column="`IdDHash`" type="Int32" generator="trigger-identity" />

I tried to create my testing NH dialect in order to ensure, that NH adds the "returning " keyword to the SQL insert:

namespace NHibernate.Dialect
{
    public class SzgPostgeSQLDialect : PostgreSQLDialect
    {
        public override SqlString AddIdentifierOutParameterToInsert(SqlString insertString, string identifierColumnName, string parameterName)
        {
            SqlString pom = base.AddIdentifierOutParameterToInsert(insertString, identifierColumnName, parameterName);

            return insertString.Append(" returning ", identifierColumnName);
        }

I set a breakpoint here and I am sure that the insert command is modified well:

INSERT INTO srk."Dokumenty" ("KodDSkup", "Cesta", "Nazev") VALUES (?, ?, ?) returning "IdDHash"

But when I save the object then I get exception and I see that NH used the "unmodified INSERT" statement:

NHibernate: INSERT INTO srk."Dokumenty" ("KodDSkup", "Cesta", "Nazev") VALUES (:p0, :p1, :p2);:p0 = 1 [Type: Int32 (0:0:0)], :p1 = '123' [Type: String (0:0:0)], :p2 = 'Test1' [Type: String (0:0:0)], :nhIdOutParam = NULL [Type: Int32 (0:0:0)]

It seems that NHibernate for PG does not use the modified insert command.

C#, VS Professional 2019, Npgsql 5.0.7 and NHibernate 5.3.9 by NuGet, PostgreSQL 12.8 compiled by Visual C build 1914 64-bit.

What do I wrong (or is there a bug in original PostgreSQLDialect dialect)?

Thank you for your hints

ID

CodePudding user response:

trigger-identity generator is not supported by dynamic insert dynamic-insert="true". Related bug report: https://github.com/nhibernate/nhibernate-core/issues/1062

So just disable dynamic insert for your entity and it should work (dynamic-insert="false").

  •  Tags:  
  • Related