From 833031ba61e2d15611f23baa9294035aa95ac9f9 Mon Sep 17 00:00:00 2001 From: Heath Thomann Date: Mon, 24 Mar 2014 18:26:36 +0000 Subject: [PATCH] OPENJPA-2286: ArgumentException: Attempt to compare incompatible types class java.util.Date and class org.apache.openjpa.jdbc.sql.Raw git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1580972 13f79535-47bb-0310-9956-ffa450edef68 --- .../jdbc/kernel/exps/CompareExpression.java | 18 ++++++++++--- .../persistence/query/TestJDBCEscapeDate.java | 27 ++++++++++++++++--- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/CompareExpression.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/CompareExpression.java index 305585f19..cddcd8b76 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/CompareExpression.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/CompareExpression.java @@ -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), diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestJDBCEscapeDate.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestJDBCEscapeDate.java index 1dbacefca..2e6bec0d1 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestJDBCEscapeDate.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestJDBCEscapeDate.java @@ -44,7 +44,7 @@ public class TestJDBCEscapeDate extends SingleEMFTestCase { 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 SingleEMFTestCase { 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 SingleEMFTestCase { 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()); + } }