0

In NH is a configuration setting 'use_identifier_rollback' which is sets the id of an entity back to its default value.

This settings works with every cascade options except 'delete-orphan'. (And I know why!)

Take a look at C# Identifier Rollback

Take a look at Java Identifier Rollback

// Works with 'use_identifier_rollback' and 'cascade-option=all' 
// but not with 'cascade-option=all-delete-orphan'
Sample sample = new Sample("sample");
sample.Add(new Subsample("subsample");
int sampleId;
using(var session = sessionFactoy.OpenSession())
{
  using(var tx = session.BeginTransaction())
  {
       session.Save(sample);
       sampleId = sample.Id;
       Assert.That(sampleId, Is.GreaterThan(0));
       Assert.False(sample.IsTransient)
  } // Rollback
}
Assert.That(sample.Id, Is.EqualTo(0));
Assert.True(sample.IsTransient)

Is it bad practice to revert the id when rollbacking the save? In the java code is not comment out and works.

UPDATE: What behavior do you usually expect when you delete an entity?

// Works with 'use_identifier_rollback' and 'cascade-option=all' 
// but not with 'cascade-option=all-delete-orphan'
int sampleId; // sampleId from above
using(var session = sessionFactoy.OpenSession())
{
  using(var tx = session.BeginTransaction())
  {
       Sample sample = session.Get<Sample>(sampleId);
       Assert.That(sampleId, Is.GreaterThan(0));
       Assert.False(sample.IsTransient)
       session.Delete(sample);
       tx.Commit();
  }
}
Assert.That(sample.Id, Is.EqualTo(0));
Assert.True(sample.IsTransient)

With 'use_identifier_rollback' nhibernate sets the id to '0' or more exactly to the default of the identity. My IsTransient property depends on Id == 0

  • How do you handle entities when they becomes deleted, in case of is the entity transient or what is the Id of a deleted entity, ...?
crip
  • 145
  • 1
  • 8
  • I know I've never had a use for it. Rollback usually means that the entire unit-of-work failed, and this includes the code that instantiated the object. – Oskar Berggren Aug 30 '13 at 10:58
  • @oskar Yes, for a new entity this sounds very plausible. But what behavior do you expect when you delete an entity? Is it now transient and the id is '0'? – crip Sep 02 '13 at 06:56
  • So far I don't except to use the entity at all after it has been deleted, and therefore its id doesn't matter. Is it for some kind of post-death logging purposes? – Oskar Berggren Sep 02 '13 at 08:20

1 Answers1

0

Looks like it just hasn't been implemented yet in NHibernate. See NHibernate's issue tracker: NH-387. Log in and vote for the issue if it's important to you. You can even contribute unit tests and fixes via GitHub. Looks like someone has already done some work on this issue - there's a couple of patch files attached to the issue, but I don't know what state they're in.

Daniel Schilling
  • 4,829
  • 28
  • 60