列类型数组的JPA存储库方法名称的结构是什么?

时间:2019-03-25 07:57:02

标签: java jpa spring-data

我正在使用postgresSQL作为数据库。我的表nicknames_dictionary中有一列nicknames,其数据类型为text[]。 我想搜索多个nicknames,无论它是在数组内部还是不使用JPARepository。所以我想知道应该在JPARepository接口中添加什么方法名称。

我在postgres上运行的查询是:SELECT * FROM nicknames_dictionary WHERE nicknames = ARRAY['jo'];

我尝试使用方法名List<NicknamesDictionary> findByNicknamesIn(Collection<String> nicknames);,但错误为java.lang.String cannot be cast to [Ljava.lang.Object;。调查了官方documentation,但无法找到这种查询的结构。

实体类

@Entity
@Table(
        name = "nicknames_dictionary"
)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class NicknamesDictionaryEntity {

    @Id
    @SequenceGenerator(name="nicknames_id_generator", sequenceName="nicknames_dictionary_id_seq", allocationSize=1)
    @GeneratedValue(generator="nicknames_id_generator")
    private Long id;

    private String name;

    @Type(type = "com.ash.entity.GenericStringArrayUserType")
    @Column(name = "nicknames")
    private String[] nicknames;

    //getters & setters

}

GenericStringArrayUserType.java

public class GenericStringArrayUserType<T extends Serializable> implements UserType {

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

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

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

    @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 Object nullSafeGet(ResultSet resultSet, String[] names, SessionImplementor session, Object owner)
            throws HibernateException, SQLException {

        if (resultSet.getArray(names[0]) == null) {
            return new String[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("text", (Object[]) castObject);
            statement.setArray(index, array);
        }
    }

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

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

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

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

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

存储库类

public interface NicknamesDictionaryRepository extends JpaRepository<NicknamesDictionaryEntity, Long> {

    List< NicknamesDictionaryEntity> findByNicknamesIn(Collection<String> nicknames);
}

让我知道方法名称的正确语法是什么,或者是否还有其他方法可以使用。 (是的,如果无法使用方法名解决,我可以使用criteriabuilder

0 个答案:

没有答案