Hibernate 浮点数组jpa映射

Hibernate 浮点数组jpa映射,hibernate,spring-boot,jpa,Hibernate,Spring Boot,Jpa,我有一个浮点数组作为表中的一列。我无法用JPA映射它。有人能帮忙吗 我正在将我的服务从RubyonRails转移到SpringBoot。例如在rails控制台中看起来是这样的 billing_ids : [ [0] 27295 [1] 21323 ] 您需要添加以下maven依赖项 <dependency> <groupId>com.vladmihalcea</groupId>

我有一个浮点数组作为表中的一列。我无法用JPA映射它。有人能帮忙吗

我正在将我的服务从RubyonRails转移到SpringBoot。例如在rails控制台中看起来是这样的

 billing_ids : [
    [0] 27295
    [1] 21323
]

您需要添加以下maven依赖项

        <dependency>
            <groupId>com.vladmihalcea</groupId>
            <artifactId>hibernate-types-52</artifactId>
            <version>2.2.0</version>
        </dependency>
如下所示定义GenericArrayUserType类

public class GenericArrayUserType<T extends Serializable> implements UserType {

    protected static final int[] SQL_TYPES = { Types.ARRAY };
    private  Class<T> typeParameterClass;

    @Override
    public Object assemble(Serializable cached, Object owner) throws HibernateException {
        return this.deepCopy(cached);
    }

    @Override
    public Object deepCopy(Object value) throws HibernateException {
        return value;
    }

    @SuppressWarnings("unchecked")
    @Override
    public Serializable disassemble(Object value) throws HibernateException {
        return (T) this.deepCopy(value);
    }

    @Override
    public boolean equals(Object x, Object y) throws HibernateException {

        if (x == null) {
            return y == null;
        }
        return x.equals(y);
    }

    @Override
    public int hashCode(Object x) throws HibernateException {
        return x.hashCode();
    }

    @Override
    public boolean isMutable() {
        return true;
    }

    @Override
    public Object nullSafeGet(ResultSet resultSet, String[] names, SessionImplementor session, Object owner)
            throws HibernateException, SQLException {

        if (resultSet.getArray(names[0]) == null) {
            return new Integer[0];
        }

        Array array = resultSet.getArray(names[0]);
        @SuppressWarnings("unchecked")
        T javaArray = (T) array.getArray();
        return javaArray;
    }

    @Override
    public void nullSafeSet(PreparedStatement statement, Object value, int index, SessionImplementor session)
            throws HibernateException, SQLException {
        Connection connection = statement.getConnection();
        if (value == null) {
            statement.setNull(index, SQL_TYPES[0]);
        } else {
            @SuppressWarnings("unchecked")
            T castObject = (T) value;
            Array array = connection.createArrayOf("integer", (Object[]) castObject);
            statement.setArray(index, array);
        }
    }

    @Override
    public Object replace(Object original, Object target, Object owner) throws HibernateException {
        return original;
    }

    @Override
    public Class<T> returnedClass() {
        return typeParameterClass;
    }

    @Override
    public int[] sqlTypes() {
        return new int[] { Types.ARRAY };
    }


}
公共类GenericArrayUserType实现UserType{
受保护的静态final int[]SQL_TYPES={TYPES.ARRAY};
私有类typeParameterClass;
@凌驾
公共对象汇编(可序列化缓存,对象所有者)引发HibernateException{
返回此.deepCopy(缓存);
}
@凌驾
公共对象deepCopy(对象值)引发HibernateException{
返回值;
}
@抑制警告(“未选中”)
@凌驾
公共可序列化反汇编(对象值)引发HibernateException{
返回(T)此.deepCopy(值);
}
@凌驾
公共布尔等于(对象x,对象y)抛出HibernateException{
如果(x==null){
返回y==null;
}
返回x等于(y);
}
@凌驾
public int hashCode(对象x)抛出HibernateeException{
返回x.hashCode();
}
@凌驾
公共布尔可交换(){
返回true;
}
@凌驾
公共对象nullSafeGet(ResultSet ResultSet、字符串[]名称、SessionImplementor会话、对象所有者)
抛出HibernateeException、SQLException{
if(resultSet.getArray(名称[0])==null){
返回新的整数[0];
}
Array Array=resultSet.getArray(名称[0]);
@抑制警告(“未选中”)
T javaArray=(T)array.getArray();
返回javaArray;
}
@凌驾
public void nullSafeSet(PreparedStatement语句、对象值、int索引、SessionImplementor会话)
抛出HibernateeException、SQLException{
Connection=statement.getConnection();
如果(值==null){
语句.setNull(索引,SQL_类型[0]);
}否则{
@抑制警告(“未选中”)
T castObject=(T)值;
Array Array=connection.createArrayOf(“integer”,(Object[])castObject);
语句.setArray(索引,数组);
}
}
@凌驾
公共对象替换(对象原始、对象目标、对象所有者)引发HibernateException{
归还原件;
}
@凌驾
公共类returnedClass(){
返回类型参数类;
}
@凌驾
公共int[]sqlTypes(){
返回新的int[]{Types.ARRAY};
}
}
这将适用于整数数组类型的列

public class GenericFloatArrayUserType<T extends Serializable> implements UserType {

    protected static final int[] SQL_TYPES = { Types.ARRAY };
    private  Class<T> typeParameterClass;

    @Override
    public Object assemble(Serializable cached, Object owner) throws HibernateException {
        return this.deepCopy(cached);
    }

    @Override
    public Object deepCopy(Object value) throws HibernateException {
        return value;
    }

    @SuppressWarnings("unchecked")
    @Override
    public Serializable disassemble(Object value) throws HibernateException {
        return (T) this.deepCopy(value);
    }

    @Override
    public boolean equals(Object x, Object y) throws HibernateException {

        if (x == null) {
            return y == null;
        }
        return x.equals(y);
    }

    @Override
    public int hashCode(Object x) throws HibernateException {
        return x.hashCode();
    }

    @Override
    public boolean isMutable() {
        return true;
    }

    @Override
    public Object nullSafeGet(ResultSet resultSet, String[] names, SessionImplementor session, Object owner)
            throws HibernateException, SQLException {

        if (resultSet.getArray(names[0]) == null) {
            return new Double[0];
        }

        Array array = resultSet.getArray(names[0]);
        @SuppressWarnings("unchecked")
        T javaArray = (T) array.getArray();
        return javaArray;
    }

    @Override
    public void nullSafeSet(PreparedStatement statement, Object value, int index, SessionImplementor session)
            throws HibernateException, SQLException {
        Connection connection = statement.getConnection();
        if (value == null) {
            statement.setNull(index, SQL_TYPES[0]);
        } else {
            @SuppressWarnings("unchecked")
            T castObject = (T) value;
            Array array = connection.createArrayOf("decimal", (Object[]) castObject);
            statement.setArray(index, array);
        }
    }

    @Override
    public Object replace(Object original, Object target, Object owner) throws HibernateException {
        return original;
    }

    @Override
    public Class<T> returnedClass() {
        return typeParameterClass;
    }

    @Override
    public int[] sqlTypes() {
        return new int[] { Types.ARRAY };
    }


}
公共类GenericFloatArrayUserType实现UserType{
受保护的静态final int[]SQL_TYPES={TYPES.ARRAY};
私有类typeParameterClass;
@凌驾
公共对象汇编(可序列化缓存,对象所有者)引发HibernateException{
返回此.deepCopy(缓存);
}
@凌驾
公共对象deepCopy(对象值)引发HibernateException{
返回值;
}
@抑制警告(“未选中”)
@凌驾
公共可序列化反汇编(对象值)引发HibernateException{
返回(T)此.deepCopy(值);
}
@凌驾
公共布尔等于(对象x,对象y)抛出HibernateException{
如果(x==null){
返回y==null;
}
返回x等于(y);
}
@凌驾
public int hashCode(对象x)抛出HibernateeException{
返回x.hashCode();
}
@凌驾
公共布尔可交换(){
返回true;
}
@凌驾
公共对象nullSafeGet(ResultSet ResultSet、字符串[]名称、SessionImplementor会话、对象所有者)
抛出HibernateeException、SQLException{
if(resultSet.getArray(名称[0])==null){
返回新的双精度[0];
}
Array Array=resultSet.getArray(名称[0]);
@抑制警告(“未选中”)
T javaArray=(T)array.getArray();
返回javaArray;
}
@凌驾
public void nullSafeSet(PreparedStatement语句、对象值、int索引、SessionImplementor会话)
抛出HibernateeException、SQLException{
Connection=statement.getConnection();
如果(值==null){
语句.setNull(索引,SQL_类型[0]);
}否则{
@抑制警告(“未选中”)
T castObject=(T)值;
Array Array=connection.createArrayOf(“decimal”,(Object[])castObject);
语句.setArray(索引,数组);
}
}
@凌驾
公共对象替换(对象原始、对象目标、对象所有者)引发HibernateException{
归还原件;
}
@凌驾
公共类returnedClass(){
返回类型参数类;
}
@凌驾
公共int[]sqlTypes(){
返回新的int[]{Types.ARRAY};
}
}

对浮点数据类型尝试此操作

这是一个整数数组,类似的列都存在浮点值。我们可以将其用作列表和列表吗?共享您尝试过的实体映射。你在用哪个数据库?我在用postgres。我尝试过Integer[],当我使用com.vladmichalcea hibernate-types-52 2.3.5依赖项时,它正在处理这个问题。我想将它映射为List和List。首先,它是否适用于整数数组列?我为Float添加了一个GenericType类
public class GenericFloatArrayUserType<T extends Serializable> implements UserType {

    protected static final int[] SQL_TYPES = { Types.ARRAY };
    private  Class<T> typeParameterClass;

    @Override
    public Object assemble(Serializable cached, Object owner) throws HibernateException {
        return this.deepCopy(cached);
    }

    @Override
    public Object deepCopy(Object value) throws HibernateException {
        return value;
    }

    @SuppressWarnings("unchecked")
    @Override
    public Serializable disassemble(Object value) throws HibernateException {
        return (T) this.deepCopy(value);
    }

    @Override
    public boolean equals(Object x, Object y) throws HibernateException {

        if (x == null) {
            return y == null;
        }
        return x.equals(y);
    }

    @Override
    public int hashCode(Object x) throws HibernateException {
        return x.hashCode();
    }

    @Override
    public boolean isMutable() {
        return true;
    }

    @Override
    public Object nullSafeGet(ResultSet resultSet, String[] names, SessionImplementor session, Object owner)
            throws HibernateException, SQLException {

        if (resultSet.getArray(names[0]) == null) {
            return new Double[0];
        }

        Array array = resultSet.getArray(names[0]);
        @SuppressWarnings("unchecked")
        T javaArray = (T) array.getArray();
        return javaArray;
    }

    @Override
    public void nullSafeSet(PreparedStatement statement, Object value, int index, SessionImplementor session)
            throws HibernateException, SQLException {
        Connection connection = statement.getConnection();
        if (value == null) {
            statement.setNull(index, SQL_TYPES[0]);
        } else {
            @SuppressWarnings("unchecked")
            T castObject = (T) value;
            Array array = connection.createArrayOf("decimal", (Object[]) castObject);
            statement.setArray(index, array);
        }
    }

    @Override
    public Object replace(Object original, Object target, Object owner) throws HibernateException {
        return original;
    }

    @Override
    public Class<T> returnedClass() {
        return typeParameterClass;
    }

    @Override
    public int[] sqlTypes() {
        return new int[] { Types.ARRAY };
    }


}