Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Hibernate 3 Adds XML-Relational Persistence : Page 2

Thanks to the XML persistence features in the recently released Hibernate 3, Java developers now have a framework that provides an efficient and consistent method for effortless OR and XML persistence.


advertisement
A Real World Example: Price Catalog Synchronization
A common e-business scenario should demonstrate the usefulness of XML-relational persistence mechanisms. Consider an example in which XML integrates priced product catalogs between online merchants and vendors.

The electronic catalog contains the priced product lists. The online store sells the products, which it manages through its own inventory (similar to Amazon's relationship with Toys-R-Us and sporting goods stores). In order to accurately and efficiently reflect price updates, the online merchant needs to receive frequent product pricing feeds. It receives these feeds as an XML document that looks similar to this:

<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>



The comprehensive master product price list is stored on the database as follows:

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

The online store presents a Web representation of the priced catalog through the existing OR mapping, where the priced products are represented as demo.Product Java objects:

/** Product object represents the priced catalog item. */ public class Product { int id; String sku; String description; Double listPrice; Double basePrice; Double orderPrice;

These objects also are mapped as the following (the column names are listed for clarity, even though Hibernate can auto-map the properties to column names if they match):

<?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="demo"> <class name="Product" table="product" node="product"> <id name="id" type="int" node="@prod_id" column="id"> </id> <property name="sku" node="@sku" column="sku" not-null="true"/> <property name="description" node="description" column="description" not-null="true"/> <property name="listPrice" node="list_price" column="list_price" /> <property name="basePrice" node="drop_price" column="base_price"/> <property name="orderPrice" column="order_price"/> </class> </hibernate-mapping>

In this scenario, Hibernate's XML-relational persistence comes in very handy. As the e-business application receives XML feeds containing the product price updates, it persists them into the product database utilizing Hibernate's XML persistence mechanisms. Hibernate offers several XML persistence alternatives, including the Hibernate saveOrUpdate method:

document = saxReader.read(inputXML); List users = document.selectNodes("//product"); try { Session session = HibernateUtil.sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Session dom4jSession = session.openSession(EntityMode.DOM4J); Iterator iter = users.iterator(); while (iter.hasNext()) { Object next = iter.next(); dom4jSession.saveOrUpdate("demo.Product", next ); }// end while transaction.commit(); session.close();



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap