-->
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.  [ 1 post ] 
Author Message
 Post subject: problem in mapping a xml file for persistence
PostPosted: Tue Aug 04, 2009 5:28 am 
Newbie

Joined: Mon Jul 27, 2009 2:37 am
Posts: 1
i am trying to persist a xml file. I am facing a problem in getting the data at the parent node.
Code is :-

database table
Code:
CREATE TABLE PRODUCT
(
  id INT UNIQUE NOT NULL,
  description VARCHAR(45) ,
  sku VARCHAR(45) ,
  list_price FLOAT,
  base_price FLOAT,
  order_price FLOAT,
  CONSTRAINT PK_PRODUCT PRIMARY KEY (id )
)


xml file being persisted
Code:
<products>
   <product prod_id="3" sku="100101">            
      <description>Athlete mode body fat scale</description>
      <list_price>100.00</list_price>
      <drop_price>60.00</drop_price>
   </product>   
   <product prod_id="4" sku="100102">
      <description>Thermometer</description>
      <list_price>20.00</list_price>
      <drop_price>11.00</drop_price>
   </product>
</products>


mapping file
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 package="xmldb">
   <class entity-name="xmldb" table="product" node="product">
      <id name="id" type="int" node="@prod_id" column="id"/>
      <property name="sku" node="@sku" type="string" column="sku" not-null="true"/>
      <property name="description" node="description" type="string" column="description" not-null="true"/>
      <property name="listPrice" node="list_price" type="float" column="list_price" />
      <property name="basePrice"  node="drop_price" type="float" column="base_price"/>
      <property name="orderPrice" node=".@op" type="float" column="order_price" />
         </class>   
</hibernate-mapping>


source code for persisting
Code:
package xmldb;

import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.io.SAXReader;
import org.hibernate.EntityMode;
import org.hibernate.Session;

import Util.HibernateUtil;

public class mymain {
   public static void main(String[] args) {
      try
      {
         // TODO Auto-generated method stub
         SAXReader saxReader = new SAXReader();
         Document document = saxReader.read("src\\xmldb\\myxml.xml");
         List users = document.selectNodes("//product");
         
         Session s = HibernateUtil.getSessionFactory().openSession();
         Session xmls = s.getSession(EntityMode.DOM4J);
         xmls.beginTransaction();
         Iterator iter = users.iterator();
         Object next= null;
         while (iter.hasNext())
         {
            next = iter.next();
            xmls.saveOrUpdate("xmldb",next);
            
         }
         xmls.getTransaction().commit();
         xmls.close();
         s.close();
      }
      catch (Exception ex)
      {
         System.out.println("An error occurred.\n"+ex);
         ex.printStackTrace();
      }
      
   }


}



i am trying to get the order price which is in the parent node of the node that i am persisting. the hibernate documentation also says that for parent node you can use "."(period)
but i am not able to save the order price.

Thanks for help in advance....


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.