-->
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.  [ 4 posts ] 
Author Message
 Post subject: Persisting xml data including child elements
PostPosted: Sat Jul 26, 2008 10:33 am 
Newbie

Joined: Sat Jul 26, 2008 10:13 am
Posts: 3
Hi

I am attempting to persist xml data into a database with Hibernate.

The Account table is created and the elements are persisted, but I cannot get the child Contact elements to be persisted, although the Contact table is created. I have tried many mapping file configurations but no matter what I try no Contacts are persisted.

Where am I going wrong with the mapping?


Hibernate version:
3.2.5

XML document to be persisted:
Code:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:opp="http://xyz/wsdl/OpportunityService">
   <soapenv:Header/>
   <soapenv:Body>
      <opp:OpportunityServiceOperationResponse>
   <contactList>

<opp:ListOfXYZOpportunity>
        <opp:Opportunity operation="" searchspec="">
                <opp:Account>Account Number 1</opp:Account>
               
                <opp:ListOfContact>
                        <opp:Contact operation="" searchspec="">
                                <opp:AccessLevel>4</opp:AccessLevel>
                                <opp:Accomplishments>some stuff 1</opp:Accomplishments>
                            </opp:Contact>
                        <opp:Contact operation="" searchspec="">
                                <opp:AccessLevel>3</opp:AccessLevel>
                                <opp:Accomplishments>some stuff 2</opp:Accomplishments>
                            </opp:Contact>
                        <opp:Contact operation="" searchspec="">
                                <opp:AccessLevel>2</opp:AccessLevel>
                                <opp:Accomplishments>some stuff 3</opp:Accomplishments>
                            </opp:Contact>
                    </opp:ListOfContact>
            </opp:Opportunity>

</opp:ListOfXYZOpportunity>

         </contactList>
      </opp:OpportunityServiceOperationResponse>
   </soapenv:Body>
</soapenv:Envelope>


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="Opportunity"
        table="OPPORTUNITY">
       
    <id name="id" column="ID" type="integer">
      <generator class="native"/>
    </id>

    <set name="contacts" inverse="true" cascade="all" >
        <key column="CONTACT_ID" />
        <one-to-many class="Contact" node="opp:Contact"/>
    </set>

   
    <property name="account" column="ACCOUNT" node="Account" type="string"/>
   
</class>
<class entity-name="Contact"
        table="CONTACT">
       
    <id name="id" column="ID" type="long">
      <generator class="native"/>
    </id>
   
    <many-to-one name="opportunity"
            column="OPPORTUNITY_ID" 
            embed-xml="false"
            class="Opportunity"
            cascade="all"/>

   
    <property name="access_level"
            column="ACCESS_LEVEL"
            node="AccessLevel"
            type="string">
    </property>
           
    <property name="accomplishments"
            column="ACCOMPLISHMENTS"
            node="Accomplishments"
            type="string">
    </property>
   
</class>
</hibernate-mapping>


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

            SAXReader reader = new SAXReader();
            Document document = reader.read(new File("sample_opportunity.xml"));

            Session dom4jSession = session.getSession(EntityMode.DOM4J);
           
            List<Element> opportunityNodes = document.selectNodes("//opp:Opportunity");
            for(Element opportunity: opportunityNodes){
                dom4jSession.save(opportunity);
            }

            session.getTransaction().commit();


Name and version of the database you are using:
MySQL 5

The generated SQL (show_sql=true):
Code:
Hibernate: insert into OPPORTUNITY (ACCOUNT) values (?)
Hibernate: insert into OPPORTUNITY (ACCOUNT) values (?)
Hibernate: insert into OPPORTUNITY (ACCOUNT) values (?)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 27, 2008 11:32 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Make sure you are problematically saving the child instances.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 27, 2008 11:50 am 
Newbie

Joined: Sat Jul 26, 2008 10:13 am
Posts: 3
Thanks Cameron. I can get the Contacts to persist with the code below but the OPPORTUNITY_ID on the CONTACT table is always null.

1. How do I ensure the foreign key is persisted?
2. Is there any way to save a top level entity (Opportunity) and have the child entities (Contacts) persisted without having to programatically do so?

Code:
SAXReader reader = new SAXReader();
            Document document = reader.read(new File("sample_opportunity.xml"));

            Session dom4jSession = session.getSession(EntityMode.DOM4J);
           
            List<Element> opportunityNodes = document.selectNodes("//opp:Opportunity");
            for(Element opportunity: opportunityNodes){
                dom4jSession.save(opportunity);
            }
           
            List<Element> contactNodes = document.selectNodes("//opp:Contact");
            for(Element contact: contactNodes){
                dom4jSession.save(contact);
            }


Top
 Profile  
 
 Post subject: Bump
PostPosted: Tue Jul 29, 2008 3:45 am 
Newbie

Joined: Sat Jul 26, 2008 10:13 am
Posts: 3
Bump


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