Java 如何编写 Spring Data 方法名称来检索列中的所有元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18145990/
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
How to write Spring Data method name to retrieve all elements in a column?
提问by m3th0dman
Suppose I have the class:
假设我有这样的课程:
@Entity
public class Bean {
@Id
private String beanId;
//other fields & setters and getters
}
And the corresponding Spring Data JPA repository, where I want to have in a List<String>
all the beanIds
.
以及相应的 Spring Data JPA 存储库,我希望在其中包含List<String>
所有beanIds
.
@RepositoryDefinition(domainClass = Bean.class, idClass = String.class)
public interface BeanRepository {
@Query("select b.beanId from Bean b")
List<String> findAllBeanId();
}
As written above, everything works as expected; but this is a simple operation and I do not want to write a query explicitly. What should the name of the method be such that Spring Data can parse it and obtain the above mentioned query (or the same functionality). I have searched in both the reference documentation as two books I have on Spring Data. The above name (findAllBeanId
) and others that I have tried (findBeanId
, findBeanBeanId
etc.) throw the following exception as root cause:
如上所述,一切都按预期进行;但这是一个简单的操作,我不想明确地编写查询。方法的名称应该是什么,以便 Spring Data 可以解析它并获得上述查询(或相同的功能)。我在参考文档中搜索了两本关于 Spring Data 的书。上述域名(findAllBeanId
)和其他人,我已经试过(findBeanId
,findBeanBeanId
等)抛出以下异常的根本原因:
org.springframework.data.mapping.PropertyReferenceException: No property find found for type Trade
at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:75)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:327)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:353)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:353)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307)
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:271)
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:245)
at org.springframework.data.repository.query.parser.Part.<init>(Part.java:72)
at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:180)
at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:260)
at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:240)
at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:68)
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:57)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:90)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:162)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:68)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:279)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:147)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:153)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:43)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)
... 22 more
采纳答案by ragnor
In the Spring docs: http://static.springsource.org/spring-data/jpa/docs/1.3.0.RELEASE/reference/html/jpa.repositories.htmlthere is nothing about getting only particular column/property from entity by query generated from method name. So I think that currently it is not possible.
在 Spring 文档中:http: //static.springsource.org/spring-data/jpa/docs/1.3.0.RELEASE/reference/html/jpa.repositories.html没有关于仅从实体获取特定列/属性的内容通过从方法名称生成的查询。所以我认为目前这是不可能的。
回答by Oliver Drotbohm
The code you showed works/should work as expected. It's simply not causing the exception you see :).
您显示的代码可以/应该按预期工作。它根本不会导致您看到的异常:)。
The exception is referring to a Trade
, which seems to indicate that you have a repository for Trade
somewhere which seems to refer to a missing property. The code you've shown is definitely not the one causing the exception. This can effectively not be the case as you're defining the query manually so that the query derivation mechanism doesn't even kick in for the repo you've shown.
例外是指 a Trade
,这似乎表明您有一个Trade
似乎指向缺失属性的某个地方的存储库。您显示的代码绝对不是导致异常的代码。当您手动定义查询时,实际上情况并非如此,因此查询派生机制甚至不会为您显示的存储库启动。
I've pushed a test casefor you to see this in action.
我已经推送了一个测试用例让你看到它的实际效果。