Hibernate 5.4 setting default value in primary key throws error

Description

Setting a default value for primary key throws an error in 5.4. It used to work fine in 3.5. We set default value to avoid dealing with null when checking if the object is new.

Error thrown is:
ucee.runtime.exp.NativeException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [ENTITYNAME#] at org.hibernate.internal.ExceptionConverterImpl.wrapStaleStateException(ExceptionConverterImpl.java:223) at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:93) at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181) at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:188) at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1366) at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1349) at org.lucee.extension.orm.hibernate.HibernateORMSession._flush(HibernateORMSession.java:225) at org.lucee.extension.orm.hibernate.HibernateORMSession.flush(HibernateORMSession.java:218) at org.lucee.extension.orm.hibernate.HibernateORMSession.flush(HibernateORMSession.java:213) at lucee.runtime.functions.orm.ORMFlush.call(ORMFlush.java:32) at lucee.runtime.functions.orm.ORMFlush.call(ORMFlush.java:28)

Environment

None

Attachments

1
  • 14 Mar 2022, 06:00 pm

Activity

Show:

Zac Spitzer 7 June 2023 at 07:41

As per the updated test case, the same problem happens when not updating the PK.

says ACF does the same and google says a lot of people have hit this problem,

Reckon it’s a WONTFIX, isn’t half the idea that ORM does all these concurrency checkes between session

Michael Born @ Ortus 7 June 2023 at 03:24

This is an interesting one. I’ve seen similar issues when changing the entity state inside an event listener. I believe Hibernate versions the entity and checks the state before committing (flushing) its own changes to the database.

The bottom line is that the primary key default value is being set on the entity in a non-Hibernate-friendly fashion:

  1. Perhaps it is being set in the entity in a way that Hibernate cannot detect. This may need to go through the CFCSetter class as the Hibernate Setter method for that primary key. (Hibernate seems to only use the custom Setter methods when setting primary key values on an entity)

  2. Perhaps the primary key is being updated after load. This is a no-no in Hibernate land, as Hibernate can’t identify an entity after the identifier changes.

Or perhaps there is actually multiple sessions flushing concurrently. This seems far-fetched, but hey… there’s a lot of code in Lucee and the Hibernate extension, a fair number of which touches/opens the session.

It’s also worth asking why in the world you would set a default on a primary key. To me, the following are the same:

if( isNull( entity.getID() ) ){ /* entity is new! */ } if( entity.getId() == 1 ){ /* entity is new! */ }

The first makes logical sense, the second does not. A default primary key would end up saved to the database if you’re not careful… at which point every new record will collide if the primary key is not set.

Zac Spitzer 6 June 2023 at 07:52

thoughts?

Zac Spitzer 5 June 2023 at 13:07
Edited

Won't Fix

Details

Assignee

Reporter

Priority

New Issue warning screen

Before you create a new Issue, please post to the mailing list first https://dev.lucee.org

Once the issue has been verified, one of the Lucee team will ask you to file an issue

Affects versions

Created 14 March 2022 at 18:01
Updated 8 June 2023 at 18:13
Resolved 8 June 2023 at 18:13

Flag notifications