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

105 lines
3.3 KiB
Java
Raw Normal View History

2018-06-27 12:29:21 +02:00
package eu.eudat.configurations.typedefinition;
2017-12-15 00:01:26 +01:00
2018-02-16 11:34:02 +01:00
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.usertype.UserType;
2017-12-15 00:01:26 +01:00
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
*/
2018-02-16 11:34:02 +01:00
public class UUIDType implements UserType {
private final int[] sqlTypesSupported = new int[]{Types.NUMERIC};
private final String CAST_EXCEPTION_TEXT = " cannot be cast to a java.util.UUID";
2017-12-15 00:01:26 +01:00
2018-02-16 11:34:02 +01:00
public int[] sqlTypes() {
2017-12-15 00:01:26 +01:00
return sqlTypesSupported;
}
@SuppressWarnings("rawtypes")
2018-02-16 11:34:02 +01:00
public Class returnedClass() {
2017-12-15 00:01:26 +01:00
return UUID.class;
}
2018-02-16 11:34:02 +01:00
public boolean equals(Object x, Object y) throws HibernateException {
2017-12-15 00:01:26 +01:00
if (x == null) return y == null;
else return x.equals(y);
}
2018-02-16 11:34:02 +01:00
public int hashCode(Object x) throws HibernateException {
2017-12-15 00:01:26 +01:00
return x == null ? null : x.hashCode();
2018-02-16 11:34:02 +01:00
}
2017-12-15 00:01:26 +01:00
2018-02-16 11:34:02 +01:00
public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
assert (names.length == 1);
2017-12-15 00:01:26 +01:00
Object value = rs.getObject(names[0]);
if (value == null) return null;
2018-02-16 11:34:02 +01:00
UUID uuid = UUID.fromString(rs.getString(names[0]));
2017-12-15 00:01:26 +01:00
return rs.wasNull() ? null : uuid;
}
2018-02-16 11:34:02 +01:00
public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
if (value == null) {
2017-12-15 00:01:26 +01:00
st.setNull(index, Types.NULL);
return;
2018-02-16 11:34:02 +01:00
}
2017-12-15 00:01:26 +01:00
if (!UUID.class.isAssignableFrom(value.getClass()))
2018-02-16 11:34:02 +01:00
throw new HibernateException(value.getClass().toString() + CAST_EXCEPTION_TEXT);
2017-12-15 00:01:26 +01:00
UUID uuid = (UUID) value;
st.setObject(index, uuid, Types.OTHER);
}
2018-02-16 11:34:02 +01:00
public Object deepCopy(Object value) throws HibernateException {
2017-12-15 00:01:26 +01:00
if (value == null) return null;
UUID uuid = (UUID) value;
2018-02-16 11:34:02 +01:00
return new UUID(uuid.getMostSignificantBits(), uuid.getLeastSignificantBits());
2017-12-15 00:01:26 +01:00
}
2018-02-16 11:34:02 +01:00
public boolean isMutable() {
2017-12-15 00:01:26 +01:00
return false;
}
2018-02-16 11:34:02 +01:00
public Serializable disassemble(Object value) throws HibernateException {
2017-12-15 00:01:26 +01:00
return (Serializable) value;
}
2018-02-16 11:34:02 +01:00
public Object assemble(Serializable cached, Object owner) throws HibernateException {
2017-12-15 00:01:26 +01:00
return cached;
}
2018-02-16 11:34:02 +01:00
public Object replace(Object original, Object target, Object owner) throws HibernateException {
2017-12-15 00:01:26 +01:00
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);
//
// }
2018-02-16 11:34:02 +01:00
public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner)
throws HibernateException, SQLException {
return nullSafeGet(rs, names, owner);
}
2017-12-15 00:01:26 +01:00
2018-02-16 11:34:02 +01:00
public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session)
throws HibernateException, SQLException {
nullSafeSet(st, value, index);
}
}