java.lang.ClassCastException: org.dom4j.tree.DefaultElement is thrown when converting a list to XML using the following simple code:
Code:
<class name="com.rxp.hib.KeySet"
table="keysets">
<id name="id"
type="integer"
column="id"
unsaved-value="-1">
<generator class="identity"/>
</id>
<property name="name"
column="NAME"
not-null="true"
unique="true"
/>
<set name="keys"
lazy="false"
table="keyset_word">
<key column="set_id"/>
<many-to-many class="com.rxp.hib.Keyword"
column="key_id"
embed-xml="true"
/>
</set>
</class>
Code:
<class name="com.rxp.hib.Keyword"
table="keywords">
<id name="id"
type="integer"
column="id"
unsaved-value="-1">
<generator class="identity"/>
</id>
<property name="name"
column="NAME"
not-null="true"
unique="true"
/>
</class>
Code:
public class KeySet
{
protected static final int unset_value = -1;
protected int id;
protected String name;
protected Set keys;
/**
* Default constructor
*/
protected KeySet()
{
id = unset_value;
name = null;
keys = null;
}
/**
*/
public KeySet( String newName )
{
id = unset_value;
name = newName;
keys = null;
}
/**
* Get the Id value.
* @return the Id value.
*/
public int getId() {
return id;
}
/**
* Set the Id value.
* @param newId The new Id value.
*/
protected void setId(int newId) {
this.id = newId;
}
/**
* Get the Name value.
* @return the Name value.
*/
public String getName() {
return name;
}
/**
* Set the Name value.
* @param newName The new Name value.
*/
public void setName(String newName) {
this.name = newName;
}
/**
* Get the Keys value.
* @return the Keys value.
*/
public Set getKeys() {
return keys;
}
/**
* Set the Keys value.
* @param newKeys The new Keys value.
*/
public void setKeys(Set newKeys) {
this.keys = newKeys;
}
}
Code:
public class Keyword {
protected static final int unset_value = -1;
protected int id;
protected String name;
/**
* Default constructor
*/
protected Keyword() {
id = unset_value;
name = null;
}
/**
*/
public Keyword(String newName) {
id = unset_value;
name = newName;
}
/**
* Get the Id value.
* @return the Id value.
*/
public int getId() {
return id;
}
/**
* Set the Id value.
* @param newId The new Id value.
*/
protected void setId(int newId) {
this.id = newId;
}
/**
* Get the Name value.
* @return the Name value.
*/
public String getName() {
return name;
}
/**
* Set the Name value.
* @param newName The new Name value.
*/
protected void setName(String newName) {
this.name = newName;
}
}
Code:
:
Session dom4JSession = session.getSession(EntityMode.DOM4J);
List result = dom4JSession.createQuery( "from KeySet as ks where ks.name='colors'" ).list();
Document doc = DocumentHelper.createDocument();
Element root = doc.addElement(rootNode);
for (int i = 0; i < result.size(); i++) {
Element e = (Element)result.get(i);
root.add(e);
}
:
If this isn't possible then I think I've wasted the last couple of days learning Hibernate!
Cheers
Keith [/i]