I am using hibernate version 3.0.5. I am trying to export/import data from and to hibernate objects. I have been successfully managed to export the data from hibernate object using DOM4J.
However, when I try to import the same XML using dom4jsession it generates an error. Following is the call stack:
org.hibernate.MappingException: Unknown entity: CategoryName
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:569)
at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1089)
at org.hibernate.event.def.DefaultReplicateEventListener.onReplicate(DefaultReplicateEventListener.java:56)
at org.hibernate.impl.SessionImpl.replicate(SessionImpl.java:694)
at wfp.service.CommonServiceImpl.replicateObject(CommonServiceImpl.java:14)
at wfp.web.XmlDataHandler.importData(XmlDataHandler.java:111)
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:585)
at org.apache.myfaces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:129)
at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:63)
at javax.faces.component.UICommand.broadcast(UICommand.java:106)
at javax.faces.component.UIViewRoot._broadcastForPhase(UIViewRoot.java:90)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:164)
at org.apache.myfaces.lifecycle.LifecycleImpl.invokeApplication(LifecycleImpl.java:316)
at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:86)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:106)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:374)
at org.mortbay.jetty.servlet.WebApplicationHandler$Chain.doFilter(WebApplicationHandler.java:529)
at wfp.web.HibernateFilter.doFilter(HibernateFilter.java:37)
at org.mortbay.jetty.servlet.WebApplicationHandler$Chain.doFilter(WebApplicationHandler.java:520)
at wfp.web.AccessControlFilter.doFilter(AccessControlFilter.java:79)
at org.mortbay.jetty.servlet.WebApplicationHandler$Chain.doFilter(WebApplicationHandler.java:520)
at org.mortbay.jetty.servlet.WebApplicationHandler.dispatch(WebApplicationHandler.java:472)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:520)
at org.mortbay.http.HttpContext.handle(HttpContext.java:1451)
at org.mortbay.jetty.servlet.WebApplicationContext.handle(WebApplicationContext.java:515)
at org.mortbay.http.HttpContext.handle(HttpContext.java:1403)
at org.mortbay.http.HttpServer.service(HttpServer.java:889)
at org.mortbay.http.HttpConnection.service(HttpConnection.java:831)
at org.mortbay.http.HttpConnection.handleNext(HttpConnection.java:998)
at org.mortbay.http.HttpConnection.handle(HttpConnection.java:848)
at org.mortbay.http.SocketListener.handleConnection(SocketListener.java:222)
at org.mortbay.util.ThreadedServer.handle(ThreadedServer.java:325)
at org.mortbay.util.ThreadPool$PoolThread.run(ThreadPool.java:520)
My mapping file is as follows:
<?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="wfp.domain">
<class name="CategoryName"
table="category_name"
node="CategoryName">
<id name="metadataId" column="category_name_id" node="@metadataid">
<generator class="native"/>
</id>
<version name="version" unsaved-value="null" node="@version"/>
<property name="name" type="string" not-null="true" unique="true" node="name"/>
<property name="description" type="string" not-null="true" node="description"/>
<property name="displayOrder" column="display_order" not-null="true" node="displayorder"/>
<list name="skills" cascade="all" inverse="true" lazy="false" node="." embed-xml="true">
<key column="category_name_id"/>
<list-index column="display_order" base="0"/>
<one-to-many class="SkillName" embed-xml="false" node="skillname"/>
</list>
<many-to-one name="wfp" column="wfp_id" not-null="true" node="wfp/@wfpid" embed-xml="false"/>
</class>
</hibernate-mapping>
Following is a part of the XML that I try to import:
<CategoryName metadataid="1" version="16"><skillname>1</skillname><skillname>2</skillname><skillname>3</skillname><skillname>4</skillname><skillname>5</skillname><skillname>6</skillname><skillname>7</skillname><skillname>8</skillname><skillname>9</skillname><skillname>10</skillname><skillname>11</skillname><skillname>12</skillname><skillname>187</skillname><skillname>188</skillname><skillname>189</skillname><name>Infrastructure</name><description>Infrastructure skills include all the setup and configuration of the various tools and systems required for work to happen</description><displayorder>0</displayorder><wfp wfpid="1"/></CategoryName>
I am using the following code to import the data from the XML:
Code:
Document doc = reader.read(file);
Element root = doc.getRootElement();
Session session = HibernateWrapper.currentSession();
Session dom4jSession = session.getSession(EntityMode.DOM4J);
CommonService commonService = (CommonService) IoC.getFactory().getInstance("CommonService");
Iterator i = root.elementIterator();
Transaction tx = dom4jSession.beginTransaction();
while(i.hasNext())
{
Element element = (Element) i.next();
dom4jSession.replicate("CategoryName", element,ReplicationMode.OVERWRITE);
session.flush();
}
tx.commit();
I have tried using saveorUpdate method instead of the replicate, but it did the same. Can someone help? Thanks.