Author: julien(a)jboss.com
Date: 2006-12-11 06:06:15 -0500 (Mon, 11 Dec 2006)
New Revision: 5792
Added:
trunk/jems/src/main/org/jboss/portal/jems/hibernate/QNameUserType.java
Modified:
trunk/jems/src/main/org/jboss/portal/jems/hibernate/StringWrapperUserType.java
Log:
Added hibernate QNameUserType which maps a QName on two database columns.
Added: trunk/jems/src/main/org/jboss/portal/jems/hibernate/QNameUserType.java
===================================================================
--- trunk/jems/src/main/org/jboss/portal/jems/hibernate/QNameUserType.java 2006-12-11
10:15:31 UTC (rev 5791)
+++ trunk/jems/src/main/org/jboss/portal/jems/hibernate/QNameUserType.java 2006-12-11
11:06:15 UTC (rev 5792)
@@ -0,0 +1,140 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.jems.hibernate;
+
+import org.hibernate.usertype.UserType;
+import org.hibernate.HibernateException;
+import org.hibernate.Hibernate;
+
+import javax.xml.namespace.QName;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.PreparedStatement;
+import java.sql.Types;
+import java.io.Serializable;
+
+/**
+ * An hibernate user type to persist the
<code>javax.xml.namespace.QName</code> class on two database columns.
+ *
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class QNameUserType implements UserType
+{
+
+ /** . */
+ private static final int[] SQL_TYPES =
{Hibernate.STRING.sqlType(),Hibernate.STRING.sqlType()};
+
+ public int[] sqlTypes()
+ {
+ return SQL_TYPES;
+ }
+
+ public Class returnedClass()
+ {
+ return QName.class;
+ }
+
+ public boolean equals(Object x, Object y) throws HibernateException
+ {
+ if (x == y)
+ {
+ return true;
+ }
+ else if (x == null || y == null)
+ {
+ return false;
+ }
+ else
+ {
+ return x.equals(y);
+ }
+ }
+
+ public int hashCode(Object x) throws HibernateException
+ {
+ return x.hashCode();
+ }
+
+ public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner) throws
HibernateException, SQLException
+ {
+ // Test nullity on local part
+ String localPart = resultSet.getString(names[1]);
+ if (resultSet.wasNull())
+ {
+ return null;
+ }
+
+ // The namespace URI
+ String namespaceURI = resultSet.getString(names[0]);
+
+ // Oracle considers that the empty string is null (very nice feature, clap clap),
so we replace null by empty string
+ if (namespaceURI == null)
+ {
+ namespaceURI = "";
+ }
+
+ //
+ return new QName(namespaceURI, localPart);
+ }
+
+ public void nullSafeSet(PreparedStatement statement, Object value, int index) throws
HibernateException, SQLException
+ {
+ if (value == null)
+ {
+ statement.setNull(index, Types.VARCHAR);
+ statement.setNull(index + 1, Types.VARCHAR);
+ }
+ else
+ {
+ QName qname = (QName)value;
+ statement.setString(index, qname.getNamespaceURI());
+ statement.setString(index + 1, qname.getLocalPart());
+ }
+ }
+
+ public Object deepCopy(Object value) throws HibernateException
+ {
+ return value;
+ }
+
+ public boolean isMutable()
+ {
+ return false;
+ }
+
+ public Serializable disassemble(Object value) throws HibernateException
+ {
+ return (QName)value;
+ }
+
+ public Object assemble(Serializable cached, Object owner) throws HibernateException
+ {
+ return cached;
+ }
+
+ public Object replace(Object original, Object target, Object owner) throws
HibernateException
+ {
+ return original;
+ }
+}
Modified: trunk/jems/src/main/org/jboss/portal/jems/hibernate/StringWrapperUserType.java
===================================================================
---
trunk/jems/src/main/org/jboss/portal/jems/hibernate/StringWrapperUserType.java 2006-12-11
10:15:31 UTC (rev 5791)
+++
trunk/jems/src/main/org/jboss/portal/jems/hibernate/StringWrapperUserType.java 2006-12-11
11:06:15 UTC (rev 5792)
@@ -65,18 +65,18 @@
}
}
- public int hashCode(Object object) throws HibernateException
+ public int hashCode(Object x) throws HibernateException
{
- return object.hashCode();
+ return x.hashCode();
}
public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner) throws
HibernateException, SQLException
{
+ String value = resultSet.getString(names[0]);
if (resultSet.wasNull())
{
return null;
}
- String value = resultSet.getString(names[0]);
return fromString(value);
}