Member Menu
 
 Monthly JBoss newsletter:
 
Hibernate Books
CaveatEmptor

Mapping Oracle XMLType to DOM4J Element

XML type representation for Hibernate can be mapped to DOM4J Element type. Implementation will be based on Oracle Drivers Only. This example provides a simple implementation that will work on weblogic platform or with plain simple oracle connections.

The idea is to be able to create simple realtional-xml databases. By using a XML type the finders can use XPath and search troughout the XMLTypes. This is a very powerfull concept as it combines the relational and XML based structures into one simple XML-Relational database.

package mypackage;
....

public class HibernateXMLType implements UserType ,Serializable{
.....

/*

     * (non-Javadoc)

     * 

     * @see net.sf.hibernate.UserType#nullSafeGet(java.sql.ResultSet,

     *      java.lang.String[], java.lang.Object)

     */

    public Object nullSafeGet( ResultSet rs, String [ ] names, Object arg2 ) throws HibernateException, SQLException {

        OracleResultSet ors = null;

        if ( rs instanceof weblogic.jdbc.wrapper.ResultSet ) {

            ors = (OracleResultSet) getNativeResultSet ( rs );

        } else if ( rs instanceof OracleResultSet ) {

            ors = (OracleResultSet) rs;

        } else {

            throw new UnsupportedOperationException (

                    "Only direct support for Weblogic and Oracle drivers. Please check your pool driver." );

        }

        OPAQUE op = ors.getOPAQUE ( names[0] );

        oracle.xdb.XMLType xt = oracle.xdb.XMLType.createXML ( op );

        DOMReader reader = new DOMReader ();

        org.dom4j.Document document = reader.read ( xt.getDOM () );

        return document.getRootElement ();// getClobVal();

    }

    /*

     * (non-Javadoc)

     * 

     * @see net.sf.hibernate.UserType#nullSafeSet(java.sql.PreparedStatement,

     *      java.lang.Object, int)

     */

    public void nullSafeSet( PreparedStatement st, Object value, int index ) throws HibernateException, SQLException {

        String conName = "";

        try {

            OraclePreparedStatement ost = null;

            if ( st instanceof OraclePreparedStatement ) {

                ost = (OraclePreparedStatement) st;

            } else {

                throw new HibernateException ( "PreparedStatement object must be a OraclePreparedStatement !! "

                        + "Connection class is " + st.getClass ().getName () );

            }

            Connection conn = st.getConnection ();

            conName = conn.getClass ().getName ();

            // Get the oracle connection class for checking

            Class oracleConnectionClass = Class.forName ( "oracle.jdbc.driver.OracleConnection" );

            // Make sure connection object is right type

            if ( !oracleConnectionClass.isAssignableFrom ( conn.getClass () ) ) {

                if ( conn instanceof PoolConnection ) {

                    try {

                        conn = getNativeConnection ( st.getConnection ().getMetaData ().getConnection () );

                        if ( !oracleConnectionClass.isAssignableFrom ( conn.getClass () ) ) {

                            throw new HibernateException (

                                    "JDBC connection object must be a oracle.jdbc.OracleConnection !! "

                                            + "Connection class is " + conn.getClass ().getName () );

                        }

                    } catch ( Exception sEx ) {

                        throw new HibernateException (

                                "JDBC connection object must be a oracle.jdbc.OracleConnection !! "

                                        + "Connection class is " + conn.getClass ().getName () );

                    }

                } else

                    throw new UnsupportedOperationException (

                            "Only direct support for Weblogic and Oracle drivers. Please check your pool driver." );

            }

            if ( oracleConnectionClass.isAssignableFrom ( conn.getClass () ) ) {

                ost.setOPAQUE ( index, oracle.xdb.XMLType.createXML ( conn, ( (Element) value ).asXML () ) );// (OPAQUE)

            } else {

                throw new HibernateException ( "JDBC connection object must be a oracle.jdbc.OracleConnection !! "

                        + "Connection class is " + conn.getClass ().getName () );

            }

        } catch ( SQLException e ) {

            e.printStackTrace ();

            throw e;

        } catch ( HibernateException e ) {

            e.printStackTrace ();

            throw e;

        } catch ( ClassCastException e ) {

            e.printStackTrace ();

            // could not find the class with reflection

            throw new HibernateException ( "Unable to case a required class.\n" + e.getMessage () + "  -- value class:"

                    + conName );

        } catch ( ClassNotFoundException e ) {

            // could not find the class with reflection

            throw new HibernateException ( "Unable to find a required class.\n" + e.getMessage () );

        }

    }

....
}

Have fun....

http://www.coena.com

© Copyright 2006, Red Hat Middleware, LLC. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc. [Privacy Policy]