使用spring存储库按数组过滤

时间:2017-06-13 14:23:07

标签: arrays spring hibernate spring-mvc spring-repositories

我有以下设置:

Pojo:

@Data
@EqualsAndHashCode
@ToString
@NoArgsConstructor
@Entity
@Immutable
public class ExchangeView {

    @Id
    private Long id;

    private Integer number;

    @Type(type = "com.myapp.domain.custom.StringArrayType")
    private String[] exchangeObjects, environments, bus;

    private String name, technology, type;

}

自定义类型:

public class StringArrayType implements UserType{

private final int[] arrayTypes = new int[] { Types.ARRAY };

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

@Override
public Class<String[]> returnedClass() {
    return String[].class;
}

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

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

@Override
public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws HibernateException, SQLException {
    // get the first column names
    if (names != null && names.length > 0 && rs != null && rs.getArray(names[0]) != null) {
        String[] results = (String[]) rs.getArray(names[0]).getArray();
        return results;
    }
    return null;
}

@Override
public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) throws HibernateException,
        SQLException {
    // setting the column with string array
    if (value != null && st != null) {
        String[] castObject = (String[]) value;
        Array array = session.connection().createArrayOf("varchar", castObject);
        st.setArray(index, array);
    } else {
        st.setNull(index, arrayTypes[0]);
    }
}

@Override
public Object deepCopy(Object value) throws HibernateException {
    return value == null ? null : ((String[]) value).clone();
}

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

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

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

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

public StringArrayType toStringArrayType(List<String> strings){


    return null;
}

}

和Spring存储库:

public interface ExchangeViewRepository extends Repository<ExchangeView, Long> {

    List<ExchangeView> findAll();

    List<ExchangeView> findByEnvironments(String[] environments);


    List<ExchangeView> findByBus(String[] bus);
}

在我的数据库中,我得到了一些这样的数据:

id      name        type    technology      environments                   bus

33      REGROUP     batch   ESB webMethods  {PREPRODUCTION,PRODUCTION,RECETTE}  {ASFR,BCIT}
34      CONDPAI     batch   ESB webMethods  {PREPRODUCTION,PRODUCTION,RECETTE}  {ASFR}

在我的控制器中,我正在尝试使用此方法

List<ExchangeView> findByBus(String[] bus);

但它过滤了我的表格,其中的行具有我传递给我的方法的确切数组的总线。 我想拥有以下总线的所有线路(包括带有其他总线的线路和这个总线列表)。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您可以在存储库中使用本机查询来查找包含给定总线的所有ExchangeView。我对hsqldb进行了测试,它按预期工作。

@Query(nativeQuery = true, value = "select * from EXCHANGE_VIEW where POSITION_ARRAY(:bus IN bus) > 0")
List<ExchangeView> findByBus(@Param("bus") String bus);

查询将根据您的dbms而有所不同。

<强>更新

要获取所有任何其中一条给定总线的行,您可以使用此查询:

@Query(nativeQuery = true, value = "select * from EXCHANGE_VIEW where id in ( select id from EXCHANGE_VIEW, unnest(bus) as bus(b) where b in(:bus_names))")
List<ExchangeView> findByBus(@Param("bus_names") List<String> buses);