openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jgras...@apache.org
Subject svn commit: r1383114 - in /openjpa/branches/2.2.x: openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/querycache/ openjpa-persistence/src/main/java/org/apache/openjpa/persistence/
Date Mon, 10 Sep 2012 21:30:51 GMT
Author: jgrassel
Date: Mon Sep 10 21:30:50 2012
New Revision: 1383114

URL: http://svn.apache.org/viewvc?rev=1383114&view=rev
Log:
OPENJPA-2261: Query SQL Cache issue with NULL parameters

Added:
    openjpa/branches/2.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/querycache/QCEntity.java
  (with props)
    openjpa/branches/2.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/querycache/TestQuerySQLCache.java
  (with props)
Modified:
    openjpa/branches/2.2.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java

Added: openjpa/branches/2.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/querycache/QCEntity.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/querycache/QCEntity.java?rev=1383114&view=auto
==============================================================================
--- openjpa/branches/2.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/querycache/QCEntity.java
(added)
+++ openjpa/branches/2.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/querycache/QCEntity.java
Mon Sep 10 21:30:50 2012
@@ -0,0 +1,75 @@
+/*
+ * 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 agreed 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.querycache;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.NamedQueries;
+import javax.persistence.NamedQuery;
+
+@Entity
+@NamedQueries({ 
+    @NamedQuery(name = "QCEntity.getByAmount", query = "SELECT o from QCEntity o WHERE o.amount=:amount")
+})
+public class QCEntity {
+    @Id
+    @Column(name = "PK")
+    private String pk;
+
+    @Column(name = "DESCRIPTION")
+    private String description;
+
+    @Column(name = "AMOUNT")
+    private Long amount;
+
+    public QCEntity() {
+        
+    }
+    
+    public QCEntity(String pk, String description, Long amount) {
+        this.pk = pk;
+        this.description = description;
+        this.amount = amount;
+    }
+    
+    public String getPk() {
+        return pk;
+    }
+
+    public void setPk(String pk) {
+        this.pk = pk;
+    }
+
+    public String getDescription() {
+        return description;
+    }
+
+    public void setDescription(String description) {
+        this.description = description;
+    }
+
+    public Long getAmount() {
+        return amount;
+    }
+
+    public void setAmount(Long amount) {
+        this.amount = amount;
+    }
+}

Propchange: openjpa/branches/2.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/querycache/QCEntity.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/branches/2.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/querycache/TestQuerySQLCache.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/querycache/TestQuerySQLCache.java?rev=1383114&view=auto
==============================================================================
--- openjpa/branches/2.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/querycache/TestQuerySQLCache.java
(added)
+++ openjpa/branches/2.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/querycache/TestQuerySQLCache.java
Mon Sep 10 21:30:50 2012
@@ -0,0 +1,222 @@
+/*
+ * 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 agreed 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.querycache;
+
+import java.util.List;
+
+import javax.persistence.EntityManager;
+import javax.persistence.Query;
+
+import org.apache.openjpa.persistence.EntityManagerImpl;
+import org.apache.openjpa.persistence.test.SQLListenerTestCase;
+
+public class TestQuerySQLCache extends SQLListenerTestCase {
+    EntityManager em;
+    
+    public void setUp() {
+        super.setUp(
+            DROP_TABLES,
+            "openjpa.jdbc.QuerySQLCache", "true",
+            "openjpa.DataCache", "false",
+            QCEntity.class
+            );
+        em = emf.createEntityManager();
+        
+        em.getTransaction().begin();
+        QCEntity qc1 = new QCEntity("pk1", "description", Long.valueOf(1));
+        QCEntity qc2 = new QCEntity("pk2", "description-2", Long.valueOf(1));
+        QCEntity qc3 = new QCEntity("pk3", null, null);
+        
+        em.persist(qc1);
+        em.persist(qc2);
+        em.persist(qc3);
+        
+        em.getTransaction().commit();
+        
+        em.clear();
+    }
+    
+    public void testNullParamsWithNumericPosition01() {
+        // Verify Query SQL Cache is enabled
+        EntityManagerImpl eml = (EntityManagerImpl) em;
+        assertTrue(eml.getQuerySQLCache());
+               
+        Query q = em.createQuery("SELECT o from QCEntity o WHERE o.amount=?1");
+        
+        // Test with NULL parameter, SQL should contain a IS NULL predicate
+        resetSQL();
+        q.setParameter(1, null);
+        List resultListNull1A = q.getResultList();
+        assertTrue((getLastSQL(sql) != null) && (getLastSQL(sql).contains("IS NULL")));
+        assertNotNull(resultListNull1A);
+        assertEquals(1, resultListNull1A.size());
+        
+        resetSQL();
+        q.setParameter(1, null);
+        List resultListNull1B = q.getResultList();
+        assertTrue((getLastSQL(sql) != null) && (getLastSQL(sql).contains("IS NULL")));
+        assertNotNull(resultListNull1B);
+        assertEquals(1, resultListNull1B.size());
+        
+        // Test with non-NULL paramter, SQL should contain the = predicate
+        resetSQL();
+        q.setParameter(1, new Long(1));
+        List resultListNotNull = q.getResultList();
+        assertTrue((getLastSQL(sql) != null) && !(getLastSQL(sql).contains("IS NULL")));
+        assertNotNull(resultListNotNull);
+        assertEquals(2, resultListNotNull.size());      
+        
+        // Test again with NULL parameter, SQL should contain a IS NULL predicate
+        resetSQL();
+        q.setParameter(1, null);
+        List resultListNull2 = q.getResultList();
+        assertTrue((getLastSQL(sql) != null) && (getLastSQL(sql).contains("IS NULL")));
+        assertNotNull(resultListNull2);
+        assertEquals(1, resultListNull2.size());
+    }
+    
+    public void testNullParamsWithNumericPosition02() {
+        // Verify Query SQL Cache is enabled
+        EntityManagerImpl eml = (EntityManagerImpl) em;
+        assertTrue(eml.getQuerySQLCache());
+                      
+        // Test with NULL parameter, SQL should contain a IS NULL predicate
+        resetSQL();
+        Query q1 = em.createQuery("SELECT o from QCEntity o WHERE o.amount=?1");
+        q1.setParameter(1, null);
+        List resultListNull1A = q1.getResultList();
+        assertTrue((getLastSQL(sql) != null) && (getLastSQL(sql).contains("IS NULL")));
+        assertNotNull(resultListNull1A);
+        assertEquals(1, resultListNull1A.size());
+        
+        resetSQL();
+        Query q2 = em.createQuery("SELECT o from QCEntity o WHERE o.amount=?1");
+        q2.setParameter(1, null);
+        List resultListNull1B = q2.getResultList();
+        assertTrue((getLastSQL(sql) != null) && (getLastSQL(sql).contains("IS NULL")));
+        assertNotNull(resultListNull1B);
+        assertEquals(1, resultListNull1B.size());
+        
+        // Test with non-NULL paramter, SQL should contain the = predicate
+        resetSQL();
+        Query q3 = em.createQuery("SELECT o from QCEntity o WHERE o.amount=?1");
+        q3.setParameter(1, new Long(1));
+        List resultListNotNull = q3.getResultList();
+        assertTrue((getLastSQL(sql) != null) && !(getLastSQL(sql).contains("IS NULL")));
+        assertNotNull(resultListNotNull);
+        assertEquals(2, resultListNotNull.size());      
+        
+        // Test again with NULL parameter, SQL should contain a IS NULL predicate
+        resetSQL();
+        Query q4 = em.createQuery("SELECT o from QCEntity o WHERE o.amount=?1");
+        q4.setParameter(1, null);
+        List resultListNull2 = q4.getResultList();
+        assertTrue((getLastSQL(sql) != null) && (getLastSQL(sql).contains("IS NULL")));
+        assertNotNull(resultListNull2);
+        assertEquals(1, resultListNull2.size());
+    }
+    
+    public void testNullParamsWithNamedQuery01() {
+        // Verify Query SQL Cache is enabled
+        EntityManagerImpl eml = (EntityManagerImpl) em;
+        assertTrue(eml.getQuerySQLCache());        
+        
+        Query q = em.createNamedQuery("QCEntity.getByAmount");
+        
+        resetSQL();       
+        q.setParameter("amount", null);
+        List resultListNull1A = q.getResultList();
+        assertTrue((getLastSQL(sql) != null) && (getLastSQL(sql).contains("IS NULL")));
+        assertNotNull(resultListNull1A);
+        assertEquals(1, resultListNull1A.size());
+        em.clear();
+        
+        // Test with NULL parameter, SQL should contain a IS NULL predicate
+        resetSQL();
+        q.setParameter("amount", null);
+        List resultListNull1B = q.getResultList();
+        assertTrue((getLastSQL(sql) != null) && (getLastSQL(sql).contains("IS NULL")));
+        assertNotNull(resultListNull1B);
+        assertEquals(1, resultListNull1B.size());
+        em.clear();
+        
+        // Test with non-NULL parameter, SQL should contain the = predicate
+        resetSQL();
+        q.setParameter("amount", new Long(1));
+        List resultListNotNull = q.getResultList();
+        assertTrue((getLastSQL(sql) != null) && !(getLastSQL(sql).contains("IS NULL")));
+        assertNotNull(resultListNotNull);
+        assertEquals(2, resultListNotNull.size());
+        em.clear();
+        
+        // Test again with NULL parameter, SQL should contain a IS NULL predicate
+        resetSQL();
+        q.setParameter("amount", null);
+        List resultListNull2 = q.getResultList();
+        assertTrue((getLastSQL(sql) != null) && (getLastSQL(sql).contains("IS NULL")));
+        assertNotNull(resultListNull2);
+        assertEquals(1, resultListNull2.size());
+        em.clear();
+    }
+    
+    public void testNullParamsWithNamedQuery02() {
+        // Verify Query SQL Cache is enabled
+        EntityManagerImpl eml = (EntityManagerImpl) em;
+        assertTrue(eml.getQuerySQLCache());        
+        
+        resetSQL();
+        Query q1A = em.createNamedQuery("QCEntity.getByAmount");
+        q1A.setParameter("amount", null);
+        List resultListNull1A = q1A.getResultList();
+        assertTrue((getLastSQL(sql) != null) && (getLastSQL(sql).contains("IS NULL")));
+        assertNotNull(resultListNull1A);
+        assertEquals(1, resultListNull1A.size());
+        em.clear();
+        
+        // Test with NULL parameter, SQL should contain a IS NULL predicate
+        resetSQL();
+        Query q1B = em.createNamedQuery("QCEntity.getByAmount");
+        q1B.setParameter("amount", null);
+        List resultListNull1B = q1B.getResultList();
+        assertTrue((getLastSQL(sql) != null) && (getLastSQL(sql).contains("IS NULL")));
+        assertNotNull(resultListNull1B);
+        assertEquals(1, resultListNull1B.size());
+        em.clear();
+        
+        // Test with non-NULL parameter, SQL should contain the = predicate
+        resetSQL();
+        Query q2 = em.createNamedQuery("QCEntity.getByAmount");
+        q2.setParameter("amount", new Long(1));
+        List resultListNotNull = q2.getResultList();
+        assertTrue((getLastSQL(sql) != null) && !(getLastSQL(sql).contains("IS NULL")));
+        assertNotNull(resultListNotNull);
+        assertEquals(2, resultListNotNull.size());
+        em.clear();
+        
+        // Test again with NULL parameter, SQL should contain a IS NULL predicate
+        resetSQL();
+        Query q3 = em.createNamedQuery("QCEntity.getByAmount");
+        q3.setParameter("amount", null);
+        List resultListNull2 = q3.getResultList();
+        assertTrue((getLastSQL(sql) != null) && (getLastSQL(sql).contains("IS NULL")));
+        assertNotNull(resultListNull2);
+        assertEquals(1, resultListNull2.size());
+        em.clear();
+    }
+}

Propchange: openjpa/branches/2.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/querycache/TestQuerySQLCache.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: openjpa/branches/2.2.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.2.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java?rev=1383114&r1=1383113&r2=1383114&view=diff
==============================================================================
--- openjpa/branches/2.2.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java
(original)
+++ openjpa/branches/2.2.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java
Mon Sep 10 21:30:50 2012
@@ -539,6 +539,15 @@ public class QueryImpl<X> extends Abstra
             }
             return false;
         }
+        
+        // Determine if the query has NULL parameters.  If so, then do not use a PreparedQuery
from the cache
+        for (Object val : params.values()) {
+            if (val == null) {
+                ignorePreparedQuery();
+                return false;
+            }
+        }
+        
         Boolean registered = cache.register(_id, _query, fetch);
         boolean alreadyCached = (registered == null);
         String lang = _query.getLanguage();



Mime
View raw message