Introduction to XML link persistence in Hibernate 3

Source: Internet
Author: User
Tags configuration settings xml attribute

Thanks to the XML persistence feature recently released in Hibernate 3, Java developers now have a framework component, which is an easy-to-implement object relationship (OR) XML persistence provides an efficient and consistent method.

The ease of use, high performance, object link persistence, and other advanced features of Hibernate bring great surprises to the IT industry. The latest version of Hibernate (released on March 29 ,)ProductAPIS bring about an important new feature: XML persistence. With Hibernate 3, Java application developers can easily merge XML documents into relational databases.

This new feature should clearly inform existing Hibernate developers, because it also follows the same consistency method of POJO (pure old Java object) and requires the least knowledge to be learned. The advantages of XML persistence should also be introduced to new users. This article describes the persistence method of Hibernate 3.

Why is XML persistence important?

Most large commercial databases support some form of local XML persistence. XML persistence is a relatively new mechanism-even for large vendors, standards in this field are constantly emerging. The result is that to integrate the ubiquitous relational persistence mechanism with the increasing XML solutions, architects must rely on vendor-specific features or implement custom XML persistence framework components. Neither of these two choices is too attractive. The vendor-specific features are not universal, because lock-in may occur, and the implementation of customized framework components may consume a lot of time and money, making the code difficult to maintain.

Hibernate XML persistence is a natural solution for OR (Object relationship) persistence. It can move across all relational platforms supported by Hibernate (such as virtual or real relational platforms, allows free migration of objects, XML-based applications, and integration solutions without worrying about lower-level relational implementation methods.

Detailed information about the architecture

Hibernate is a framework component with a good architecture. It seamlessly utilizes the local environment and does not require any special interference or installation operations. To switch from a database to another database, you usually only need to change the driver and configure Hibernate (online configuration settings) to use another database language.

Hibernate uses the dom4j framework component for XML analysis and maintenance. To fully utilize the XML features of Hibernate, you must be familiar with dom4j. In general, you will find that dom4j is easier to use than JAXP provided by Java or XML analyzer compatible with JAXP. It requires us to learn less about the relevant knowledge, and you can use Hibernate XML persistence efficiently with the least dom4j knowledge.

   Actual Example: price directory synchronization

General e-commerce cases can demonstrate the role of the XML link persistence mechanism. Let's consider an example. In this example, XML integrates the product price directory between online retailers and suppliers.

This electronic directory contains the list of products that have been priced. An online store sells products and manages them through its own inventory list (similar to the relationship between Amazon and Toys-R-Us and a sports product store ). To accurately and effectively reflect price changes, online retailers must frequently receive product price information. It stores the information as an XML document, as shown below:

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

A comprehensive list of major product prices is stored in the database, as shown below:

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

Online retailers provide a Web representation of the Pricing Catalog through existing OR ing. All the pricing products are demo. Product Java objects:

/** The Product object shows the pricing directory items */
Public class Product {
Int id;
String sku;
String description;
Double listPrice;
Double basePrice;
Double orderPrice;

These objects are mapped as follows (for clarity, the column names are listed, although Hibernate can automatically map the attributes to the column names when the attributes and column names 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 case, the persistence of the XML relationship of Hibernate is very convenient. Because the e-commerce application receives XML containing product price updates, it writes these XML into the product database using the XML persistence mechanism of Hibernate. Hibernate provides several XML persistence options, including the saveOrUpdate method of Hibernate:

document = saxReader.read(inputXML);
List users = document.selectNodes("//product");
try {
  Session session = ibernateUtil.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(); 

   XML ing syntax

The ing file used in the above example is not used for the Hibernate 2 ing file. Hibernate 3 introduces several new ing types dedicated for XML persistence.

The main new ing attribute is node, which is associated with an element or attribute in the mapped XML document.

A "Node" may display some of the following mappings:

· "Element-name (element name)": In this example, the <product> </product> element is expressed as node = "product ".

· "@ Attribute-name (attribute name)": In this example, node = "@ sku" is mapped to an XML attribute <product sku = "1001001">.

· ". (Period)": maps to the parent element of an element (for example, <products> indicates the parent element of <product> ).

· "Element-name/@ attribute-name (element name/attribute name)": the property mapped to the named element (product/@ sku ).

   XML persistence is not the main task of Hibernate.

The Hibernate 3 framework component efficiently implements some of the most common methods currently (except LDAP ). The Java Community now has a set of framework components that provide an efficient and consistent method for easily implementing OR and XML persistence.

After learning about the above content, it is very important to understand the tasks of the Hibernate project. Although the XML features of Hibernate 3 are very useful and attractive, they are not used to replace the most popular XML grouping and transformation framework components. No matter how well its OR ing solution is, we should not expect Hibernate to become the mainstream XML maintenance framework component (according to the speech by Gavin King, author of Hibernate, on TheServerSide Java Symposium 2005 ).

For this reason, you should regard the XML persistence feature as a useful extension of the powerful Hibernate framework component, it allows you to easily merge other popular data performance mechanisms into your own applications. However, if you have to deal with complex integration and conversion situations, it is best to look for other XML-specific framework components.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.