Return-Path: X-Original-To: apmail-openjpa-users-archive@minotaur.apache.org Delivered-To: apmail-openjpa-users-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id E0C2CED14 for ; Thu, 28 Feb 2013 16:20:02 +0000 (UTC) Received: (qmail 36177 invoked by uid 500); 28 Feb 2013 16:20:02 -0000 Delivered-To: apmail-openjpa-users-archive@openjpa.apache.org Received: (qmail 36038 invoked by uid 500); 28 Feb 2013 16:20:02 -0000 Mailing-List: contact users-help@openjpa.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@openjpa.apache.org Delivered-To: mailing list users@openjpa.apache.org Received: (qmail 36028 invoked by uid 99); 28 Feb 2013 16:20:01 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 28 Feb 2013 16:20:01 +0000 X-ASF-Spam-Status: No, hits=2.0 required=5.0 tests=SPF_NEUTRAL,URI_HEX X-Spam-Check-By: apache.org Received-SPF: neutral (nike.apache.org: 216.139.236.26 is neither permitted nor denied by domain of jtmosso@gmail.com) Received: from [216.139.236.26] (HELO sam.nabble.com) (216.139.236.26) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 28 Feb 2013 16:19:55 +0000 Received: from jim.nabble.com ([192.168.236.80]) by sam.nabble.com with esmtp (Exim 4.72) (envelope-from ) id 1UB6Ck-0004wd-HY for users@openjpa.apache.org; Thu, 28 Feb 2013 08:19:34 -0800 Date: Thu, 28 Feb 2013 08:19:34 -0800 (PST) From: BigManStan To: users@openjpa.apache.org Message-ID: <1362068374495-7582965.post@n2.nabble.com> Subject: Custom field mapping fails with MAX() query MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Virus-Checked: Checked by ClamAV on apache.org Hi, I have created a custom mapping in my entity classes to map java Date objects to a nonstandard Date object. (JodaTime) This mapping uses a ValueHandler to implement the conversion, and @Strategy annotation on the fields to indicate that this handler should be used. The conversion works as expected except in queries with MIN or MAX(dateFieldToConvert), where it dies with a ClassCastException in the OpenJPA code without ever attempting to call my handler. Here is the stack trace of the failure: =========================================================== Caused by: java.lang.ClassCastException: Cannot convert object "12/1/12 12:00 AM" of type "class java.sql.Date" into an instance of "class org.joda.time.LocalDate". at org.apache.openjpa.kernel.Filters.convert(Filters.java:336) at org.apache.openjpa.kernel.Filters.convert(Filters.java:265) at org.apache.openjpa.jdbc.kernel.exps.UnaryOp.load(UnaryOp.java:125) at org.apache.openjpa.jdbc.kernel.ProjectionResultObjectProvider.getResultObject(ProjectionResultObjectProvider.java:78) at org.apache.openjpa.kernel.QueryImpl$PackingResultObjectProvider.getResultObject(QueryImpl.java:2075) at org.apache.openjpa.kernel.QueryImpl.singleResult(QueryImpl.java:1330) at org.apache.openjpa.kernel.QueryImpl.toResult(QueryImpl.java:1242) at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:1007) at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:863) ... 33 more =========================================================== And here is the ValueHandler implementation code I am using: =========================================================== public class LocalDateValueHandler extends AbstractValueHandler { ... @Override public Object toDataStoreValue(ValueMapping vm, Object val, JDBCStore store) { if (val instanceof LocalDate) { return convertLocalDate((LocalDate) val); } else if (val instanceof String) { return convertLocalDate(format.parseDateTime((String) val).toLocalDate()); } else { return val; } } @Override public Object toObjectValue(ValueMapping vm, Object val) { if (val instanceof Date) { Date date = (Date) val; return new LocalDate(date); } else { return val; } } @Override public Column[] map(ValueMapping vm, String name, ColumnIO io, boolean adapt) { DBDictionary dict = vm.getMappingRepository().getDBDictionary(); DBIdentifier colName = DBIdentifier.newColumn(name, dict != null ? dict.delimitAll() : false); return map(vm, colName, io, adapt); } private Column[] map(ValueMapping vm, DBIdentifier name, ColumnIO io, boolean adapt) { Column col = new Column(); col.setIdentifier(name); col.setJavaType(JavaSQLTypes.SQL_DATE); col.setType(Types.DATE); return new Column[] { col }; } ... } =========================================================== Any help would be appreciated. Thanks, Justin -- View this message in context: http://openjpa.208410.n2.nabble.com/Custom-field-mapping-fails-with-MAX-query-tp7582965.html Sent from the OpenJPA Users mailing list archive at Nabble.com.