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
This commit is contained in:
Heath Thomann 2014-03-24 18:26:36 +00:00
parent 030ba2a41f
commit 833031ba61
2 changed files with 38 additions and 7 deletions

View File

@ -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),

View File

@ -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());
}
}