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 (?)