ConcurrentModificationException in EntityPrinter.toString(EntityPrinter.java:104) with Log Level DEBUG or finer

Description

When the log level for hibernate is set to DEBUG (or FINE in Wildfly), hibernate may exhibit ConcurrentModificationExceptions when trying to print entities in EntityPrinter.toString(EntityPrinter.java:104).

In order to exhibit this behavior certain requirements must be fulfilld (see test case for example):

  1. Log Level set to DEBUG or finer

  2. hibernate property: hibernate.jpa.compliance.proxy=true

  3. Lazy Loading must be enabled and the child class must be proxied

  4. Must load at least two parent entities

The process of getting the Exception is roughly as follows:

  1. Store 1 child entity and 2 parent entities referencing the child entity.

  2. Get a clean slate by clearing the PersistenceContext

  3. Load the 2 Parent Entities with the child entity being lazily loaded as a proxy

  4. commit or flush the transaction

While printing the first parent entity, the child entity will be initialized and added to the "EntitiesByKey" Map of the Persistence Context. Then iteration will then proceed to the next parent entity and notice that the HashMap has been modified and throw the ConcurrentModificationException.

See test case for more information.

Probably a duplicate of HHH-2218, just including a test case.

Environment

Hibernate: 5.3.6 (production), 5.4.0 (Test Case)
OS: Windows 10
JDK: AdoptOpenJDK 1.8.0_202-b08
DB: Oracle (production) and h2 (Test)

Status

Assignee

Unassigned

Reporter

Christopher Czyba

Fix versions

None

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

5.3.6
5.4.0

Priority

Major