I am using hibernate 3.1rc1.
I am also using the xml mapping feature of hibernate (chapter 19 in the manual), which is considered experimental.
I am trying to dump and restore my database with that feature, making it easier to restore testdata.
When restoring the database, it throws a ClassCastException on a string id.
In the following code DemandedOrder has a embed-xml="false" reference to company. It fails on processing a version attribute where the owner is a uuid String (the id of a Company) and can't be casted to an Element.
Code:
public class RestoreDatabaseFromXmlDaoImpl extends HibernateDaoSupport {
private static final String DEFAULT_ROOT_ENTITY_NAME = "database";
private static final String TABLE_ENTITY_NAME = "table";
private static final String CLASS_NAME_ATTRIBUTE_NAME = "className";
private void tempInit() {
rootElementName = "mammoet";
tableClassList.add(Company.class);
tableClassList.add(DemandedOrder.class);
tableClassList.add(SuppliedOrder.class);
}
private String rootElementName = DEFAULT_ROOT_ENTITY_NAME;
private List tableClassList = new LinkedList();
public void setRootElementName(String rootElementName) {
this.rootElementName = rootElementName;
}
public void setTableClassList(List tableClassList) {
this.tableClassList = tableClassList;
}
public void dumpDatabaseIntoXml(OutputStream out) throws IOException {
tempInit();
final Session dom4jSession = getSession().getSession(EntityMode.DOM4J);
Document document = DocumentHelper.createDocument();
Element rootElement = document.addElement(rootElementName);
for (Iterator tableClassIt = tableClassList.iterator(); tableClassIt.hasNext();) {
final Class tableClass = (Class) tableClassIt.next();
Element tableElement = DocumentHelper.createElement(TABLE_ENTITY_NAME);
tableElement.addAttribute(CLASS_NAME_ATTRIBUTE_NAME, tableClass.getName());
// getHibernateTemplate().loadAll(tableClass) does not work
// because HibernateTemplate Session creating does not support EntityMode of Hibernate3
List recordElements = (List) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = dom4jSession.createCriteria(tableClass);
// TODO HibernateTemplate.prepareCriteria(criteria);
return criteria.list();
}
}, true);
for (Iterator recordElementsIt = recordElements.iterator(); recordElementsIt.hasNext();) {
Element recordElement = (Element) recordElementsIt.next();
tableElement.add(recordElement);
}
rootElement.add(tableElement);
}
XMLWriter xmlWriter = new XMLWriter(out, OutputFormat.createPrettyPrint());
xmlWriter.write(document);
}
public void restoreDatabaseFromXml(InputStream in) throws IOException {
tempInit();
Session dom4jSession = getSession().getSession(EntityMode.DOM4J);
SAXReader reader = new SAXReader();
Document document;
try {
document = reader.read(in);
} catch (DocumentException e) {
throw new IllegalArgumentException("Could not parse XML document", e);
}
Element rootElement = document.getRootElement();
Assert.isTrue(rootElementName.equals(rootElement.getName()),
"Root element " + rootElement.getName() + " should be " + rootElementName);
List tableElementList = rootElement.elements(TABLE_ENTITY_NAME);
for (Iterator tableElementIt = tableElementList.iterator(); tableElementIt.hasNext();) {
Element tableElement = (Element) tableElementIt.next();
String className = tableElement.attributeValue(CLASS_NAME_ATTRIBUTE_NAME);
Assert.hasLength(className, "Table elements should contain a class name");
List recordElementList = tableElement.elements();
for (Iterator recordElementIt = recordElementList.iterator(); recordElementIt.hasNext();) {
Element recordElement = (Element) recordElementIt.next();
dom4jSession.replicate(className, recordElement, ReplicationMode.EXCEPTION); // Throws a classcastexception when casting a String id into an dom4j Element
}
}
}
}