假设我有班级:
@Entity
public class Bean {
@Id
private String beanId;
//other fields & setters and getters
}
以及相应的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();
}
如上所述,一切都按预期工作;但这是一个简单的操作,我不想显式编写查询。该方法的名称应该是什么,以便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
答案 0 :(得分:5)
在Spring文档:http://static.springsource.org/spring-data/jpa/docs/1.3.0.RELEASE/reference/html/jpa.repositories.html中,没有任何关于通过从方法名称生成的查询仅从实体获取特定列/属性。所以我认为目前这是不可能的。
答案 1 :(得分:1)
您展示的代码工作/应按预期工作。它只是没有导致您看到的异常:)。
该异常指的是Trade
,这似乎表明您在某处有Trade
的存储库,似乎是指缺少的属性。您显示的代码绝对不是导致异常的代码。实际情况并非如此,因为您手动定义查询,以便查询派生机制甚至不会为您显示的仓库提供支持。
我已经推送了一个test case让你看到这一点。