Hi all,
I have some questions and a problem regarding to importing XML data into a relational database using hibernate. First things first, the questions.
[*] Is there any documentation/tutorials available regarding this subject? If so, where can I find it?
[*] Are parent child relations automatically resolved?
Example:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<DataContainer>
<EquipmentError dateTime="2003-12-04T09:18:16.85+01:00" laneList="" zoneList="" errorId="53860 - BVM: Servo power failure" errorInstanceId="2">
<Extensions>
</other_data>
</Extensions>
</EquipmentError>
</DataContainer>
Parent: EquipmentError (PK=errorInstanceId)
Child: Extensions (PK=FK=errorInstanceId of parent)
Is it possible to import the xml above without applying any transformations (i.e. adding the errorInstanceId attribute also to the Extensions alement)?
My actual problem:
[*] When trying to store a parent child relation in two steps (first I store the parent, then the child), I cannot store the child. Am I missing something in my mapping document?
parent inputCode:
<EquipmentError dateTime="2003-12-04T09:18:16.85+01:00" laneList="" zoneList="" errorId="53860 - BVM: Servo power failure" errorInstanceId="2" />
child inputCode:
<Extensions errorInstanceId="1">
<EquipmentErrorSubsystem>
<MachineError vendorErrorCode="53860" description="BVM: Servo power failure">
<Subsystem subsystemType="BVM" subsystemId="3" operatorName="User" recoveryType="0" feeder="0" channel="0"/>
</MachineError>
</EquipmentErrorSubsystem>
</Extensions>
Hibernate version: 3.2.0cr5
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>
<class entity-name="EquipmentError"
table="EquipmentError"
node="EquipmentError"
lazy="false">
<id name="errorInstanceId"
column="errorInstanceId"
node="@errorInstanceId"
type="string"
unsaved-value="unsaved">
<generator class="assigned"/>
</id>
<property name="dateTime"
column="dateTime"
node="@dateTime"
type="string"/>
<property name="errorId"
column="errorId"
node="@errorId"
type="string"/>
<property name="materialHandlerType"
column="materialHandlerType"
node="@materialHandlerType"
type="string"/>
<property name="laneList"
column="laneList"
node="@laneList"
type="string"/>
<property name="zoneList"
column="zoneList"
node="@zoneList"
type="string"/>
<set name="extensions"
inverse="false"
cascade="all"
lazy="false">
<key column="equipmentErrorId"></key>
<one-to-many entity-name="Extensions"
node="Extensions"
embed-xml="true"
/>
</set>
</class>
<class entity-name="Extensions"
table="Extensions"
node="Extensions"
lazy="false">
<id name="id"
column="id"
node="@id"
type="long">
<generator class="native"/>
</id>
<many-to-one name="equipmentErrorId"
column="equipmentErrorId"
node="@errorInstanceId"
embed-xml="true"
entity-name="EquipmentError"
cascade="all"
lazy="false"/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
final Session session = getSessionFactory().openSession();
session.beginTransaction();
// Write dom4j DOM into Hibernate Session
// TODO what to do with the DataContainer?
try {
if ("DataContainer".equals(documentInput.getRootElement().getName())) {
final Element datacontainer = documentInput.getRootElement();
for (final Iterator i = datacontainer.elementIterator(); i.hasNext();) {
final Element element = (Element) i.next();
if (null != element) {
session.getSession(EntityMode.DOM4J).merge(element);
}
}
} else {
final Element element = documentInput.getRootElement();
if (null != element) {
session.getSession(EntityMode.DOM4J).merge(element);
}
}
session.flush();
session.getTransaction().commit();
} catch (RuntimeException e) {
session.getTransaction().rollback();
throw e;
} finally {
if (null != session) {
session.close();
}
}
Full stack trace of any exception that occurs:Code:
org.hibernate.TypeMismatchException: Provided id of the wrong type. Expected: class java.lang.String, got class org.dom4j.tree.DefaultAttribute
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:84)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:815)
at org.hibernate.event.def.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:229)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:120)
at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:687)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:669)
at org.hibernate.engine.CascadingAction$6.cascade(CascadingAction.java:245)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
at org.hibernate.event.def.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:412)
at org.hibernate.event.def.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:178)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:123)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:53)
at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:677)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:661)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:665)
at com.assembleon.emis.collector.HibernateXmlSink.input(HibernateXmlSink.java:110)
at com.assembleon.emis.collector.HibernateXmlSink.input(HibernateXmlSink.java:1)
at com.assembleon.emis.collector.XsltFilter.input(XsltFilter.java:159)
at com.assembleon.emis.collector.XsltFilter.input(XsltFilter.java:1)
at com.assembleon.emis.pipeline.PipeComposite.input(PipeComposite.java:59)
at com.assembleon.emis.collector.DirectorySource.run(DirectorySource.java:46)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask$Sync.innerRunAndReset(Unknown Source)
at java.util.concurrent.FutureTask.runAndReset(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$101(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.runPeriodic(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Name and version of the database you are using: Derby 10.2.1.6
The generated SQL (show_sql=true): -
Debug level Hibernate log excerpt:
6437 [pool-1-thread-1] DEBUG org.hibernate.impl.SessionImpl - opened session at timestamp: 11629073402
6437 [pool-1-thread-1] DEBUG org.hibernate.transaction.JDBCTransaction - begin
6437 [pool-1-thread-1] DEBUG org.hibernate.jdbc.ConnectionManager - opening JDBC connection
6437 [pool-1-thread-1] DEBUG org.hibernate.transaction.JDBCTransaction - current autocommit status: false
6437 [pool-1-thread-1] DEBUG org.hibernate.transaction.JDBCTransaction - commit
6437 [pool-1-thread-1] DEBUG org.hibernate.transaction.JDBCTransaction - committed JDBC Connection
6437 [pool-1-thread-1] DEBUG org.hibernate.jdbc.ConnectionManager - aggressively releasing JDBC connection
6469 [pool-1-thread-1] DEBUG org.hibernate.jdbc.ConnectionManager - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
6625 [pool-1-thread-1] DEBUG org.hibernate.impl.SessionImpl - opened session at timestamp: 11629073405
6625 [pool-1-thread-1] DEBUG org.hibernate.transaction.JDBCTransaction - begin
6625 [pool-1-thread-1] DEBUG org.hibernate.jdbc.ConnectionManager - opening JDBC connection
6625 [pool-1-thread-1] DEBUG org.hibernate.transaction.JDBCTransaction - current autocommit status: false
62390 [pool-1-thread-1] DEBUG org.hibernate.impl.SessionImpl - opened session [dom4j]
385406 [pool-1-thread-1] DEBUG org.hibernate.transaction.JDBCTransaction - rollback
385406 [pool-1-thread-1] DEBUG org.hibernate.transaction.JDBCTransaction - rolled back JDBC Connection
385406 [pool-1-thread-1] DEBUG org.hibernate.jdbc.ConnectionManager - aggressively releasing JDBC connection
385406 [pool-1-thread-1] DEBUG org.hibernate.jdbc.ConnectionManager - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
Thanks in advance,
Niels