Application of ing DB2 pureXML data through Hibernate

Source: Internet
Author: User
Tags xquery

This article mainly introduces how to use Hibernate to simplify the actual development of DB2 pureXML-based applications. We mainly map the data of DB2 pureXML through N examples to reduce the development difficulty. The following is a detailed description of the main content of the article. I hope you will have a better understanding of it after browsing.

Prerequisites

Readers need to have a certain understanding of the Hibernate framework and DB2 pureXML-related technologies, such as SQL/XML and XQuery. This article is based on DB2 V9.5 and Hibernate 3.3.1GA. Since the example XML table in the Sample database of DB2 V9.7 does not contain namespaces, if you use DB2 V9.7, you need to remove the namespace declaration from the Sample code.

Hibernate Introduction

Nowadays, enterprise-level applications are developed in an object-oriented manner. The rich resources of the Java Community provide both product quality and development efficiency for the development process. However, at the database level, for various reasons, such as usage habits and performance considerations, we still follow the data model design of "entity-relation, cut a three-dimensional object into a table structure of each plane, and then associate these tables. On the other hand, the development of the database itself has also encountered a bottleneck, and there is no good way to directly manage objects. This article refers to Java-class instances ), provides object-based CRUD (add, delete, modify, and query) operations and transaction control.

Based on the above considerations, open-source Hibernate, as an object ing framework of relational databases, has become the first choice for enterprise-level application developers. Developers often use Hibernate as the adhesive to bond Java objects with relational databases, eliminating the hassle of manual ing.

Application of DB2 pureXML

Java and XML are natural allies. Both of them can be used to describe complex objects, and the conversion between them is also easy. There are many open-source projects that support Java object to XML serialization and XML document to Java object deserialization (de-serialization ). Java parses XML documents using SAX and DOM and performs corresponding operations.

If you have two choices for Java designers, one is to split the Java object into several complex tables, which are closely related, and the other is to serialize the Java object into XML directly, then it is saved to the database, and the performance of database operations such as insertion and query is ensured. I think few people can reject the second choice.

DB2 pureXML can exert its unique value in this context. You can directly insert an XML document into a database without splitting it into a relational table. You can retrieve the entire document from the database during query, you can also extract only several node elements of an XML document and use one or more elements as the query conditions. When data is updated, the entire XML document can be updated or only several elements of the document can be updated. These operations are based on industry-standard XQuery and SQL/XML. For more information about the data operation interfaces and methods of DB2 pureXML, see the relevant articles.

These features undoubtedly give programmers a lot of space to imagine and design flexibility.

How does Hibernate and DB2 pureXML coexist?

As An ORM (Object-Relational Mapping) tool, Hibernate first considers the ing between Object and Relational table Relational. The SQL/XML and XQuery interfaces used by DB2 pureXML are not directly supported in Hibernate. It seems that to use DB2 pureXML, only

Abandon the Hibernate framework and use JDBC to implement data access interfaces.

After some exploration, I found that this is not the case. In Hibernate 3.3.1GA, the vast majority of DB2 pureXML operations can be supported. However, these support methods are not familiar yet. The rest of this article will introduce in detail how to develop DB2 pureXML applications on the Hibernate framework.

While reading the following content, you can refer to the sample code to try it. Before you begin, we recommend that you create a sample database containing the XML sample table. Run db2sampl-xml in db2cmd. The XML sample table is created under the current user schema.

Various query and object ing methods

In general, the object ing of XML documents in Java generally adopts the following methods:

Maps to a DOM object;

Extract some elements from the XML document and map them to common Java classes;

For complex and multi-level XML documents, map them into several referenced Java classes;

Maps an XML document to an instance of a dynamic class.

In Hibernate, you can have the following richer options. Of course, these options are just extensions of the above method.

Retrieve XML documents as a whole

Hibernate supports direct ing of XML documents to the java. lang. String type. Therefore, if you want to obtain the entire XML document, you can return the XML document as a Java String object in this way, and then use JDOM and other tools in the Java program to further process the object. This method is similar to that for CLOB data types.

In addition, Hibernate allows you to customize the data type, which interacts directly with XML columns in the database. For more information, see https://www.hibernate.org/466.html. this article describes how to compile a Custom User role to map XML columns in DB2 into DOM objects.

Native SQL query

The common Query Language of Hibernate is HQL, but it also supports native database SQL. You can directly write native SQL statements to support SQL statements with unique database features. As shown in List 1, you can write SQL/XML to read the/customerinfo/name node and the Cid attribute under/customerinfo.

The above content is an introduction to ing DB2 pureXML data using Hibernate. I hope you will get some benefits.

Related Article

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.