Ich versuche BigDecimal-Array in postgresql über Hibernate mit meiner UserType-Klasse abzubilden, aber es gibt einen Fehler: "Keine Dialektzuordnung für JDBC-Typ: 2003". Ich verwende Hibernate 5 und Postgresql 9.4-1206-jdbc42. Mein Modell enthält:Hibernate UserType-Fehler
@Column(name = "prices")
@Type(type = "com.hms.domain.customTypes.BigDecimalArrayType")
private BigDecimal[] prices;
Hier meine Userdata-Klasse ist:
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.usertype.UserType;
import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.*;
public class BigDecimalArrayType implements UserType {
private static final int[] SQL_TYPES = {Types.ARRAY};
public final int[] sqlTypes() {
return SQL_TYPES;
}
public Class returnedClass() {
return BigDecimal[].class;
}
public boolean equals(Object x, Object y) throws HibernateException {
return x == y;
}
public int hashCode(Object x) throws HibernateException {
return x.hashCode();
}
public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException {
Array array = rs.getArray(names[0]);
BigDecimal[] javaArray = (BigDecimal[]) array.getArray();
if (javaArray == null) { return new BigDecimal(0);}
return javaArray;
}
public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException {
Connection connection = st.getConnection();
BigDecimal[] bigDecimals = (BigDecimal[]) value;
Array array = connection.createArrayOf("NUMERIC", bigDecimals);
//Array array = session.connection().createArrayOf("NUMERIC", bigDecimals);
if (null != array){
st.setArray(index, array);
} else {
st.setNull(index, SQL_TYPES[0]);
}
}
public Object deepCopy(Object value) throws HibernateException {
return value == null ? null : ((BigDecimal[]) value).clone();
}
public boolean isMutable() {
return true;
}
public Serializable disassemble(Object value) throws HibernateException {
return (Serializable) this.deepCopy(value);
}
public Object assemble(Serializable cached, Object owner) throws HibernateException {
return this.deepCopy(cached);
}
public Object replace(Object original, Object target, Object owner) throws HibernateException {
return this.deepCopy(original);
}
}
Ich habe viele Beiträge gelesen und es scheint, dass mein Code sollte in Ordnung sein. Aber da stimmt was nicht. P.S: Sorry für mein Englisch.
Mögliche Duplikate von [Zuordnung eines Postgres-Arrays mit Hibernate] (http://StackOverflow.com/questions/1647583/mapping-a-postgres-array-with-hibernate) oder [Array mit UserType in Hibernate und Postgresql] (http://stackoverflow.com/questions/14219498/array-with-usertype-in-hibernate-and-postgresql-mappingexception) – nbrooks
Ich benutze Hibernate 5 und Postgres 9.4-1206-jdbc42. Es gibt also keine Doppelarbeit. –