All pastes #1883276 Raw Edit

Hibernate PostgreSQL UUID Suppor

public java v1 · immutable
#1883276 ·published 2010-06-15 00:14 UTC
rendered paste body
package hypernova.model.persistence;import java.io.Serializable;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Types;import java.util.UUID;import org.hibernate.HibernateException;import org.hibernate.usertype.UserType;public class UUIDUserType implements UserType {	@SuppressWarnings("unchecked")	public Class returnedClass() {		return UUID.class;	}	public int[] sqlTypes() {		return new int[] {			Types.OTHER		};	}	public boolean equals(Object x, Object y) throws HibernateException {		return (x == y) || (x != null && y != null && (x.equals(y)));	}	public Object nullSafeGet(ResultSet rs, String[] names, Object owner)			throws HibernateException, SQLException {		return rs.getObject(names[0]);	}	public void nullSafeSet(PreparedStatement st, Object value, int index)			throws HibernateException, SQLException {		if (value == null) {			st.setNull(index, Types.OTHER);			return;		} else {			st.setObject(index, value);		}	}	public Object assemble(Serializable cached, Object owner)			throws HibernateException {		return deepCopy(cached);	}	public Serializable disassemble(Object value) throws HibernateException {		return (UUID) deepCopy(value);	}	// UUID is immutable, so we do not copy it actually	public Object deepCopy(Object value) throws HibernateException {		return (UUID) value;	}	public int hashCode(Object x) throws HibernateException {		return x.hashCode();	}	public boolean isMutable() {		return false;	}	public Object replace(Object original, Object target, Object owner)			throws HibernateException {		return original;	}}