I am trying to persist some xml with xml-relational mapping using Hibernate. The xml instances have multiple instances of repeatable elements. E.g.,
Code:
<document>
<document_id>bbcards000001</document_id>
<record_create_date>3-5-2007</record_create_date>
<indexing_data_id>bbcards</indexing_data_id>
<item_title>[John Clarkson]</item_title>
<alternate_title>Allen & Ginter World's Champions (N28)</alternate_title>
<subject facet="Player">John Clarkson</subject>
<subject facet="City">Boston</subject>
<subject facet="Team">Boston Beaneaters</subject>
<subject facet="League">National League</subject>
<note>This set is generally acknowledged as the first significant tobacco set issued. Ten baseball players are included in the set, which also depicts six other sports.</note>
<note>9 cards in set</note>
</document>
How do I deal with this? I tried using multiple sets in the mapping (shown below), using a single set works just fine, when I run with the second, after getting sql/debug messages that looked just as expected, at commit time, I get "Found shared references".
Hibernate version:3.2.2
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="DigitalObject"
table="DigitalObjects"
node="document">
<id name="docid" column="DOC_ID" type="int" unsaved-value="0">
<generator class="native"/>
</id>
<property name="originalId"
column="document_id"
node="document_id"
type="string"/>
<property name="item_title"
column="item_title"
node="item_title"
type="string"/>
<property name="alternate_title"
column="alternate_title"
node="alternate_title"
type="string"/>
<set name="subjects"
node="."
embed-xml="true">
<key column="DOC_ID"
not-null="true"/>
<one-to-many entity-name="Subject"
embed-xml="true"/>
</set>
<set name="notes"
node="."
embed-xml="true">
<key column="DOC_ID"
not-null="true"/>
<one-to-many entity-name="Note"
embed-xml="true"/>
</set>
</class>
<class entity-name="Subject"
table="SUBJECTS"
node="subject">
<id name="subjid" column="SUBJECT_ID" type="int" unsaved-value="0">
<generator class="native"/>
</id>
<many-to-one name="subjectsDo" embed-xml="false" update="false" insert="false" not-null="true" entity-name="DigitalObject" column="DOC_ID"/>
<property name="facet"
column="FACET"
node="@facet"
type="string"/>
<property name="subject_text"
column="SUBJECT_TEXT"
node="."
type="string"/>
</class>
<class entity-name="Note"
table="NOTES"
node="note">
<id name="noteid" column="NOTE_ID" type="int" unsaved-value="0">
<generator class="native"/>
</id>
<many-to-one name="notesDo" embed-xml="false" update="false" insert="false" not-null="true" entity-name="DigitalObject" column="DOC_ID"/>
<property name="note_text"
column="NOTE_TEXT"
node="."
type="string"/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Session dom4jSession = session.getSession(EntityMode.DOM4J);
while ( it.hasNext ( ) ) {
Node n = ( Node ) it.next ( ) ;
dom4jSession.save("DigitalObject", n);
/*
List subjectNodes = n.selectNodes("subject");
Iterator subjectIt = subjectNodes.iterator();
while (subjectIt.hasNext())
{
Node sNode = ( Node ) subjectIt.next();
dom4jSession.save("Subject", sNode);
}
*/
/*
List noteNodes = n.selectNodes("note");
Iterator noteIt = noteNodes.iterator();
while (noteIt.hasNext())
{
Node nNode = ( Node ) noteIt.next();
dom4jSession.save("Note", nNode);
}
*/
System.out.println ("Node saved.");
}
session.getTransaction().commit();
HibernateUtil.getSessionFactory().close();
Full stack trace of any exception that occurs:Code:
Exception in thread "main" org.hibernate.HibernateException: Found shared references to a collection: DigitalObject.notes
at org.hibernate.engine.Collections.processReachableCollection(Collections.java:163)
at org.hibernate.event.def.FlushVisitor.processCollection(FlushVisitor.java:37)
at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:101)
at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:61)
at org.hibernate.event.def.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:55)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:131)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:196)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:343)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at gov.loc.am.nonmarc.NonMarcXmlIngestOperation.main(NonMarcXmlIngestOperation.java:57)
Name and version of the database you are using:hsqldb-1_8_0_7
[/code]