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

Hibernate JIRA | Winston Huang | 1 decade ago
  1. 0

    when importing from xml format and trying to save the instance using dom4jsession, i got the following exception. it would succeed if i don't use an optimistic locking object (with versioning). see the attached test case for details. i have a many-to-many set relationship from user to role. the xml data files are in User.xml and Role.xml. loading Role.xml is fine. but loading User.xml failed. log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment). log4j:WARN Please initialize the log4j system properly. Exception in thread "main" java.lang.ClassCastException: org.dom4j.tree.DefaultElement at org.hibernate.event.def.DirtyCollectionSearchVisitor.processCollection(DirtyCollectionSearchVisitor.java:50) at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:104) at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:64) at org.hibernate.event.def.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:58) at org.hibernate.event.def.DefaultFlushEntityEventListener.isUpdateNecessary(DefaultFlushEntityEventListener.java:301) at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:111) at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:190) at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:70) at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730) at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:329) at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86) at TestDom4jVersionedObject.main(TestDom4jVersionedObject.java:26)

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

    when importing from xml format and trying to save the instance using dom4jsession, i got the following exception. it would succeed if i don't use an optimistic locking object (with versioning). see the attached test case for details. i have a many-to-many set relationship from user to role. the xml data files are in User.xml and Role.xml. loading Role.xml is fine. but loading User.xml failed. log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment). log4j:WARN Please initialize the log4j system properly. Exception in thread "main" java.lang.ClassCastException: org.dom4j.tree.DefaultElement at org.hibernate.event.def.DirtyCollectionSearchVisitor.processCollection(DirtyCollectionSearchVisitor.java:50) at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:104) at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:64) at org.hibernate.event.def.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:58) at org.hibernate.event.def.DefaultFlushEntityEventListener.isUpdateNecessary(DefaultFlushEntityEventListener.java:301) at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:111) at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:190) at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:70) at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730) at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:329) at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86) at TestDom4jVersionedObject.main(TestDom4jVersionedObject.java:26)

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

    jroller.com | 6 months ago
    java.lang.ClassCastException
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 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
  6. 0

    import java.util.Set; public class TestA implements java.io.Serializable { private int a_id; private String testa; private Set b; public TestA() { } public int getA_id() { return this.a_id; } public void setA_id(int a_id) { this.a_id = a_id; } public String getTesta() { return this.testa; } public void setTesta(String testa) { this.testa = testa; } public Set getB() { return this.b; } public void setB(Set b) { this.b = b; } } public class TestB implements java.io.Serializable { private int b_id; private int ref_a; private String testb; public TestB() { } public int getB_id() { return this.b_id; } public void setB_id(int b_id) { this.b_id = b_id; } public int getRef_a() { return this.ref_a; } public void setRef_a(int ref_a) { this.ref_a = ref_a; } public String getTestb() { return this.testb; } public void setTestb(String testb) { this.testb = testb; } public String toString() { return b_id + ": " + testb; } public boolean equals(Object o) { if (!(o instanceof TestB)) return false; if ((b_id == ((TestB)o).getB_id()) && (ref_a == ((TestB)o).getRef_a())) return true; else return false; } } <hibernate-mapping > <class name="TestA" table="testa" node="testa"> <id name="a_id" type="int" unsaved-value="any" column="a_id" node="@a_id"/> <property name="testa" column="testa" node="testa"/> <set name="b" node="testb" embed-xml="true"> <key column="ref_a"/> <one-to-many class="TestB"/> </set> </class> </hibernate-mapping> <hibernate-mapping> <class name="TestB" table="testb"> <composite-id> <key-property name="b_id" type="int" column="b_id"/> <key-property name="ref_a" column="ref_a"/> </composite-id> <property name="testb" column="testb" node="testb"/> </class> </hibernate-mapping> import org.hibernate.cfg.Configuration; import org.hibernate.*; import org.dom4j.*; import org.dom4j.io.*; import java.util.*; public class Main { public Main() { } public static void main(String[] args) { Configuration cfg = new Configuration() .addClass(TestA.class) .addClass(TestB.class); SessionFactory factory = cfg.buildSessionFactory(); Session session4 = factory.openSession().getSession(EntityMode.DOM4J); Document doc = DocumentHelper.createDocument(); Element root = doc.addElement("testa"); Query query = session4.createQuery("from TestA as a"); for (Iterator it = query.iterate(); it.hasNext();) { Element e = (Element) it.next(); root.add(e); //TestA t = (TestA) it.next(); //System.out.println(t.getA_id()+t.getB().toString()); } } } Exception in thread "main" java.lang.ClassCastException: org.dom4j.tree.DefaultElement at org.hibernate.type.ComponentType.toLoggableString(ComponentType.java:279) at org.hibernate.pretty.MessageHelper.infoString(MessageHelper.java:68) at org.hibernate.engine.EntityKey.toString(EntityKey.java:77) at java.lang.String.valueOf(String.java:2577) at java.lang.StringBuffer.append(StringBuffer.java:220) at org.hibernate.util.StringHelper.toString(StringHelper.java:168) at org.hibernate.loader.Loader.getRow(Loader.java:762) at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:295) at org.hibernate.loader.Loader.doQuery(Loader.java:389) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:208) at org.hibernate.loader.Loader.loadCollection(Loader.java:1367) at org.hibernate.loader.collection.OneToManyLoader.initialize(OneToManyLoader.java:107) at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:488) at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60) at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1356) at org.hibernate.type.CollectionType.getCollection(CollectionType.java:470) at org.hibernate.type.CollectionType.resolveKey(CollectionType.java:328) at org.hibernate.type.CollectionType.resolve(CollectionType.java:322) at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:105) at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:494) at org.hibernate.loader.Loader.doQuery(Loader.java:413) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:208) at org.hibernate.loader.Loader.loadEntity(Loader.java:1278) at org.hibernate.loader.entity.EntityLoader.load(EntityLoader.java:141) at org.hibernate.loader.entity.EntityLoader.load(EntityLoader.java:126) at org.hibernate.persister.entity.BasicEntityPersister.load(BasicEntityPersister.java:2491) at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:386) at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:367) at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:166) at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:140) at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:119) at org.hibernate.impl.SessionImpl.immediateLoad(SessionImpl.java:593) at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:59) at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:80) at org.hibernate.proxy.Dom4jLazyInitializer.getElement(Dom4jLazyInitializer.java:21) at org.hibernate.proxy.Dom4jProxy.target(Dom4jProxy.java:561) at org.hibernate.proxy.Dom4jProxy.getParent(Dom4jProxy.java:177) at org.dom4j.tree.AbstractElement.addNode(AbstractElement.java:2130) at org.dom4j.tree.AbstractElement.add(AbstractElement.java:1412) at Main.main(Main.java:27) Fetching data with default EntityMode (now commented out in class Main) works fine. (Tried also with H3 from CVS. Posted to forum, but no response in a week, so suppose to be a bug.)

    Hibernate JIRA | 1 decade ago | Martin Caslavsky
    java.lang.ClassCastException: org.dom4j.tree.DefaultElement

    Not finding the right solution?
    Take a tour to get the most out of Samebug.

    Tired of useless tips?

    Automated exception search integrated into your IDE

    Root Cause Analysis

    1. java.lang.ClassCastException

      org.dom4j.tree.DefaultElement

      at org.hibernate.event.def.DirtyCollectionSearchVisitor.processCollection()
    2. Hibernate
      SessionImpl.managedFlush
      1. org.hibernate.event.def.DirtyCollectionSearchVisitor.processCollection(DirtyCollectionSearchVisitor.java:50)
      2. org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:104)
      3. org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:64)
      4. org.hibernate.event.def.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:58)
      5. org.hibernate.event.def.DefaultFlushEntityEventListener.isUpdateNecessary(DefaultFlushEntityEventListener.java:301)
      6. org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:111)
      7. org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:190)
      8. org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:70)
      9. org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
      10. org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
      11. org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:329)
      11 frames
    3. Hibernate
      JDBCTransaction.commit
      1. org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
      1 frame
    4. Unknown
      TestDom4jVersionedObject.main
      1. TestDom4jVersionedObject.main(TestDom4jVersionedObject.java:26)
      1 frame