You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Jody Grassel <fy...@gmail.com> on 2013/03/05 15:43:11 UTC

Re: Custom field mapping fails with MAX() query

Good morning.  I remember investigating an issue raised last year with
someone using JODA time APIs.  In that instance, the developer was
attempting to use the JODA types in identity fields, which wouldn't work
without significant changes to the object id handling since the JODA types
do not extend java.util.Date (the temporal type required by the JPA
specification.)

However, as a regular persistant-capable field I would expect the JODA
types to be used just fine as either a BLOB (since they are Serializable),
or some other column type through the use of the @Externalizer and @Factory
tools to convert it to/from the type defined on the table column.  I would
expect ValueHandlers to be able to perform this conversion just as well.

Can you open a JIRA and upload a junit to reproduce the problem they are
encountering?

On Thu, Feb 28, 2013 at 10:19 AM, BigManStan <jt...@gmail.com> wrote:

> 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.
>