java.lang.ClassCastException

There are no available Samebug tips for this exception. Do you have an idea how to solve this issue? A short tip would help users who saw this issue last week.

  • If you attempt to replicate() an object that has a property of type Calendar (calendar_date), you get a ClassCastException. I loathe jumping to conclusions, but I am guessing it is happening on line 54 of CalendarDateType.java, which looks like this: result.setTime( ( (Date) Hibernate.DATE.fromStringValue(xml) ) ); (note that "Date" is java.sql.Date, and the thing that is being created on the right is a java.util.Date). Muchas Gracias -Dave ------------------------------------------------ [java] Hibernate: select id from donut where id =? [java] java.lang.ClassCastException [java] at org.hibernate.type.CalendarDateType.fromStringValue(CalendarDateType.java:54) [java] at org.hibernate.type.NullableType.fromXMLString(NullableType.java:149) [java] at org.hibernate.type.NullableType.fromXMLNode(NullableType.java:173) [java] at org.hibernate.property.Dom4jAccessor$ElementGetter.get(Dom4jAccessor.java:194) [java] at org.hibernate.property.Dom4jAccessor$Dom4jGetter.getForInsert(Dom4jAccessor.java:9 4) [java] at org.hibernate.tuple.AbstractEntityTuplizer.getPropertyValuesToInsert(AbstractEntit yTuplizer.java:264) [java] at org.hibernate.persister.entity.AbstractEntityPersister.getPropertyValuesToInsert(A bstractEntityPersister.java:3328) [java] at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractS aveEventListener.java:244) [java] at org.hibernate.event.def.DefaultReplicateEventListener.onReplicate(DefaultReplicate EventListener.java:117) [java] at org.hibernate.impl.SessionImpl.fireReplicate(SessionImpl.java:854) [java] at org.hibernate.impl.SessionImpl.replicate(SessionImpl.java:848) [java] at DonutTest.reconstitute(DonutTest.java:57) [java] at DonutTest.main(DonutTest.java:35)
    via by David Tashima,
  • If you attempt to replicate() an object that has a property of type Calendar (calendar_date), you get a ClassCastException. I loathe jumping to conclusions, but I am guessing it is happening on line 54 of CalendarDateType.java, which looks like this: result.setTime( ( (Date) Hibernate.DATE.fromStringValue(xml) ) ); (note that "Date" is java.sql.Date, and the thing that is being created on the right is a java.util.Date). Muchas Gracias -Dave ------------------------------------------------ [java] Hibernate: select id from donut where id =? [java] java.lang.ClassCastException [java] at org.hibernate.type.CalendarDateType.fromStringValue(CalendarDateType.java:54) [java] at org.hibernate.type.NullableType.fromXMLString(NullableType.java:149) [java] at org.hibernate.type.NullableType.fromXMLNode(NullableType.java:173) [java] at org.hibernate.property.Dom4jAccessor$ElementGetter.get(Dom4jAccessor.java:194) [java] at org.hibernate.property.Dom4jAccessor$Dom4jGetter.getForInsert(Dom4jAccessor.java:9 4) [java] at org.hibernate.tuple.AbstractEntityTuplizer.getPropertyValuesToInsert(AbstractEntit yTuplizer.java:264) [java] at org.hibernate.persister.entity.AbstractEntityPersister.getPropertyValuesToInsert(A bstractEntityPersister.java:3328) [java] at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractS aveEventListener.java:244) [java] at org.hibernate.event.def.DefaultReplicateEventListener.onReplicate(DefaultReplicate EventListener.java:117) [java] at org.hibernate.impl.SessionImpl.fireReplicate(SessionImpl.java:854) [java] at org.hibernate.impl.SessionImpl.replicate(SessionImpl.java:848) [java] at DonutTest.reconstitute(DonutTest.java:57) [java] at DonutTest.main(DonutTest.java:35)
    via by David Tashima,
  • I try to save a entity (DOM4J mode) which contains a list: <multiple-choice-task id="1"> ... <choices> <choice id="2"> <task task-ref="1"/> ... </choice> <choice id="3"> <task task-ref="1"/> ... </choice> </choices> </multiple-choice-task> Mappings: <list name="choices" lazy="true" inverse="false" cascade="all-delete-orphan" node="choices"> <key column="taskid" not-null="true"/> <list-index column="index"/> <one-to-many class="....Choice"/> </list> <class name="....Choice" table="choice" node="choice"> <id name="id" column="id" unsaved-value="null" node="@id"> <generator class="hilo"/> </id> <many-to-one name="task" insert="false" update="false" node="task/@task-ref" embed-xml="false"> <column name="taskid" not-null="true"/> </many-to-one> ... </class> If I save the multiple-choice-task with Hibernate 3.1.3 I get following Exception: java.lang.ClassCastException: org.dom4j.tree.DefaultElement at org.hibernate.type.ListType.indexOf(ListType.java:49) at org.hibernate.engine.StatefulPersistenceContext.getIndexInParent(StatefulPersistenceContext.java:1066) at org.hibernate.engine.StatefulPersistenceContext.getIndexInOwner(StatefulPersistenceContext.java:1041) at org.hibernate.property.IndexPropertyAccessor$IndexGetter.getForInsert(IndexPropertyAccessor.java:74) at org.hibernate.tuple.AbstractEntityTuplizer.getPropertyValuesToInsert(AbstractEntityTuplizer.java:264) at org.hibernate.persister.entity.AbstractEntityPersister.getPropertyValuesToInsert(AbstractEntityPersister.java:3331) at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:244) Sourcecode of ListType.indexOf: public Object indexOf(Object collection, Object element) { List list = (List) collection; for ( int i=0; i<list.size(); i++ ) { //TODO: proxies! if ( list.get(i)==element ) return new Integer(i); } return null; } The problem is that the collection is in DOM4J mode a Element with doesn't implement List. In my example collection is the <choices> Element and element is one of the <choice> Elements. I suggest following FIX: public Object indexOf(Object collection, Object element) { if (collection instanceof List) { int index = ((List) collection).indexOf(element); return index == -1 ? null : new Integer(index); } else if (collection instanceof Element) { int index = ((Element) collection).content().indexOf(element); return index == -1 ? null : new Integer(index); } else { throw new IllegalArgumentException("collection type not supported."); } } It works for me. By the way: Why bother with a own for loop and own comparison? Because of equals()? list.get(i) can be much more costly on non RandomAccess lists than an unnessesary equals(). Please fix this issue in the next release. I have to work with the SVN Version and my own fix until than. I have a production system to which I want to add XML backup/restore. Thanks. Alexander Kiel
    via by Alexander Kiel,
  • I try to save a entity (DOM4J mode) which contains a list: <multiple-choice-task id="1"> ... <choices> <choice id="2"> <task task-ref="1"/> ... </choice> <choice id="3"> <task task-ref="1"/> ... </choice> </choices> </multiple-choice-task> Mappings: <list name="choices" lazy="true" inverse="false" cascade="all-delete-orphan" node="choices"> <key column="taskid" not-null="true"/> <list-index column="index"/> <one-to-many class="....Choice"/> </list> <class name="....Choice" table="choice" node="choice"> <id name="id" column="id" unsaved-value="null" node="@id"> <generator class="hilo"/> </id> <many-to-one name="task" insert="false" update="false" node="task/@task-ref" embed-xml="false"> <column name="taskid" not-null="true"/> </many-to-one> ... </class> If I save the multiple-choice-task with Hibernate 3.1.3 I get following Exception: java.lang.ClassCastException: org.dom4j.tree.DefaultElement at org.hibernate.type.ListType.indexOf(ListType.java:49) at org.hibernate.engine.StatefulPersistenceContext.getIndexInParent(StatefulPersistenceContext.java:1066) at org.hibernate.engine.StatefulPersistenceContext.getIndexInOwner(StatefulPersistenceContext.java:1041) at org.hibernate.property.IndexPropertyAccessor$IndexGetter.getForInsert(IndexPropertyAccessor.java:74) at org.hibernate.tuple.AbstractEntityTuplizer.getPropertyValuesToInsert(AbstractEntityTuplizer.java:264) at org.hibernate.persister.entity.AbstractEntityPersister.getPropertyValuesToInsert(AbstractEntityPersister.java:3331) at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:244) Sourcecode of ListType.indexOf: public Object indexOf(Object collection, Object element) { List list = (List) collection; for ( int i=0; i<list.size(); i++ ) { //TODO: proxies! if ( list.get(i)==element ) return new Integer(i); } return null; } The problem is that the collection is in DOM4J mode a Element with doesn't implement List. In my example collection is the <choices> Element and element is one of the <choice> Elements. I suggest following FIX: public Object indexOf(Object collection, Object element) { if (collection instanceof List) { int index = ((List) collection).indexOf(element); return index == -1 ? null : new Integer(index); } else if (collection instanceof Element) { int index = ((Element) collection).content().indexOf(element); return index == -1 ? null : new Integer(index); } else { throw new IllegalArgumentException("collection type not supported."); } } It works for me. By the way: Why bother with a own for loop and own comparison? Because of equals()? list.get(i) can be much more costly on non RandomAccess lists than an unnessesary equals(). Please fix this issue in the next release. I have to work with the SVN Version and my own fix until than. I have a production system to which I want to add XML backup/restore. Thanks. Alexander Kiel
    via by Alexander Kiel,
    • java.lang.ClassCastException at org.hibernate.type.CalendarDateType.fromStringValue(CalendarDateType.java:54) at org.hibernate.type.NullableType.fromXMLString(NullableType.java:149) at org.hibernate.type.NullableType.fromXMLNode(NullableType.java:173) at org.hibernate.property.Dom4jAccessor$ElementGetter.get(Dom4jAccessor.java:194) at org.hibernate.property.Dom4jAccessor$Dom4jGetter.getForInsert(Dom4jAccessor.java:94) at org.hibernate.tuple.AbstractEntityTuplizer.getPropertyValuesToInsert(AbstractEntityTuplizer.java:264) at org.hibernate.persister.entity.AbstractEntityPersister.getPropertyValuesToInsert(AbstractEntityPersister.java:3328) at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:244) at org.hibernate.event.def.DefaultReplicateEventListener.onReplicate(DefaultReplicateEventListener.java:117) at org.hibernate.impl.SessionImpl.fireReplicate(SessionImpl.java:854) at org.hibernate.impl.SessionImpl.replicate(SessionImpl.java:848) at DonutTest.reconstitute(DonutTest.java:57) at DonutTest.main(DonutTest.java:35)
    No Bugmate found.