openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jared Pearson <jaredapear...@gmail.com>
Subject ClassCastException thrown when using custom strategy on @Id column
Date Mon, 25 Apr 2011 21:23:30 GMT
I'm using a custom ValueHandler to convert a numeric value in the
database to a Joda DateTime.  I'm getting a ClassCastException when I
use the @Strategy on column.  If I remove the strategy off of the
numeric date field, the record is loaded correctly.  Any help or
insight would be helpful.

java.lang.ClassCastException: org.joda.time.DateMidnight cannot be
cast to org.apache.openjpa.util.ObjectId
	at com.company.product.Entity.pcCopyKeyFieldsToObjectId(Entity.java)
	at org.apache.openjpa.enhance.PCRegistry.copyKeyFieldsToObjectId(PCRegistry.java:167)
	at org.apache.openjpa.util.ApplicationIds.fromPKValues(ApplicationIds.java:224)
	at org.apache.openjpa.jdbc.meta.ClassMapping.getObjectId(ClassMapping.java:219)
	at org.apache.openjpa.jdbc.meta.ClassMapping.getObjectId(ClassMapping.java:147)
	at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.load(JDBCStoreManager.java:1031)
	at org.apache.openjpa.jdbc.sql.AbstractResult.load(AbstractResult.java:280)
	at org.apache.openjpa.jdbc.sql.SelectImpl$SelectResult.load(SelectImpl.java:2381)
	at org.apache.openjpa.jdbc.meta.strats.RelationToManyInverseKeyFieldStrategy.loadElement(RelationToManyInverseKeyFieldStrategy.java:90)
	at org.apache.openjpa.jdbc.meta.strats.RelationCollectionInverseKeyFieldStrategy.loadElement(RelationCollectionInverseKeyFieldStrategy.java:76)
	at org.apache.openjpa.jdbc.meta.strats.StoreCollectionFieldStrategy.load(StoreCollectionFieldStrategy.java:558)
	at org.apache.openjpa.jdbc.meta.FieldMapping.load(FieldMapping.java:934)
	at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.load(JDBCStoreManager.java:702)
	at org.apache.openjpa.kernel.DelegatingStoreManager.load(DelegatingStoreManager.java:117)
	at org.apache.openjpa.kernel.ROPStoreManager.load(ROPStoreManager.java:78)

@Entity
@IdClass(EntityKey.class)
public class Entity
	implements Serializable
{
        @Id
        @Column(name="ENTITY_CODE")
        private String code;

	@Id
	@Column(name="EFFDATE")
	@Strategy("com.company.product.NumericDateValueHandler")
	private DateMidnight effectiveDate;
	
}

public class EntityKey
{
        private String code;
	private DateMidnight effectiveDate;
}

public class NumericDateValueHandler
	implements ValueHandler
{
	private static final long serialVersionUID = 8355414495623865366L;

	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);

		Column column = new Column();
		column.setIdentifier(colName);
		column.setType(Types.DECIMAL);
		column.setJavaType(JavaTypes.INT);
		column.setSize(-1);
		return new Column[]{column};
	}

	public boolean isVersionable(ValueMapping vm) {
		return false;
	}

	public boolean objectValueRequiresLoad(ValueMapping vm) {
		return false;
	}

	public Object getResultArgument(ValueMapping vm) {
		return null;
	}

	public Object toDataStoreValue(ValueMapping vm, Object val, JDBCStore store) {
		return toDataStoreValue(vm, (DateMidnight)val, store);
	}
	
	public Integer toDataStoreValue(ValueMapping vm, DateMidnight value,
JDBCStore store)
	{
		if(value == null)
		{
			return null;
		}
		
		int cyymmddValue = DateUtils.toNumeric(value);
		return cyymmddValue;
	}

	public Object toObjectValue(ValueMapping vm, Object val) {
		return toObjectValue(vm, (Integer)val);
	}

	public Object toObjectValue(ValueMapping vm, Object val,
			OpenJPAStateManager sm, JDBCStore store,
			JDBCFetchConfiguration fetch) throws SQLException {
		return toObjectValue(vm, (Integer)val);
	}
	
	public DateMidnight toObjectValue(ValueMapping vm, Integer value)
	{
		if(value == null || value < 1)
		 {
                         return null;
		}

		return DateUtils.fromNumeric(value);

	}
}

Mime
View raw message