gpt4 book ai didi

java - 如何编写 Spring Data 方法名称来检索列中的所有元素?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:48:33 25 4
gpt4 key购买 nike

假设我有类(class):

@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

最佳答案

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

关于java - 如何编写 Spring Data 方法名称来检索列中的所有元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18145990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com