1 package org.jbpm.bpel.hibernate; 2 3 import java.io.Serializable ; 4 import java.sql.PreparedStatement ; 5 import java.sql.ResultSet ; 6 import java.sql.SQLException ; 7 import java.sql.Types ; 8 9 import javax.xml.namespace.QName ; 10 11 import org.hibernate.Hibernate; 12 import org.hibernate.HibernateException; 13 import org.hibernate.usertype.UserType; 14 15 19 public class QNameType implements UserType { 20 21 private static final int[] SQL_TYPES = new int[] {Types.VARCHAR, Types.VARCHAR}; 22 public int[] sqlTypes() { 23 return SQL_TYPES; 24 } 25 26 public boolean equals(Object x, Object y) throws HibernateException { 27 return (x == null) ? (y == null) : ((QName )x).equals(y); 28 } 29 30 public boolean isMutable() { 31 return false; 32 } 33 34 public Class returnedClass() { 35 return QName .class; 36 } 37 38 public int hashCode(Object x) throws HibernateException { 39 return x.hashCode(); 40 } 41 42 public Object deepCopy(Object value) throws HibernateException { 43 return value; 44 } 45 46 public Serializable disassemble(Object value) throws HibernateException { 47 return (Serializable ) value; 48 } 49 50 public Object assemble(Serializable cached, Object owner) throws HibernateException { 51 return cached; 52 } 53 54 public Object replace(Object original, Object target, Object owner) throws HibernateException { 55 return target; 56 } 57 58 61 public Object nullSafeGet(ResultSet rs, String [] names, Object owner) 62 throws HibernateException, SQLException { 63 String namespaceURI = (String ) Hibernate.STRING.nullSafeGet(rs, names[0]); 64 String localPart = (String ) Hibernate.STRING.nullSafeGet(rs, names[1]); 65 return (namespaceURI != null || localPart != null) ? new QName (namespaceURI, localPart) : null; 66 } 67 68 71 public void nullSafeSet(PreparedStatement st, Object value, int index) 72 throws HibernateException, SQLException { 73 if(value == null) { 74 st.setNull(index, Types.VARCHAR); 75 st.setNull(index + 1, Types.VARCHAR); 76 } 77 else if (returnedClass().isAssignableFrom(value.getClass())) { 78 QName qname = (QName ) value; 79 st.setString(index, qname.getNamespaceURI()); 81 st.setString(index + 1, qname.getLocalPart()); 82 } 83 else { 84 throw new IllegalArgumentException ("Received value is not a[" + 86 returnedClass().getName() + "] but [" + value.getClass() + "]"); 87 } 88 } 89 } 90 | Popular Tags |