数据库保存数据,类型为ListDouble,字段只需设置为float4[]或者float8[],然后导入下述类,并在实体类的对应字段位置加上@Type(type = "XX.ListToArrayUserType"),这里写保存工具类的包路径
import com.google.common.base.Strings;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.usertype.ParameterizedType;
import org.hibernate.usertype.UserType;

import java.io.Serializable;
import java.sql.*;
import java.util.List;
import java.util.Properties;


public class ListToArrayUserType implements UserType, ParameterizedType, Serializable {

    protected static final int[] SQL_TYPES = {Types.ARRAY};

    /**
     * 默认 ArrayList
     */
    private Class listType;


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

    @Override
    public Class returnedClass() {
        return listType;
    }

    @Override
    public boolean equals(Object o, Object o1) throws HibernateException {
        if (o == null) {
            return o1 == null;
        }
        return o.equals(o1);
    }

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

    @Override
    public Object nullSafeGet(ResultSet resultSet, String[] strings,
                              SharedSessionContractImplementor sharedSessionContractImplementor,
                              Object o) throws HibernateException, SQLException {
        if (resultSet.getArray(strings[0]) == null) {
            return null;
        }
        Array array = resultSet.getArray(strings[0]);
        List<Double> result = newList();
        Float[] javaArray = (Float[]) array.getArray();
        for (Float value : javaArray) {
            result.add(value.doubleValue());
        }
        return result;
    }

    @Override
    public void nullSafeSet(PreparedStatement preparedStatement, Object o, int i,
                            SharedSessionContractImplementor sharedSessionContractImplementor)
            throws HibernateException, SQLException {
        Connection connection = preparedStatement.getConnection();
        if (o == null) {
            preparedStatement.setNull(i, SQL_TYPES[0]);
        } else {
            List castObject = (List) o;
            Object[] intArr = castObject.toArray();
            Array array = connection.createArrayOf("Float", intArr);
            preparedStatement.setArray(i, array);
        }

    }


    @Override
    public Object deepCopy(Object o) throws HibernateException {
        if (o == null) {
            return null;
        }
        List copyList = newList();
        copyList.addAll((List) o);
        return copyList;
    }

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

    @Override
    @SuppressWarnings("unchecked")
    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;
    }

    @Override
    public void setParameterValues(Properties properties) {

        String listType = (String) properties.get("listType");
        if (!Strings.isNullOrEmpty(listType)) {
            try {
                this.listType = Class.forName(listType);
            } catch (ClassNotFoundException e) {
                throw new HibernateException(e);
            }
        } else {
            this.listType = java.util.ArrayList.class;
        }

    }

    private List newList() {
        try {
            return (List) listType.newInstance();
        } catch (Exception e) {
            throw new HibernateException(e);
        }
    }
}

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐