uoa-validator-api/src/main/java/eu/dnetlib/validatorapi/utils/CustomStringArrayType.java

80 lines
2.4 KiB
Java

package eu.dnetlib.validatorapi.utils;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.usertype.UserType;
import java.io.Serializable;
import java.sql.*;
import java.util.Arrays;
public class CustomStringArrayType implements UserType {
@Override
public int[] sqlTypes() {
return new int[]{Types.ARRAY};
}
@Override
public Class returnedClass() {
return String[].class;
}
@Override
public boolean equals(Object o1, Object o2) throws HibernateException {
if (o1 instanceof String[] && o2 instanceof String[]) {
return Arrays.deepEquals((String[])o1, (String[])o2);
} else {
return false;
}
}
@Override
public int hashCode(Object o) throws HibernateException {
return Arrays.hashCode((String[])o);
}
@Override
public Object nullSafeGet(ResultSet resultSet, String[] names,
SharedSessionContractImplementor sharedSessionContractImplementor, Object o)
throws HibernateException, SQLException {
Array array = resultSet.getArray(names[0]);
return array != null ? array.getArray() : null;
}
@Override
public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index,
SharedSessionContractImplementor sharedSessionContractImplementor) throws HibernateException, SQLException {
if (value != null && preparedStatement != null) {
Array array = sharedSessionContractImplementor.connection().createArrayOf("text", (String[])value);
preparedStatement.setArray(index, array);
} else {
preparedStatement.setNull(index, sqlTypes()[0]);
}
}
@Override
public Object deepCopy(Object o) throws HibernateException {
String[] a = (String[])o;
return Arrays.copyOf(a, a.length);
}
@Override
public boolean isMutable() {
return false;
}
@Override
public Serializable disassemble(Object o) throws HibernateException {
return (Serializable) o;
}
@Override
public Object assemble(Serializable serializable, Object o) throws HibernateException {
return serializable;
}
@Override
public Object replace(Object o, Object o1, Object o2) throws HibernateException {
return o;
}
}