JPA Specification Select返回所有列而不是特定列

niknxzdl  于 2023-04-06  发布在  Spring
关注(0)|答案(1)|浏览(380)

我使用的是JPA规范,只需要选择特定的列。
这是密码:

Specification<Item> spec = (root, query, builder) -> {
            query.select(root.get("Id"));

            Predicate predicate = builder.equal(root.get("Id"), "12345");

            return predicate;

        };

在日志中,我看到Item Entity中的所有列都是从数据库中选择的。
是窃听器吗?
用法:
接口:

public interface Repo extends PagingAndSortingRepository<Item,String>, JpaSpecificationExecutor<Item> {
}

电话:

repo.findAll(spec );
0yg35tkg

0yg35tkg1#

JpaSpecificationExecutor被专门定义为返回实体类型。我怀疑它忽略了.select(root.get("Id"))
通常情况下,如果你有一个非常动态的条件集,你会使用规范。如果你只有几个参数,你需要搜索,我会使用派生查询,或命名查询。

public interface Repo extends PagingAndSortingRepository<Item,String>, JpaSpecificationExecutor<Item> {

   @Query("Select i.id from Item i where name=:name")
   Long getIdforName(String name);
}

相关问题