openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hthom...@apache.org
Subject svn commit: r1580973 - in /openjpa/branches/2.3.x: ./ openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/CompareExpression.java openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestJDBCEscapeDate.java
Date Mon, 24 Mar 2014 18:29:15 GMT
Author: hthomann
Date: Mon Mar 24 18:29:15 2014
New Revision: 1580973

URL: http://svn.apache.org/r1580973
Log:
OPENJPA-2286: ArgumentException: Attempt to compare incompatible types class java.util.Date
and class org.apache.openjpa.jdbc.sql.Raw

Modified:
    openjpa/branches/2.3.x/   (props changed)
    openjpa/branches/2.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/CompareExpression.java
    openjpa/branches/2.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestJDBCEscapeDate.java

Propchange: openjpa/branches/2.3.x/
------------------------------------------------------------------------------
  Merged /openjpa/branches/2.2.x:r1580939

Modified: openjpa/branches/2.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/CompareExpression.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/CompareExpression.java?rev=1580973&r1=1580972&r2=1580973&view=diff
==============================================================================
--- openjpa/branches/2.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/CompareExpression.java
(original)
+++ openjpa/branches/2.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/CompareExpression.java
Mon Mar 24 18:29:15 2014
@@ -26,6 +26,7 @@ import org.apache.openjpa.kernel.Filters
 import org.apache.openjpa.kernel.exps.ExpressionVisitor;
 import org.apache.openjpa.lib.util.Localizer;
 import org.apache.openjpa.util.UserException;
+import org.apache.openjpa.jdbc.sql.Raw;
 
 /**
  * Compares two values.
@@ -67,10 +68,19 @@ class CompareExpression
         BinaryOpExpState bstate = (BinaryOpExpState) state;
         _val1.calculateValue(sel, ctx, bstate.state1, _val2, bstate.state2);
         _val2.calculateValue(sel, ctx, bstate.state2, _val1, bstate.state1);
-        if (!Filters.canConvert(_val1.getType(), _val2.getType(), false)
-            && !Filters.canConvert(_val2.getType(), _val1.getType(), false))
-            throw new UserException(_loc.get("cant-convert", _val1.getType(),
-                _val2.getType()));
+        Class val1Type = _val1.getType();
+        Class val2Type = _val2.getType();
+        // For purposes of the 'canConvert', when dealing with a Lit with Raw
+        // use a String type since Raw contains a String.
+        if (_val1 instanceof Lit && val1Type.isAssignableFrom(Raw.class)){
+            val1Type = String.class;
+        }
+        if (_val2 instanceof Lit && val2Type.isAssignableFrom(Raw.class)){
+            val2Type = String.class;
+        }
+        if (!Filters.canConvert(val1Type, val2Type, false)
+            && !Filters.canConvert(val2Type, val1Type, false))
+            throw new UserException(_loc.get("cant-convert", val1Type, val2Type));
 
         ctx.store.getDBDictionary().comparison(buf, _op,
             new FilterValueImpl(sel, ctx, bstate.state1, _val1),

Modified: openjpa/branches/2.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestJDBCEscapeDate.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestJDBCEscapeDate.java?rev=1580973&r1=1580972&r2=1580973&view=diff
==============================================================================
--- openjpa/branches/2.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestJDBCEscapeDate.java
(original)
+++ openjpa/branches/2.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestJDBCEscapeDate.java
Mon Mar 24 18:29:15 2014
@@ -44,7 +44,7 @@ public class TestJDBCEscapeDate extends 
         setUp(Employee.class, DROP_TABLES);
     }
 
-    public void testJDBCEscape() {
+    public void populate(){
         EntityManager em = emf.createEntityManager();
         EntityTransaction tran = em.getTransaction();
         Employee e = new Employee();
@@ -55,9 +55,13 @@ public class TestJDBCEscapeDate extends 
         e.setHireTimestamp(new Date());
         em.persist(e);
         tran.begin();
-        em.flush();
         tran.commit();
-        em.clear();
+        em.close();        
+    }
+    
+    public void testJDBCEscape() {
+        populate();
+        EntityManager em = emf.createEntityManager();
 
         String[] jpql;
         DBDictionary dict = ((JDBCConfiguration)emf.getConfiguration()).getDBDictionaryInstance();
@@ -157,4 +161,21 @@ public class TestJDBCEscapeDate extends 
         Assert.assertEquals(1, updateCnt);
         em.close();
     }
+    
+    /*
+     * Added for OJ-2286.  The test executes the same query multiple times.  Prior
+     * to the JIRA fix, upon the second exception an exception would occur.  
+     */
+    public void testMultipleQueryExecutionWithDateLiteral() {
+        populate();
+        EntityManager em = emf.createEntityManager();
+        
+        Query q = em.createQuery("SELECT e FROM Employee e WHERE e.hireTimestamp > {ts
'2001-01-01 00:00:00'}");        
+        Assert.assertEquals("First assertion", 1, q.getResultList().size());
+        // Prior to JIRA OJ-2286, an exception would occur here:
+        Assert.assertEquals("Second assertion", 1, q.getResultList().size());
+        // For good measure execute it a couple more times.  :)
+        Assert.assertEquals("Third assertion", 1, q.getResultList().size());
+        Assert.assertEquals("Fourth assertion", 1, q.getResultList().size());
+    }    
 }



Mime
View raw message