openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From BigManStan <jtmo...@gmail.com>
Subject Custom field mapping fails with MAX() query
Date Thu, 28 Feb 2013 16:19:34 GMT
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.

Mime
View raw message