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.  [ 2 posts ] 
Author Message
 Post subject: Persisting XML
PostPosted: Tue Jun 06, 2006 5:20 am 
Newbie

Joined: Tue Jun 06, 2006 5:00 am
Posts: 2
I'm trying to store an XML document (or a part of it) directly in the Databse using Hibernate. I used the information described in Chapter 18 of the manual. Returning the DB information as XML works as a charm but I'm a bit stuck on the insert.

I'm probably missing some small detail so any help (or a link to an example) would be appreciated.

P.S. I also tried calling the saveAndUpdate using the XML Doc as a parameter but this gives the same error with this message:
org.hibernate.MappingException: Unknown entity: org.dom4j.tree.DefaultDocument

Hibernate version:

3.1

Mapping documents:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="dsi.dataObjects">
   <class name="EvtSystem" table="EVT_SYSTEM" node="system">
       <id name="id" node="id" column="SYS_PK" >
            <generator class="increment"/>
        </id>
       
        <property name="acr" node="system/@acronym">
            <column name="SYS_ACR" length="8" not-null="true" sql-type="VARCHAR2" />
        </property>
        <property name="name" node="name">
            <column name="SYS_NAME" length="50" not-null="true" sql-type="VARCHAR2" />
        </property>
        <property name="channelType" node="channelType">
            <column name="SYS_CHA_TYPE" length="10" not-null="true" sql-type="VARCHAR2" />
        </property>
        <property name="address" node="address">
            <column name="SYS_ADDRESS" length="255" not-null="true" sql-type="VARCHAR2" />
        </property>   
         <property name="reactionParameter" node="reactionParameter">
            <column name="SYS_PARAM" length="1024" not-null="true" sql-type="VARCHAR2" />
        </property>   
   </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

Code:
   session = HibernateUtil.getSessionFactory().getCurrentSession();
        Document doc = org.dom4j.DocumentHelper.createDocument();
        Element sys = doc.addElement("system") ;
        sys.addAttribute("acronym", "SYA");
        Element tmpE = null;
        tmpE = sys.addElement("name");
        tmpE.setText("a_name");
        tmpE = sys.addElement("channelType");
        tmpE.setText("a_chan");
        tmpE = sys.addElement("address");
        tmpE.setText("an_add");
        tmpE = sys.addElement("reactionParameter");
        tmpE.setText("react");
        tx = session.beginTransaction();
        dom4jSession = session.getSession(org.hibernate.EntityMode.DOM4J);

        session.saveOrUpdate(sys);
       
        tx.commit();


Full stack trace of any exception that occurs:
Code:
org.hibernate.MappingException: Unknown entity: system
   at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:514)
   at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1302)
   at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:180)
   at org.hibernate.event.def.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:460)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:84)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
   at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:502)
   at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:494)
   at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:490)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:324)
   at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
   at $Proxy0.saveOrUpdate(Unknown Source)
   at dsi.test.HibernateTester.testXMLMapping(HibernateTester.java:439)
   at dsi.test.HibernateTester.runHibernateCheck(HibernateTester.java:54)
   at dsi.test.HibernateTester.main(HibernateTester.java:27)

Name and version of the database you are using:

Oracle i9


Top
 Profile  
 
 Post subject: Solution
PostPosted: Tue Jun 06, 2006 5:39 am 
Newbie

Joined: Tue Jun 06, 2006 5:00 am
Posts: 2
As a was looking at my post on the forum I suddenly saw some of my errors.
I was using the regular session instead of the DOM4j one.

thanks for the effort if anyone was already looking into this problem.

I added the code if anyone else has a similar problem.


Code:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        Session dom4jSession = session.getSession(org.hibernate.EntityMode.DOM4J);
        tx = session.beginTransaction();
        Document doc = org.dom4j.DocumentHelper.createDocument();
        Element sys = doc.addElement("system") ;
        sys.addAttribute("acronym", "SYA");
        Element tmpE = null;
        tmpE = sys.addElement("name");
        tmpE.setText("a_name");
        tmpE = sys.addElement("channelType");
        tmpE.setText("a_chan");
        tmpE = sys.addElement("address");
        tmpE.setText("an_add");
        tmpE = sys.addElement("reactionParameter");
        tmpE.setText("react");
       
        dom4jSession.save( EvtSystem.class.getName(), sys );
        tx.commit();
[/code][/quote]


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.