Postgres数组成员资格测试

时间:2018-09-01 23:21:03

标签: arrays postgresql hibernate spring-data

我在一个Postgres数据库表(“ person”)中有一列(例如“昵称”),该表被声明为文本数组(text [])。我可以使用实现休眠UserType接口的自定义类(StringList)从精细列中检索值。这是我的实体课

@Entity
@Table(name = "person")
public class Person {
    ...

    @Column(columnDefinition = "text[]")
    @Type(type = "com.me.type.StringList")
    private List<String> nicknames;
}

如何检索包含给定昵称的Person实体?我正尝试使用Spring JPA查询,

@Query("SELECT p FROM Person p WHERE ...)")
List<Person> findPersonsWithNickname(String nickname );

每次尝试完成查询都会给我一个运行时异常,当我在StringList中放置断点时,对于该查询它们不会被击中。

这是定义StringList的方式:

import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.usertype.UserType;

import java.io.Serializable;
import java.sql.Array;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class StringList implements UserType {
    private final int[] arrayTypes = new int[]{Types.ARRAY};

    public int[] sqlTypes() {
        return arrayTypes;
    }

    public Class<List> returnedClass() {
        return List.class;
    }

    public boolean equals(Object x, Object y) throws HibernateException {
        return x == null ? y == null : x.equals(y);
    }

    public int hashCode(Object x) throws HibernateException {
        return x == null ? 0 : x.hashCode();
    }

    @Override
    public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner)
        throws HibernateException, SQLException {
        if (names != null && names.length > 0 && rs != null && rs.getArray(names[0]) != null) {
            Object array = rs.getArray(names[0]).getArray();
            if (array instanceof String[])
                return Arrays.asList((String[]) array);
            else
                return Arrays.asList();
        }

        return null;
    }

    @Override
    public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session)
        throws HibernateException, SQLException {
        if (value != null && st != null) {
            List<String> list = (List<String>) value;
            String[] castObject = list.toArray(new String[list.size()]);
            Array array = session.connection().createArrayOf("text", castObject);
            st.setArray(index, array);
        } else {
            st.setNull(index, arrayTypes[0]);
        }
    }

    public Object deepCopy(Object value) throws HibernateException {
        if (value == null)
            return null;

        List<String> list = (List<String>) value;
        ArrayList<String> clone = new ArrayList<String>();
        for (Object stringValue : list)
            clone.add((String) stringValue);

        return clone;
    }

    public boolean isMutable() {
        return false;
    }

    public Serializable disassemble(Object value) throws HibernateException {
        return (Serializable) value;
    }

    public Object assemble(Serializable cached, Object owner) throws HibernateException {
        return cached;
    }

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

0 个答案:

没有答案