Java 用于选择特定列的 Spring Data JPA 规范
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22171822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Data JPA Specification to Select Specific Columns
提问by Chinmay
We can select specific columns by writing custom @Query methods in our Repository Interface. However, I don't want to write so many methods for different properties.
我们可以通过在我们的存储库接口中编写自定义 @Query 方法来选择特定的列。但是,我不想为不同的属性编写这么多方法。
I tried this, but it returns the entire object all the time.
我试过这个,但它一直返回整个对象。
public class MySpecifications {
public static Specification<MyInfo> propertiesWithId(final String[] properties, final Object id, final String idProperty)
{
return new Specification<MyInfo>() {
@Override
public Predicate toPredicate(Root<MyInfo> root,
CriteriaQuery<?> query, CriteriaBuilder cb) {
query = cb.createTupleQuery(); //tried cb.createQuery(MyInfo.class); as well
List<Selection<? extends Object>> selectionList = new ArrayList<Selection<? extends Object>>();
for (String property : properties) {
Selection<? extends Object> selection = root.get(property);
selectionList.add(selection);
}
return query.multiselect(selectionList).where(cb.equal(root.get(idProperty), id)).getRestriction();
}
};
}
}
used as:
用作:
MyInfo findOne(Specification(properties,idValue, idProperty));
Is this the correct way? Where is the mistake?
这是正确的方法吗?错误在哪里?
回答by yohan_f
I tried this, but it returns the entire object all the time.
我试过这个,但它一直返回整个对象。
This method returns single entity matching given specification. Please check here
此方法返回匹配给定规范的单个实体。请检查这里
According to my understanding this is the correct way. U can access the properties of the entity as normal (Eg. MyInfo.getIdProperty())
根据我的理解,这是正确的方法。您可以正常访问实体的属性(例如 MyInfo.getIdProperty())
回答by chrismarx
The current spring data jpa specification executor is limited to criteria in the where clause, so you can'tchange the selected columns, it's implicitely limited to full entities only (take a look at JpaSpecificationExecutor
interface documentation). You'll have to go with a custom repository implementation, or move to named queries-
当前的 spring 数据 jpa 规范执行程序仅限于 where 子句中的条件,因此您无法更改所选列,它仅隐式地仅限于完整实体(查看JpaSpecificationExecutor
接口文档)。您必须使用自定义存储库实现,或者转向命名查询-
Spring Data JPA and Querydsl to fetch subset of columns using bean/constructor projection
回答by Jens Schauder
Specifications are abstractions over where clauses. Due to the design of the JPA criteria API you can all kinds of stuff in Specification but the behaviour of any side effect apart from declaring a where clause is undefined.
规范是对 where 子句的抽象。由于 JPA 标准 API 的设计,您可以在规范中使用各种内容,但除了声明 where 子句之外,任何副作用的行为都是未定义的。
If you want to control the select list you can either use query derivation with projections and the very limited query support or construct complete custom queries in a custom method.
如果您想控制选择列表,您可以使用带有投影的查询派生和非常有限的查询支持,或者在自定义方法中构建完整的自定义查询。