ISession.Replicate() should attempt an identity insert

Description

See HHH-2716 for the Hibernate issue (http://opensource.atlassian.com/projects/hibernate/browse/HHH-2716).

When my application first runs, I need to do an initial population of the database. For this, I have to ensure that the identifier column is the exact value I need for each persistent object. To do this, I guess I should use Replicate() to force a particular identity value to be inserted. Unfortunately, Replicate() seems to act the same as Save when the ID column is an identity - i.e. it doesn't send the ID column value to the database.

For example, I have a persistent object (BusinessRegion - ID 1, Name "Europe"). When using replicate, I expect the following query:

INSERT INTO BusinessRegion
(ID, Name)
VALUES (1, 'Europe')

Instead, I get:

INSERT INTO BusinessRegion
(Name)
VALUES ('Europe')

Before running this query, I can send "SET IDENTITY_INSERT ____ ON" to SQL, which should allow me to insert the identity column.

Environment

None

Attachments

2

Activity

Show:

Sukh 
June 8, 2009 at 10:24 AM
(edited)

I've added a test that demonstrates the failure to insert an identity column on replicate.

I've added the approriate properties and methods to dialect, and overridden them on MsSql2005Dialect (see SupportsIdentityInsertOnReplicate, GetStartIdentityInsertString(string tableName) and GetEndIdentityInsertString(string tableName) ).

I've also modified DefaultReplicateEventListener to always generate the key when replicating (I think to match the documentation, this should actually fail for dialects that don't support identity insertion??)

Now, all I need to do is call GetStartIdentityInsertString() and GetEndIdentityInsertString() before any identity inserts when replicating only. Any tips as to how I should do this?

Fabio Maulo 
June 1, 2009 at 3:35 PM

mmmm... no tests, nothing about "SET IDENTITY_INSERT ON", nothing about the others PostInsertIdentifier, nothing about applicability of the same solution with others RDBMS....

Try to analyze it a bit more deeper.
For example:

  • The dialect should have: public virtual bool SupportsIdentityOnReplicate

  • The dialect should have: public virtual string IdentityOverrideEnable
    and so on...

Sukh 
June 1, 2009 at 4:07 AM

This patch forces the key to be passed to the database when the Replicate() method is used. This matches what the documentation says should happen: "Persist the state of the given detached instance, reusing the current identifier value. "

Obviously this will fail using an identity column, but with SQL Server at least you can use "SET IDENTITY_INSERT ON" to override the default identity generation behaviour.

Sukh 
June 1, 2009 at 2:37 AM

It's a tad bit too late for that. Not quite in the mood to change 200-odd tables/classes/mappings for that

Fabio Maulo 
May 30, 2009 at 4:52 PM

There is a more easy solution.
Don't use identity and identity-style generators!!

Details

Assignee

Reporter

Components

Affects versions

Priority

Who's Looking?

Open Who's Looking?
Created May 29, 2009 at 8:38 AM
Updated October 12, 2017 at 1:49 PM
Who's Looking?