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>