-->
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.  [ 3 posts ] 
Author Message
 Post subject: Mapping repeatable xml elements
PostPosted: Thu Mar 08, 2007 5:28 pm 
Newbie

Joined: Thu Mar 08, 2007 4:28 pm
Posts: 2
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 &amp; 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]


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 25, 2007 8:37 am 
Newbie

Joined: Sun Mar 25, 2007 8:33 am
Posts: 2
Hi,

did you ever get this sorted out?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 26, 2007 8:45 am 
Newbie

Joined: Thu Mar 08, 2007 4:28 pm
Posts: 2
Not really. I figured out a work-around I think, to add an element around each repeatable element like so:


Code:
<notes>
<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>
</notes>


Then adjust my hbm files accordingly. It is not really the ideal solution because I have to apply a stylesheet to the xml before I am able to store it to add these extra grouping elements (since I don't really have control over the xml schema in use).


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