-->
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: Importing XML data using Dom4j
PostPosted: Mon Mar 20, 2006 9:50 am 
Newbie

Joined: Mon Mar 20, 2006 8:42 am
Posts: 11
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Using the following mappings I exported Customer's data. It worked fine and embedded child records in parent record.

While importing the same data no exception occured, but i did not get desired result. Records were inserted only in Customer(parent) table and not in CustomerLocation(child) table.

Can it be achieved???
How???

Hibernate version: 3.0

Mapping documents:

Code:

<class name="CustomerInfo" table="Customer" node="customer">

<id name="customerId" column="CustomerID" node="id" >
   <generator class="assigned" />
</id>
      
<property name="name" node="name" column="Name"/>

<property name="updatedDateTime" node="updatedDateTime" column="UpdatedDateTime"/>

<property name="createdDateTime" node="createdDateTime">
   <column name="CreatedDateTime" />
</property>

<property name="createdBy" node="createdBy">
   <column name="CreatedBy" />
</property>
      
<property name="updatedBy" node="updatedBy">
   <column name="UpdatedBy" />
</property>
         
<set name="locations" table="CustomerLocation" inverse="true" node="locations" embed-xml="true" cascade="replicate">
   <key column="customerid" not-null="true"/>
   <one-to-many class="sbs.DivPlantInfo" node="sbs.DivPlantInfo"  embed-xml="true" />
</set>

</class>



Code:
<class name="DivPlantInfo" table="CustomerLocation" node="sbs.DivPlantInfo">

<composite-id>
   <key-property name="customerId" column="CustomerID" node="CustomerID" />
   <key-property name="locationId" column="LocationID" node="LocationID"/>
</composite-id>

<property name="updatedDateTime" node="UpdatedDateTime">
   <column name="UpdatedDateTime" />
</property>

<property name="createdDateTime" node="CreatedDateTime">
   <column name="CreatedDateTime" />
</property>

<property name="createdBy" node="CreatedBy">
   <column name="CreatedBy" />
</property>

<property name="updatedBy" node="UpdatedBy">
   <column name="UpdatedBy" />
</property>

</class>


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

Code:
Element customer = doc.getRootElement();
arr = (ArrayList) customer.elements();

for(int i=0;i<arr.size();i++){
   //both the following statements work the same way
   //dom4jSession.replicate("sbs.CustomerInfo", arr.get(i), ReplicationMode.OVERWRITE);
   dom4jSession.saveOrUpdate("sbs.CustomerInfo", arr.get(i));
   transaction.commit();   
}
session.flush();


Full stack trace of any exception that occurs: no exception

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

The XML I exported and now am importing

Code:
<?xml version="1.0" encoding="UTF-8"?>

<customers>
  <customer>
    <id>ABC Manufacturing</id>
    <name>ABC Manufacturing</name>
    <createdBy>Administrator</createdBy>
    <locations>
      <sbs.DivPlantInfo>
        <CustomerID>ABC Manufacturing</CustomerID>
        <LocationID>Electrical Systems and Products</LocationID>
        <CreatedBy>Administrator</CreatedBy>
      </sbs.DivPlantInfo>
      <sbs.DivPlantInfo>
        <CustomerID>ABC Manufacturing</CustomerID>
        <LocationID>Plant 1</LocationID>
        <CreatedBy>Administrator</CreatedBy>
      </sbs.DivPlantInfo>
    </locations>
  </customer>
</customers>

_________________
Aisha


Top
 Profile  
 
 Post subject: Found solution to my problem
PostPosted: Tue Mar 21, 2006 10:09 am 
Newbie

Joined: Mon Mar 20, 2006 8:42 am
Posts: 11
The problem was with my mapping file. while mapping a relationship the vale of node attribute should be "."

problem was

<set name="locations" table="CustomerLocation" inverse="true" node="locations" embed-xml="true" cascade="replicate">

Correct mapping would be

<set name="locations" table="CustomerLocation" inverse="true" node="." embed-xml="true" cascade="replicate">

_________________
Aisha


Top
 Profile  
 
 Post subject: How many queries are executed
PostPosted: Thu Jun 29, 2006 1:43 pm 
Newbie

Joined: Wed Apr 12, 2006 1:01 pm
Posts: 5
Hi Fankaar,
I ran your sample code that you posted and I noticed that when I printed the HQL that it was running 1 query for every result set. So it gets all the foriegn keys and then runs a query to retrieve the foreign key elements one by one. Here is an example of the trace (with modifications to point my table names):

Hibernate: select locations0_.pk_ID as pk1_1_, locations0_.pk_ID as pk1_1_0_, locations0_.XML_DATA as XML2_1_0_ from POINTER_CACHE locations0_ where locati
ons0_.pk_ID=?
Hibernate: select locations0_.pk_ID as pk1_1_, locations0_.pk_ID as pk1_1_0_, locations0_.XML_DATA as XML2_1_0_ from POINTER_CACHE locations0_ where locati
ons0_.pk_ID=?
Hibernate: select locations0_.pk_ID as pk1_1_, locations0_.pk_ID as pk1_1_0_, locations0_.XML_DATA as XML2_1_0_ from POINTER_CACHE locations0_ where locati
ons0_.pk_ID=?
Hibernate: select locations0_.pk_ID as pk1_1_, locations0_.pk_ID as pk1_1_0_, locations0_.XML_DATA as XML2_1_0_ from POINTER_CACHE locations0_ where locati
ons0_.pk_ID=?
Hibernate: select locations0_.pk_ID as pk1_1_, locations0_.pk_ID as pk1_1_0_, locations0_.XML_DATA as XML2_1_0_ from POINTER_CACHE locations0_ where locati
ons0_.pk_ID=?
Hibernate: select locations0_.pk_ID as pk1_1_, locations0_.pk_ID as pk1_1_0_, locations0_.XML_DATA as XML2_1_0_ from POINTER_CACHE locations0_ where locati
ons0_.pk_ID=?


Did you see the same thing? Is there a way for hibernate to join these tables relationally behind the scene rather than doing multiple queries?

Thanks,
Yogesh


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 03, 2006 4:03 am 
Newbie

Joined: Mon Mar 20, 2006 8:42 am
Posts: 11
hi ychawla!

I did not notice multiple queries before.

Well i dont think hibernate can do it with single query. My logic is, that objects of child record are nested into parent. From our code we query only the parent and depending upon the mapping hibernate fetches the child records. Which requires it to fetch each matching record separately.

[sorry for late reply]

_________________
Aisha


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.