Hi everyone,
Since Hibernate doesn't support the uuid sql type and java (java.util.UUID) and PostgreSQL (version 8.3+) do, I've created a custom UserType and generator, this is a first (working) iteration, is this the correct way to do it?
UUID custom usertype
Code:
public class UuidUserType implements UserType, Serializable {
private UUID uuid;
public UuidUserType() {
super();
}
public UuidUserType(UUID uuid) {
this.uuid = uuid;
}
public void setUuid(UUID uuid) {
this.uuid = uuid;
}
public UUID getUuid() {
return uuid;
}
private static final String CAST_EXCEPTION_TEXT = " cannot be cast to a java.util.UUID.";
/*
* (non-Javadoc)
*
* @see org.hibernate.usertype.UserType#sqlTypes()
*/
public int[] sqlTypes() {
return new int[]{Hibernate.BIG_DECIMAL.sqlType()};
}
/*
* (non-Javadoc)
*
* @see org.hibernate.usertype.UserType#returnedClass()
*/
public Class returnedClass() {
return UuidUserType.class;
}
/*
* (non-Javadoc)
*
* @see org.hibernate.usertype.UserType#equals(java.lang.Object,
* java.lang.Object)
*/
public boolean equals(Object x, Object y) throws HibernateException {
if (x == null && y == null) {
return true;
} else if (x == null || y == null) {
return false;
}
if (!UuidUserType.class.isAssignableFrom(x.getClass())) {
throw new HibernateException(x.getClass().toString() + CAST_EXCEPTION_TEXT);
} else if (!UuidUserType.class.isAssignableFrom(y.getClass())) {
throw new HibernateException(y.getClass().toString() + CAST_EXCEPTION_TEXT);
}
UUID a = ((UuidUserType) x).getUuid();
UUID b = ((UuidUserType) y).getUuid();
return a.equals(b);
}
/*
* (non-Javadoc)
*
* @see org.hibernate.usertype.UserType#hashCode(java.lang.Object)
*/
public int hashCode(Object x) throws HibernateException {
if (!UuidUserType.class.isAssignableFrom(x.getClass())) {
throw new HibernateException(x.getClass().toString() + CAST_EXCEPTION_TEXT);
}
UUID uuid = ((UuidUserType) x).getUuid();
return uuid.hashCode();
}
/*
* (non-Javadoc)
*
* @see org.hibernate.usertype.UserType#nullSafeGet(java.sql.ResultSet,
* java.lang.String[], java.lang.Object)
*/
public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner) throws HibernateException, SQLException {
Object value = resultSet.getObject(names[0]);
if (value == null) {
return null;
} else {
UuidUserType retValue = new UuidUserType();
PGobject pgObject = (PGobject) value;
retValue.setUuid(UUID.fromString(pgObject.getValue()));
return retValue;
}
}
/*
* (non-Javadoc)
*
* @see org.hibernate.usertype.UserType#nullSafeSet(java.sql.PreparedStatement,
* java.lang.Object, int)
*/
public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index) throws HibernateException, SQLException {
if (value == null) {
preparedStatement.setNull(index, Types.NULL);
return;
}
if (!UuidUserType.class.isAssignableFrom(value.getClass())) {
throw new HibernateException(value.getClass().toString() + CAST_EXCEPTION_TEXT);
}
UUID uuidValue = ((UuidUserType) value).getUuid();
String uuidStringValue = uuidValue.toString();
preparedStatement.setObject(index, uuidValue, Types.OTHER);
}
/*
* (non-Javadoc)
*
* @see org.hibernate.usertype.UserType#deepCopy(java.lang.Object)
*/
public Object deepCopy(Object value) throws HibernateException {
return (UuidUserType) value;
}
/*
* (non-Javadoc)
*
* @see org.hibernate.usertype.UserType#isMutable()
*/
public boolean isMutable() {
return false;
}
/*
* (non-Javadoc)
*
* @see org.hibernate.usertype.UserType#disassemble(java.lang.Object)
*/
public Serializable disassemble(Object value) throws HibernateException {
return (Serializable) value;
}
/*
* (non-Javadoc)
*
* @see org.hibernate.usertype.UserType#assemble(java.io.Serializable,
* java.lang.Object)
*/
public Object assemble(Serializable cached, Object owner) throws HibernateException {
return cached;
}
/*
* (non-Javadoc)
*
* @see org.hibernate.usertype.UserType#replace(java.lang.Object,
* java.lang.Object, java.lang.Object)
*/
public Object replace(Object original, Object target, Object owner) throws HibernateException {
return original;
}
/**
* Returns the least significant 64 bits of this UUID's 128 bit value.
*
* @return
*/
@XmlElement
public long getLeastSignificantBits() {
return uuid.getLeastSignificantBits();
}
/**
* Returns the most significant 64 bits of this UUID's 128 bit value.
*
* @return
*/
public long getMostSignificantBits() {
return uuid.getMostSignificantBits();
}
public static UuidUserType fromString(String uuidString) {
return new UuidUserType(UUID.fromString(uuidString));
}
}
Generator:
Code:
public class UuidGenerator implements IdentifierGenerator {
public Serializable generate(SessionImplementor session, Object object) throws HibernateException {
return new UuidUserType(UUID.randomUUID());
}
}