| When Hibernate is used with JPA it inspects @Immutable entity during flushing. Steps to reproduce 1. Configure persistence.xml 2. Configure @Immutable entity 3. Configure log4j.logger.org.hibernate=TRACE 4. Load @Immutable entity from DB 5. Perform flush or commit transaction Result is visible in log
TRACE org.hibernate.internal.SessionImpl - SessionImpl#beforeTransactionCompletion()
TRACE org.hibernate.internal.SessionImpl - Automatically flushing session
TRACE org.hibernate.event.internal.AbstractFlushingEventListener - Flushing session
DEBUG org.hibernate.event.internal.AbstractFlushingEventListener - Processing flush-time cascades
TRACE org.hibernate.engine.internal.Cascade - Processing cascade ACTION_PERSIST_ON_FLUSH for: com.example.jpa.Event
This creates performance degradation especially if @Immutable entity has @Immutable @OneToMany collection and Processing flush-time cascades occurs for every element. How to map an immutable entity with JPA and Hibernate says The reason why Hibernate does not track @Immutabale entity modifications is because the entity is loaded in read-only mode, hence the detachedState or hydratedState is never stored in the currently running Persistence Context. Issue might be related to HHH-4993 Issue exists on 5.3 and 5.4.8.Final Issue does not happen with plain Hibernate because of code I attached example where problem is visible. |