Hi,
I am trying to create hibernate mapping file for saving XML -to database using dom4j.we have one main table for all attribute of parentelement/childelements with only one occurence and seperate tables for child elements with multiple occurences. So i am using one-to-many within collection mappings but its giving me shared references error while persisiting to the database.
I am new to hibernate, any suggestion will be very helpful, i am able to iterate thru using code and save them successfuly but that is taking long time so instead i want to use hibernate collection/associations mapping feature and save it directly
my xml looks like this
<rootelement>
<parentelement>
<childelement1>
<childelement2>
<childelement2>
<childelement3>
<childelement3>
</parentelement>
</rootelement>
below is main hbm/child mapping .
Code:
<hibernate-mapping>
<class table="maintable" entity-name="parentelement" node="parentelement">
<id name="id" type="long" column="lnid" >
<generator class="increment" />
</id>
<!-- parent element Mapping -->
<property
.
.
<component name="childelement1" node="childelement1" >
</component>
<set name="childelements2" node="." cascade="all" embed-xml="false">
<key column="parentid" />
<one-to-many entity-name="childelement2" node="childelement2" />
</set>
<set name="childelements3" node="." cascade="all" >
<key column="parentid" />
<one-to-many entity-name="childelement3" node="childelement3" />
</set>
</class>
<class entity-name="childelement2" table="childtable" node="childelement2">
<id name="id" type="long" column="childid2" >
<generator class="increment"/>
</id>
<property .......
</class>
</hibernate-mapping>
I am using the below code to persist, since we have large xml i am using batch mode to insert into db
Code:
reader.addHandler( "/rootelement/parentelement",
new ElementHandler() {
public void onStart(ElementPath path)
{
count++;
if ((count % 1000 )== 0 ) System.out.println(count);
}
public void onEnd(ElementPath path) {
// process a element
try {
Element row = path.getCurrent();
Session dom4jSession = session.getSession(EntityMode.DOM4J); tx = session.beginTransaction();
dom4jSession.saveOrUpdate("parentelement", row); row.detach();
//saving in batch mode
if ((count % 10) ==0)
{
if (session != null)
{
dom4jSession.flush();
dom4jSession.clear();
tx.commit(); }
}
catch(HibernateException he)
{
he.printStackTrace();
if (session != null)
{
tx.rollback();
session.clear();
session.close();
}