advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
Partners & Affiliates
advertisement
advertisement
advertisement
Average Rating: 4.1/5 | Rate this item | 11 users have rated this item.
Hibernate 3 Adds XML-Relational Persistence (cont'd)
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.
advertisement

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();

Previous Page: Introduction Next Page: XML Mapping Syntax
Page 1: IntroductionPage 3: XML Mapping Syntax
Page 2: A Real World Example: Price Catalog Synchronization 
Please rate this item (5=best)
 1  2  3  4  5
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs