HHH-11412 - EntityManager/Session setProperty should permit custom properties

This commit is contained in:
Chris Cranford 2017-02-03 13:25:11 -05:00 committed by Vlad Mihalcea
parent 6b2176ab83
commit 68e1dfffd2
2 changed files with 36 additions and 6 deletions

View File

@ -3568,13 +3568,13 @@ public final class SessionImpl
public void setProperty(String propertyName, Object value) {
checkOpen();
if ( ENTITY_MANAGER_SPECIFIC_PROPERTIES.contains( propertyName ) ) {
properties.put( propertyName, value );
applyProperties();
}
else {
log.debugf( "Trying to set a property which is not supported on entity manager level" );
if ( !( value instanceof Serializable ) ) {
log.warnf( "Property '" + propertyName + "' is not serializable, value won't be set." );
return;
}
properties.put( propertyName, value );
applyProperties();
}
@Override

View File

@ -376,6 +376,33 @@ public class EntityManagerTest extends BaseEntityManagerFunctionalTestCase {
em.close();
}
@Test
public void testSetAndGetUnserializableProperty() throws Exception {
EntityManager em = getOrCreateEntityManager();
try {
MyObject object = new MyObject();
object.value = 5;
em.setProperty( "MyObject", object );
assertFalse( em.getProperties().keySet().contains( "MyObject" ) );
}
finally {
em.close();
}
}
@Test
public void testSetAndGetSerializedProperty() throws Exception {
EntityManager em = getOrCreateEntityManager();
try {
em.setProperty( "MyObject", "Test123" );
assertTrue( em.getProperties().keySet().contains( "MyObject" ) );
assertEquals( "Test123", em.getProperties().get( "MyObject" ) );
}
finally {
em.close();
}
}
@Test
public void testPersistExisting() throws Exception {
EntityManager em = getOrCreateEntityManager();
@ -460,4 +487,7 @@ public class EntityManagerTest extends BaseEntityManagerFunctionalTestCase {
}
}
private static class MyObject {
public int value;
}
}