-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Restore database from XML with the XML mapping
PostPosted: Fri Oct 14, 2005 3:04 am 
Regular
Regular

Joined: Thu Oct 13, 2005 4:19 am
Posts: 98
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
            }
        }
    }
}

_________________
http://www.ohloh.net/accounts/ge0ffrey


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 21, 2005 3:17 am 
Regular
Regular

Joined: Thu Oct 13, 2005 4:19 am
Posts: 98
Anyone?

_________________
http://www.ohloh.net/accounts/ge0ffrey


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 24, 2005 1:15 pm 
Newbie

Joined: Wed Sep 22, 2004 10:25 pm
Posts: 9
Location: Rochester, NY USA
I get this too in Hibernate 3.0.5 and 3.1rc2. For me, it happens when I have a CompositeUserType property. If I comment out that mapping, the export works fine. If I leave in that property mapping, I get the ClassCastException. If you figure this out, please post the answer back to the forum. Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 27, 2005 10:50 am 
Beginner
Beginner

Joined: Thu Oct 13, 2005 3:40 pm
Posts: 29
I get the exact same problem with 3.0.5. The documentation says clearly XML manipulation is experimental. After several debugging sessions, I am resorting to XStream, although it writes out Hibernate members (especially when not all members are initialized when exporting toXML(). I am exploring a way to circumvent that.

If any one of you figure out XML manipulation in Hibernate using EntityMode.DOM4J, please reply to this post.

Thanks

Atul


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.