Skip to content

Commit 68e1dff

Browse files
Narosvladmihalcea
authored andcommitted
HHH-11412 - EntityManager/Session setProperty should permit custom properties
1 parent 6b2176a commit 68e1dff

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3568,13 +3568,13 @@ public LockModeType getLockMode(Object entity) {
35683568
public void setProperty(String propertyName, Object value) {
35693569
checkOpen();
35703570

3571-
if ( ENTITY_MANAGER_SPECIFIC_PROPERTIES.contains( propertyName ) ) {
3572-
properties.put( propertyName, value );
3573-
applyProperties();
3574-
}
3575-
else {
3576-
log.debugf( "Trying to set a property which is not supported on entity manager level" );
3571+
if ( !( value instanceof Serializable ) ) {
3572+
log.warnf( "Property '" + propertyName + "' is not serializable, value won't be set." );
3573+
return;
35773574
}
3575+
3576+
properties.put( propertyName, value );
3577+
applyProperties();
35783578
}
35793579

35803580
@Override

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,33 @@ public void testSetProperty() throws Exception {
376376
em.close();
377377
}
378378

379+
@Test
380+
public void testSetAndGetUnserializableProperty() throws Exception {
381+
EntityManager em = getOrCreateEntityManager();
382+
try {
383+
MyObject object = new MyObject();
384+
object.value = 5;
385+
em.setProperty( "MyObject", object );
386+
assertFalse( em.getProperties().keySet().contains( "MyObject" ) );
387+
}
388+
finally {
389+
em.close();
390+
}
391+
}
392+
393+
@Test
394+
public void testSetAndGetSerializedProperty() throws Exception {
395+
EntityManager em = getOrCreateEntityManager();
396+
try {
397+
em.setProperty( "MyObject", "Test123" );
398+
assertTrue( em.getProperties().keySet().contains( "MyObject" ) );
399+
assertEquals( "Test123", em.getProperties().get( "MyObject" ) );
400+
}
401+
finally {
402+
em.close();
403+
}
404+
}
405+
379406
@Test
380407
public void testPersistExisting() throws Exception {
381408
EntityManager em = getOrCreateEntityManager();
@@ -460,4 +487,7 @@ public void testEntityNotFoundException() throws Exception {
460487
}
461488
}
462489

490+
private static class MyObject {
491+
public int value;
492+
}
463493
}

0 commit comments

Comments
 (0)