From 68e1dfffd255b0392bedbf4f4bb93a3c02620d0f Mon Sep 17 00:00:00 2001 From: Chris Cranford Date: Fri, 3 Feb 2017 13:25:11 -0500 Subject: [PATCH] HHH-11412 - EntityManager/Session setProperty should permit custom properties --- .../org/hibernate/internal/SessionImpl.java | 12 ++++---- .../hibernate/jpa/test/EntityManagerTest.java | 30 +++++++++++++++++++ 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java index 564df69b6c..41184982f6 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java @@ -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 diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/EntityManagerTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/EntityManagerTest.java index 5bd5f7358c..c01a040bd3 100755 --- a/hibernate-core/src/test/java/org/hibernate/jpa/test/EntityManagerTest.java +++ b/hibernate-core/src/test/java/org/hibernate/jpa/test/EntityManagerTest.java @@ -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; + } }