【发布时间】:2018-03-23 01:34:05
【问题描述】:
在 Spring Data 中,我有一个 Projection 来获取我的 Entity 中的 Id。
public interface IdOnly<T> {
T getId();
}
我需要按多个字段过滤我的查询,其中许多字段可以为 null,并且不应在查询中使用 null 值。所以,我打算使用Query by Example 来完成这项工作。除此之外,我只需要第一个 ID,limiting the query on database。但不幸的是,似乎没有办法做到这一点。
// It works
IdOnly<Long> findFirstBy();
// It works too (although it does not limit the query in database)
Entity findOne(Example<Entity> example);
// It doesn't work. Throws an ClassCastException when returns a non-null entity
IdOnly<Long> findOne(Example<Entity> example);
// That's the one I really need
// The server doesn't start (IndexOutOfBoundsException)
IdOnly<Long> findFirstBy(Example<Entity> example);
那么,有没有办法使用示例进行这样的查询,以仅获取第一个投影?
【问题讨论】:
标签: java spring spring-data spring-data-jpa