Skip to main content

Returning Apache XMLBeans from a Jdbc Control

This topic assumes a strong understanding of Apache XML Beans. For additional information about XML Bean see the Apache XML Beans Site http://xmlbeans.apache.org/.

The following topic explains how to return XMLBean types from custom Jdbc controls.

An XMLBean is essentially an XML document with a Java API attached to it. The API is used for parsing and manipulating the data in the XML document.

The data can be accessed and manipulated using the XMLBean's API. For example, assume that custBean represents the XML document above. The following Java code extracts the Fred Williams from the document.
String name = custBean.getXCustomer().getXCustomerRowArray(1).getNAME();

Retrofitting database controls to return XMLBeans rather than RowSets, ResultSets, or Iterators, is a powerful technique because there are few restrictions on where XMLBeans can be imported. This is not the case with ResultSets and Iterators, which cannot be passed directly to web-tier classes (web services and page flows). Also, data in XMLBean form is very easy to manipulate because there is a rich API attached to the XMLBean.

Creating a Schema

The first step in using XMLBean classes is creating a schema from which the XMLBean classes can be generated. The schema you create for a database control must be capable of modeling the sorts of data returned from the database.

If you write your own schema, at a minimum, the schema's elements should have the same names as the fields in the database, which allows data returned from the database to be automatically mapped into the XMLBean.

When the XSD file is compiled, XMLBean types are generated that can be returned by the methods in the database control.

Editing Schemas to Create New "Document" Types

Note that only one of the generated types is a "Document" XMLBean type: XCustomerDocument. The other types, XCustomerDocument.XCustomer and XCustomerDocument.XCustomer.XCustomerRow, can only be used with reference to the "Document" type. This distinction is especially important because only "Document" types are eligible for direct participation in a business process, or to be passed to a web service. For this reason you may want to edit your schema to include "Document" types corresponding to other types in the Schema, especially if you have a very large schema with many nested types defined in terms of a single "Document" type.

To generate a new Document type for some element, move that element so that it becomes a top-level element in the schema. In the following example, the XCustomerRow element has been moved to the top-level of the schema: its original position has been replaced with a reference element:

There are now two top-level elements, XCustomer and XCustomerRow, which compile into two corresponding "Document" types: XCustomerDocument and XCustomerRowDocument.

Returning a XMLBean Types from Control Methods

Once you have generated XMLBean types that model the database data, you can import these types into your Jdbc control.

import databaseCustomerDb.XCustomerDocument;
import databaseCustomerDb.XCustomerDocument.XCustomer;
import databaseCustomerDb.XCustomerDocument.Factory;

XMLBean types can be returned from the control's methods.

@SQL(statement="SELECT custid, name, address FROM customer")
public XCustomerDocument findAllCustomersDoc();

The data returned from the query is automatically mapped into the XMLBean because the names of the database fields match the fields of the XMLBean.


Here is the apache beehive url: http://beehive.apache.org/docs/1.0.2/system-controls/jdbc/guide.html#Returning+Apache+XMLBeans+from+a+Jdbc+Control

Comments

Popular posts from this blog

Running and debugging jetty maven plugin in Netbeans

Coding with Netbeans will make developer life simple. We don't need search for plugins and install different plugins from different places. Maven projects can opened as directly in Netbeans without any settings. Here is the way to run & debug using maven jetty plugin. We can't add Jetty server as servers in Netbeans. Run project: Right click on project and open the properties. click on "Actions" on left side. On right side there will be list of available actions.  To run jetty can be created a new action or override default "Run Project" Here I am overriding default one, Because I can run the project just by hitting "F6" key click on "Run Project" Enter the value "Execute Goals" as "jetty:run", If you want run as offline add "jetty:run -o" "Active Profiles" can be empty In "set Properties" field we can set like skip test. Here I am skipping test. Done, After setting y

remove(unset) property in a property file using ANT PropertyFile task

Using PropertyFile task we can edit the property file during ANT build. Edit property value are very easy and can find here details. http://ant.apache.org/manual/Tasks/propertyfile.html but delete a property or comment a property is the tricky one. If we are using latest ant version (1.8.1 or later), we can delete a property like below. <propertyfile file="my.properties" comment="My properties">   < entry  key="propertykey" operation="del"/> < /propertyfile> but the older version that is before 1.8.1 don't have operation called " del ", if we run the command in lower version ant, we will get a error says undefined operation "del".      there is workaround instead deleting a property we can comment that property using ant's replace command. < replace file="sample.properties">                      < replacefilter token="propertykey" value="#propert

Disable hibernate Optimistic Lock for specific scenario

Optimistic Lock gives more control on concurrent modification on data. That can be achieved very easily in hibernate using @Version column. But in some cases we may have to avoid concurrency check, example background process. To disable Optimistic lock set OptimisticLockMode as NONE for entity class while hibernate is initiating. We can't remove version column that needs Database changes and ORM changes. In this way we can disable Optimistic Lock by code. implement "org.hibernate.event.Initializable" interface override the below method.     @Override     public void initialize(Configuration cfg) {         Iterator<?> persistentClassIterator = cfg.getClassMappings();         while (persistentClassIterator.hasNext()) {             PersistentClass persistentClass = PersistentClass.class.cast(persistentClassIterator.next());                     Iterator propertyIterator = persistentClass.getDeclaredPropertyIterator();                    while (proper