argos/dmp-backend/core/src/main/java/eu/eudat/configurations/typedefinition/UUIDType.java

118 lines
3.7 KiB
Java

package eu.eudat.configurations.typedefinition;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.usertype.UserType;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.UUID;
/**
* Currently not used
*/
public class UUIDType implements UserType<UUID> {
private final int[] sqlTypesSupported = new int[]{Types.NUMERIC};
private final String CAST_EXCEPTION_TEXT = " cannot be cast to a java.util.UUID";
public int[] sqlTypes() {
return sqlTypesSupported;
}
@Override
public int getSqlType() {
return sqlTypesSupported[0];
}
public Class<UUID> returnedClass() {
return UUID.class;
}
public boolean equals(UUID x, UUID y) throws HibernateException {
if (x == null) return y == null;
else return x.equals(y);
}
public int hashCode(UUID x) throws HibernateException {
return x == null ? -1 : x.hashCode();
}
@Override
public UUID nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) throws SQLException {
assert (position >= 0);
Object value = rs.getObject(position);
if (value == null) return null;
UUID uuid = UUID.fromString(rs.getString(position));
return rs.wasNull() ? null : uuid;
}
public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
assert (names.length == 1);
Object value = rs.getObject(names[0]);
if (value == null) return null;
UUID uuid = UUID.fromString(rs.getString(names[0]));
return rs.wasNull() ? null : uuid;
}
public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
if (value == null) {
st.setNull(index, Types.NULL);
return;
}
if (!UUID.class.isAssignableFrom(value.getClass()))
throw new HibernateException(value.getClass().toString() + CAST_EXCEPTION_TEXT);
UUID uuid = (UUID) value;
st.setObject(index, uuid, Types.OTHER);
}
public UUID deepCopy(UUID value) throws HibernateException {
if (value == null) return null;
return new UUID(value.getMostSignificantBits(), value.getLeastSignificantBits());
}
public boolean isMutable() {
return false;
}
public Serializable disassemble(UUID value) throws HibernateException {
return value;
}
public UUID assemble(Serializable cached, Object owner) throws HibernateException {
return (UUID) cached;
}
public UUID replace(UUID original, Object target, Object owner) throws HibernateException {
return original;
}
// public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner)
// throws HibernateException, SQLException
// {
// return nullSafeGet(rs, names, owner);
// }
//
// public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session)
// throws HibernateException, SQLException
// {
// nullSafeSet(st, value, index);
//
// }
public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner)
throws HibernateException, SQLException {
return nullSafeGet(rs, names, owner);
}
public void nullSafeSet(PreparedStatement st, UUID value, int index, SharedSessionContractImplementor session)
throws HibernateException, SQLException {
nullSafeSet(st, value, index);
}
}