These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: UserType mapping class for oracle XMLType
PostPosted: Tue Jul 06, 2004 1:52 pm 
Beginner
Beginner

Joined: Thu Feb 05, 2004 10:39 pm
Posts: 44
I just wanted to get a critique of the XMLType UserType class that I have written. Note, I have actually not yet actually used it, but am in the process of integrating it with out hibernate project.

I just wanted to put it up here and get some feedback from the community about the UserType class itself (if it looks correct) and about the XMLType itself. I did look at the threads here about XMLType but no one actually posted an entire class.

My questions would be:
1. Does the UserType class structure look correct and have I implemented the methods to do the intended tasks.
2. Does the conversion logic from and to XMLType and Clob look correct? (I have mapped my classes to use the Hibernate Clob type and this class to use the sql Clob type, will these two work correctly together?)

Thanks for any input.

So, here it is:

Code:
import java.io.Serializable;
import java.sql.Clob;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import net.sf.hibernate.Hibernate;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.UserType;
import oracle.jdbc.OraclePreparedStatement;
import oracle.jdbc.driver.OracleResultSet;
import oracle.sql.OPAQUE;


public class XMLType implements UserType, Serializable {
   
   private static final Class returnedClass = XMLType.class;
   private static final int[] SQL_TYPES = new int[] { oracle.xdb.XMLType._SQL_TYPECODE };
   
   /* (non-Javadoc)
    * @see net.sf.hibernate.UserType#sqlTypes()
    */
   public int[] sqlTypes() {
      return SQL_TYPES;
   }

   /* (non-Javadoc)
    * @see net.sf.hibernate.UserType#returnedClass()
    */
   public Class returnedClass() {
      return returnedClass;
   }

   /* (non-Javadoc)
    * @see net.sf.hibernate.UserType#equals(java.lang.Object, java.lang.Object)
    */
   public boolean equals(Object arg0, Object arg1) throws HibernateException {
      
      if(arg0 == null || arg1 == null){
         throw new HibernateException("None of the arguments can be null.");
      }
      
      if(arg0 instanceof oracle.xdb.XMLType
         && arg1 instanceof oracle.xdb.XMLType){
         
         return arg0.equals(arg1);
      }
      return false;
   }

   /* (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 = (OracleResultSet) rs;
         OPAQUE op = ors.getOPAQUE(names[0]);               
         oracle.xdb.XMLType xt= oracle.xdb.XMLType.createXML(op);
      
         return xt.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 {
      
      Clob aClob = Hibernate.createClob((String) value);
      OraclePreparedStatement ost = (OraclePreparedStatement) st;
      
      ost.setOPAQUE(index, oracle.xdb.XMLType.createXML((OPAQUE) aClob));
   }

   /* (non-Javadoc)
    * @see net.sf.hibernate.UserType#deepCopy(java.lang.Object)
    */
   public Object deepCopy(Object value) throws HibernateException {
      
      if (value == null) return null;
      
      Clob aClob = Hibernate.createClob((String) value);
      try{
         return oracle.xdb.XMLType.createXML((OPAQUE) aClob);
      }catch(SQLException e){
         throw new HibernateException(e);
      }
            
   }

   /* (non-Javadoc)
    * @see net.sf.hibernate.UserType#isMutable()
    */
   public boolean isMutable() {
      return false;
   }

}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 06, 2004 6:35 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You should not make the returned Class the type class itself, because this gets you tight coupling between UserType and the persistent Class. Instead you should use the UserType as an adapter between the property type and the db type.


Top
 Profile  
 
 Post subject: Re: UserType mapping class for oracle XMLType
PostPosted: Tue Jul 12, 2005 3:33 pm 
Beginner
Beginner

Joined: Tue Nov 02, 2004 1:34 pm
Posts: 45
single wrote:
Thanks for any input.


So how did this code work for you? I know it's been a year since you wrote it, but there doesn't seem to be a whole lotta help on this topic available.

Lee


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 12, 2005 3:38 pm 
Beginner
Beginner

Joined: Thu Feb 05, 2004 10:39 pm
Posts: 44
leebase,

The project I was working on took a different direction and the XMLType I had written never saw the light of day...how typical. From what I remember another Dev took over this piece from me and made changes although I don't know what those were to make this piece work.

Sorry, that I can't really provide you with anything working.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.