3

假设我有课:

@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) 和我尝试过的其他名称 (findBeanIdfindBeanBeanId) 引发以下异常作为根本原因:

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
4

2 回答 2

5

在 Spring 文档中:http ://static.springsource.org/spring-data/jpa/docs/1.3.0.RELEASE/reference/html/jpa.repositories.html仅从实体获取特定列/属性没有任何意义通过从方法名称生成的查询。所以我认为目前是不可能的。

于 2013-08-09T12:18:31.867 回答
1

您显示的代码工作/应该按预期工作。它根本不会导致您看到的异常:)。

例外是指 a Trade,这似乎表明您有一个Trade似乎引用缺失属性的某个地方的存储库。您显示的代码绝对不是导致异常的代码。当您手动定义查询时,实际上情况并非如此,因此查询派生机制甚至不会为您显示的存储库启动。

我已经推送了一个测试用例让你看到它的实际效果。

于 2013-08-09T17:01:48.120 回答