spring-projects / spring-data-jpa

Simplifies the development of creating a JPA-based data access layer.
https://spring.io/projects/spring-data-jpa/
Apache License 2.0
2.98k stars 1.41k forks source link

[Bug] StaleObjectStateException when search in EntityListener @PostRemove method #3501

Closed Farley-Chen closed 3 months ago

Farley-Chen commented 3 months ago

When there is more then one row in the table.

If you remove one entity, and then search in the entity listener in @PostRemove method if will fail on StaleObjectStateException.

The code sample is here.

issue.zip

schauder commented 3 months ago

JPAs behaviour is undefined when accessing the EntityManager in life cycle methods:

In general, the lifecycle method of a portable application should not invoke EntityManager or query operations, access other entity instances, or modify relationships within the same persistence context.

See: https://jakarta.ee/specifications/persistence/3.0/jakarta-persistence-spec-3.0#lifecycle-callback-methods

The behaviour depends on the underlying JPA implementation not on Spring Data.