Skip to content

Commit 17eccc3

Browse files
committed
fix impl of EntityManager.getProperties() to comply with Javadoc
1 parent 52c6583 commit 17eccc3

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@
8686
import static java.lang.Boolean.parseBoolean;
8787
import static java.lang.Integer.parseInt;
8888
import static java.lang.System.currentTimeMillis;
89-
import static java.util.Collections.unmodifiableMap;
9089
import static org.hibernate.CacheMode.fromJpaModes;
9190
import static org.hibernate.Timeouts.WAIT_FOREVER_MILLI;
9291
import static org.hibernate.cfg.AvailableSettings.CRITERIA_COPY_TREE;
@@ -2584,13 +2583,14 @@ public LockModeType getLockMode(Object entity) {
25842583
public void setProperty(String propertyName, Object value) {
25852584
checkOpen();
25862585
if ( value instanceof Serializable ) {
2587-
if ( propertyName != null ) { // store property for future reference:
2586+
if ( propertyName != null ) {
2587+
// store property for future reference
25882588
if ( properties == null ) {
2589-
properties = computeCurrentProperties();
2589+
properties = getInitialProperties();
25902590
}
25912591
properties.put( propertyName, value );
2592-
// now actually update the setting
2593-
// if it's one that affects this Session
2592+
// now actually update the setting if
2593+
// it's one that affects this Session
25942594
interpretProperty( propertyName, value );
25952595
}
25962596
else {
@@ -2662,7 +2662,7 @@ private void interpretProperty(String propertyName, Object value) {
26622662
}
26632663
}
26642664

2665-
private Map<String, Object> computeCurrentProperties() {
2665+
private Map<String, Object> getInitialProperties() {
26662666
final var map = new HashMap<>( getDefaultProperties() );
26672667
//The FLUSH_MODE is always set at Session creation time,
26682668
//so it needs special treatment to not eagerly initialize this Map:
@@ -2672,10 +2672,15 @@ private Map<String, Object> computeCurrentProperties() {
26722672

26732673
@Override
26742674
public Map<String, Object> getProperties() {
2675-
if ( properties == null ) {
2676-
properties = computeCurrentProperties();
2677-
}
2678-
return unmodifiableMap( properties );
2675+
// EntityManager Javadoc implies that the
2676+
// returned map should be a mutable copy,
2677+
// not an unmodifiable map. There's no
2678+
// good reason to cache the initial
2679+
// properties, since we have to copy them
2680+
// each time this method is called.
2681+
return properties == null
2682+
? getInitialProperties()
2683+
: new HashMap<>( properties );
26792684
}
26802685

26812686
@Override

hibernate-core/src/test/java/org/hibernate/orm/test/jpa/EntityManagerTest.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -307,13 +307,11 @@ public void testGet() throws Exception {
307307
@Test
308308
public void testGetProperties() {
309309
inEntityManager( entityManager -> {
310-
Map<String, Object> properties = entityManager.getProperties();
311-
assertNotNull( properties );
312-
assertThrows(
313-
UnsupportedOperationException.class,
314-
() -> properties.put( "foo", "bar" )
315-
);
316-
assertTrue( properties.containsKey( HibernateHints.HINT_FLUSH_MODE ) );
310+
assertNotNull( entityManager.getProperties() );
311+
assertTrue( entityManager.getProperties().containsKey( HibernateHints.HINT_FLUSH_MODE ) );
312+
// according to Javadoc, getProperties() returns mutable copy
313+
entityManager.getProperties().put( "foo", "bar" );
314+
assertFalse( entityManager.getProperties().containsKey( "foo" ) );
317315
} );
318316
}
319317

0 commit comments

Comments
 (0)