Skip to content

NH-3925 - Entity with version not detected as transient when calling SaveOrUpdate and using custom Id generator class #1354

@nhibernate-bot

Description

@nhibernate-bot

codekaizen created an issue — 8th December 2016, 0:03:31:

When an entity has a Version property and uses a custom Id generator which allows assigned ids to transient instances and SaveOrUpdate is called on the entity, a StaleObjectStateException exception is thrown during Session.Flush().

The reason for this is that the fix for NH-3505 short circuits the version check and only decides if the entity is transient in AbstractEntityPersister.IsTransient if it is using the built in "Assigned" generator. This breaks previously supported scenarios allowing assigned ids with custom generators.


codekaizen added a comment — 8th December 2016, 3:26:38:

This can be resolved by adding unsaved-value="undefined" to the id property in the mapping. It would be nice to have some validation for this if using a generator which permits assigned values, but that looks too complex for this corner case.


Oskar Berggren added a comment — 10th December 2016, 16:05:38:

Can you elaborate on the use case for such a generator?

I was going to suggest that perhaps there could be an interface "IAssigned" instead of checking the exact type, but then... it's not really that "the custom generator allows assigned ids" is it? Just to confirm, would "an entity type where some instances are added with an application-assigned identifier, while other instances should be assigned an identifier automatically using the configured identity generator" be a more accurate description? So it's not really about the generator being of any specific kind, more in how it's used? (And preferably about making sure automatic values cannot coincide with manually assigned values.)

If the latter description above is more accurate, then this really has nothing to do with custom id generators, but just about trying to assign some identifiers despite having an automatic identifier of any kind configured. Then it seems like the solution would be setting the unsaved-value in the mappings as you mention. Any idea for how to implement the "validation" you mention? I might be missing something but I don't see what parameters NH can look at to determine the suspicious case.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions