You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by rm...@apache.org on 2021/10/21 06:09:50 UTC

[openjpa] branch master updated: OPENJPA-2882: Exception passing javax.persistence.* String values to createEntityManager(Map)

This is an automated email from the ASF dual-hosted git repository.

rmannibucau pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/openjpa.git


The following commit(s) were added to refs/heads/master by this push:
     new 30c443d  OPENJPA-2882: Exception passing javax.persistence.* String values to createEntityManager(Map)
     new 0fa0ee9  Merge pull request #82 from dazey3/OJ2882_master
30c443d is described below

commit 30c443dcc32fdc43b75431c9579f8ca415421d17
Author: Will Dazey <da...@gmail.com>
AuthorDate: Mon Oct 18 13:05:17 2021 -0500

    OPENJPA-2882: Exception passing javax.persistence.* String values to createEntityManager(Map)
    
    Signed-off-by: Will Dazey <da...@gmail.com>
---
 .../persistence/property/TestEMProperties.java     | 81 ++++++++++++++++++++++
 .../apache/openjpa/persistence/JPAProperties.java  | 14 +++-
 2 files changed, 94 insertions(+), 1 deletion(-)

diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/property/TestEMProperties.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/property/TestEMProperties.java
new file mode 100644
index 0000000..bcff7b1
--- /dev/null
+++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/property/TestEMProperties.java
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agEmployee_Last_Name to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.openjpa.persistence.property;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.persistence.EntityManager;
+
+import org.apache.openjpa.persistence.OpenJPAPersistence;
+import org.apache.openjpa.persistence.OpenJPAQuery;
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+/**
+ * <b>TestEMProperties</b> is used to test various persistence properties set through EntityManager.setProperty() API
+ * to ensure no errors are thrown.
+ */
+public class TestEMProperties extends SingleEMFTestCase {
+
+    @Override
+    public void setUp() {
+        setUp(EntityContact.class,
+              EmbeddableAddress.class,
+              DROP_TABLES, "javax.persistence.query.timeout", 23456);
+    }
+
+    public void testQueryTimeoutPropertyDefault() {
+        EntityManager em = emf.createEntityManager();
+
+        String sql = "select * from EntityContact";
+        OpenJPAQuery<?> query = OpenJPAPersistence.cast(em.createNativeQuery(sql));
+        assertEquals(23456, query.getFetchPlan().getQueryTimeout());
+
+        em.clear();
+        em.close();
+    }
+
+    public void testQueryTimeoutPropertyOnEntityManagerCreation() {
+        Map<String, Object> properties = new HashMap<String, Object>();
+        properties.put("javax.persistence.query.timeout", "12345");
+        // Setting a value of type String should convert if possible and not return an error
+        EntityManager em = emf.createEntityManager(properties);
+
+        String sql = "select * from EntityContact";
+        OpenJPAQuery<?> query = OpenJPAPersistence.cast(em.createNativeQuery(sql));
+        assertEquals(12345, query.getFetchPlan().getQueryTimeout());
+
+        em.clear();
+        em.close();
+    }
+
+    public void testQueryTimeoutPropertySetOnEntityManager() {
+        EntityManager em = emf.createEntityManager();
+
+        // Setting a value of type String should convert if possible and not return an error
+        em.setProperty("javax.persistence.query.timeout", "12345");
+
+        String sql = "select * from EntityContact";
+        OpenJPAQuery<?> query = OpenJPAPersistence.cast(em.createNativeQuery(sql));
+        assertEquals(12345, query.getFetchPlan().getQueryTimeout());
+
+        em.clear();
+        em.close();
+    }
+}
diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/JPAProperties.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/JPAProperties.java
index 89f2a79..162003c 100644
--- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/JPAProperties.java
+++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/JPAProperties.java
@@ -18,6 +18,8 @@
  */
 package org.apache.openjpa.persistence;
 
+import java.math.BigDecimal;
+import java.math.BigInteger;
 import java.util.HashMap;
 import java.util.Locale;
 import java.util.Map;
@@ -134,8 +136,18 @@ public class JPAProperties {
             } else if (value instanceof CacheStoreMode || (value instanceof String && CACHE_STORE_MODE.equals(key))) {
                 return (T)DataCacheStoreMode.valueOf(value.toString().trim().toUpperCase(Locale.ENGLISH));
             }
+
+            // If the value doesn't match the result type, attempt to convert
+            if(resultType != null && !resultType.isAssignableFrom(value.getClass())) {
+                if (value instanceof String) {
+                    if ("null".equals(value)) {
+                        return null;
+                    }
+                    return StringUtil.parse((String) value, resultType);
+                }
+            }
         }
-        return (T)value;
+        return (T) value;
     }
 
     /**