Code:
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)
(Tried also with H3 from CVS.)
Fetching data with default EntityMode (now commented in class Main) works fine.