support xml columns

This commit is contained in:
Efstratios Giannopoulos 2023-11-30 13:38:22 +02:00
parent 2a766dc37f
commit 2d5c9df855
2 changed files with 89 additions and 3 deletions

View File

@ -7,7 +7,9 @@ import eu.eudat.data.converters.enums.DescriptionTemplateStatusConverter;
import eu.eudat.data.converters.enums.DescriptionTemplateVersionStatusConverter;
import eu.eudat.data.converters.enums.IsActiveConverter;
import eu.eudat.data.old.queryableentity.DataEntity;
import eu.eudat.data.types.SQLXMLType;
import jakarta.persistence.*;
import org.hibernate.annotations.Type;
import java.time.Instant;
import java.util.List;
@ -26,7 +28,8 @@ public class DescriptionTemplateEntity implements DataEntity<DescriptionTemplate
public static final String _label = "label";
public static final int _labelLength = 250;
@Column(name = "definition", nullable = false)
@Type(value = SQLXMLType.class)
@Column(name = "definition", nullable = false, columnDefinition = "xml")
private String definition;
public static final String _definition = "definition";

View File

@ -1,2 +1,85 @@
package eu.eudat.data.types;public class SQLXMLType {
package eu.eudat.data.types;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.usertype.UserType;
/**
* Store and retrieve a PostgreSQL "xml" column as a Java string.
* https://wiki.postgresql.org/wiki/Hibernate_XML_Type
*/
public class SQLXMLType implements UserType<String> {
private final int[] sqlTypesSupported = new int[] { Types.VARCHAR };
@Override
public int getSqlType() {
return Types.VARCHAR;
}
@Override
public Class returnedClass() {
return String.class;
}
@Override
public boolean equals(String x, String y) throws HibernateException {
if (x == null) {
return y == null;
} else {
return x.equals(y);
}
}
@Override
public int hashCode(String x) throws HibernateException {
return x == null ? null : x.hashCode();
}
@Override
public String nullSafeGet(ResultSet rs, int i, SharedSessionContractImplementor sharedSessionContractImplementor, Object o) throws SQLException {
String xmldoc = rs.getString( i );
return rs.wasNull() ? null : xmldoc;
}
@Override
public void nullSafeSet(PreparedStatement st, String value, int index, SharedSessionContractImplementor sharedSessionContractImplementor) throws SQLException {
if (value == null) {
st.setNull(index, Types.OTHER);
} else {
st.setObject(index, value, Types.OTHER);
}
}
@Override
public String deepCopy(String value) throws HibernateException {
if (value == null)
return null;
return new String( (String)value );
}
@Override
public boolean isMutable() {
return false;
}
@Override
public Serializable disassemble(String value) throws HibernateException {
return (String) value;
}
@Override
public String assemble(Serializable cached, Object owner) throws HibernateException {
return (String) cached;
}
@Override
public String replace(String original, String target, Object owner) throws HibernateException {
return original;
}
}