java.lang.ClassCastException: org.dom4j.tree.DefaultElement

Hibernate JIRA | Alexander Kiel | 1 decade ago
tip
Your exception is missing from the Samebug knowledge base.
Here are the best solutions we found on the Internet.
Click on the to mark the helpful solution and get rewards for you help.
  1. 0

    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

    Hibernate JIRA | 1 decade ago | Alexander Kiel
    java.lang.ClassCastException: org.dom4j.tree.DefaultElement
  2. 0

    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

    Hibernate JIRA | 1 decade ago | Alexander Kiel
    java.lang.ClassCastException: org.dom4j.tree.DefaultElement
  3. 0

    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)

    Hibernate JIRA | 1 decade ago | David Tashima
    java.lang.ClassCastException
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    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)

    Hibernate JIRA | 1 decade ago | David Tashima
    java.lang.ClassCastException

    Root Cause Analysis

    1. java.lang.ClassCastException

      org.dom4j.tree.DefaultElement

      at org.hibernate.type.ListType.indexOf()
    2. Hibernate
      AbstractEntityPersister.getPropertyValuesToInsert
      1. org.hibernate.type.ListType.indexOf(ListType.java:49)
      2. org.hibernate.engine.StatefulPersistenceContext.getIndexInParent(StatefulPersistenceContext.java:1066)
      3. org.hibernate.engine.StatefulPersistenceContext.getIndexInOwner(StatefulPersistenceContext.java:1041)
      4. org.hibernate.property.IndexPropertyAccessor$IndexGetter.getForInsert(IndexPropertyAccessor.java:74)
      5. org.hibernate.tuple.AbstractEntityTuplizer.getPropertyValuesToInsert(AbstractEntityTuplizer.java:264)
      6. org.hibernate.persister.entity.AbstractEntityPersister.getPropertyValuesToInsert(AbstractEntityPersister.java:3331)
      6 frames
    3. Hibernate
      AbstractSaveEventListener.performSaveOrReplicate
      1. org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:244)
      1 frame