无法使用 Spring Data JPA 执行聚合函数并获取结果

Posted

技术标签:

【中文标题】无法使用 Spring Data JPA 执行聚合函数并获取结果【英文标题】:Unable to execute aggregrate function and get result using Spring Data JPA 【发布时间】:2021-11-02 23:54:04 【问题描述】:

我正在尝试对特定日期执行聚合操作。请在下面找到相同的查询。


@Modifying
@Query("SELECT new AggregateJourneyFoodOrder(mealRetrievalTime, SUM(head_count), SUM(bread), SUM(achar), SUM(jam) ,SUM(others), SUM(puri), SUM(roti), SUM(thepla))"
        + "  FROM JourneyFoodOrder WHERE isRemoved=false AND mealRetrievalTime = :mealRetrievalDate")
List<AggregateJourneyFoodOrder> getOrdersByDateAndNotDisabled(Boolean isRemoved, Date mealRetrievalDate);

我遇到了例外情况

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'adminController': Unsatisfied dependency expressed through field 'journeyFoodServiceImpl'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'journeyFoodServiceImpl' defined in file [C:\Users\TiaaUser\Desktop\AWS\journeyfood\target\classes\org\brahmakumaris\journeyfood\service\JourneyFoodServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'journeyFoodOrderRepository' defined in org.brahmakumaris.journeyfood.repository.JourneyFoodOrderRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Using named parameters for method public abstract java.util.List org.brahmakumaris.journeyfood.repository.JourneyFoodOrderRepository.getOrdersByDateAndNotDisabled(java.lang.Boolean,java.util.Date) but parameter 'Optional[isRemoved]' not found in annotated query 'SELECT new AggregateJourneyFoodOrder(SUM(head_count), SUM(bread), SUM(achar), SUM(jam) ,SUM(others), SUM(puri), SUM(roti), SUM(thepla))  FROM JourneyFoodOrder WHERE isRemoved=false AND mealRetrievalTime = :mealRetrievalDate'!
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:660)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640)
    
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'journeyFoodServiceImpl' defined in file [C:\Users\TiaaUser\Desktop\AWS\journeyfood\target\classes\org\brahmakumaris\journeyfood\service\JourneyFoodServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'journeyFoodOrderRepository' defined in org.brahmakumaris.journeyfood.repository.JourneyFoodOrderRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Using named parameters for method public abstract java.util.List org.brahmakumaris.journeyfood.repository.JourneyFoodOrderRepository.getOrdersByDateAndNotDisabled(java.lang.Boolean,java.util.Date) but parameter 'Optional[isRemoved]' not found in annotated query 'SELECT new AggregateJourneyFoodOrder(SUM(head_count), SUM(bread), SUM(achar), SUM(jam) ,SUM(others), SUM(puri), SUM(roti), SUM(thepla))  FROM JourneyFoodOrder WHERE isRemoved=false AND mealRetrievalTime = :mealRetrievalDate'!
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:229)

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'journeyFoodOrderRepository' defined in org.brahmakumaris.journeyfood.repository.JourneyFoodOrderRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Using named parameters for method public abstract java.util.List org.brahmakumaris.journeyfood.repository.JourneyFoodOrderRepository.getOrdersByDateAndNotDisabled(java.lang.Boolean,java.util.Date) but parameter 'Optional[isRemoved]' not found in annotated query 'SELECT new AggregateJourneyFoodOrder(SUM(head_count), SUM(bread), SUM(achar), SUM(jam) ,SUM(others), SUM(puri), SUM(roti), SUM(thepla))  FROM JourneyFoodOrder WHERE isRemoved=false AND mealRetrievalTime = :mealRetrievalDate'!
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1786)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:602)

Caused by: java.lang.IllegalStateException: Using named parameters for method public abstract java.util.List org.brahmakumaris.journeyfood.repository.JourneyFoodOrderRepository.getOrdersByDateAndNotDisabled(java.lang.Boolean,java.util.Date) but parameter 'Optional[isRemoved]' not found in annotated query 'SELECT new AggregateJourneyFoodOrder(SUM(head_count), SUM(bread), SUM(achar), SUM(jam) ,SUM(others), SUM(puri), SUM(roti), SUM(thepla))  FROM JourneyFoodOrder WHERE isRemoved=false AND mealRetrievalTime = :mealRetrievalDate'!
    at org.springframework.data.jpa.repository.query.JpaQueryMethod.assertParameterNamesInAnnotatedQuery(JpaQueryMethod.java:172)
    at org.springframework.data.jpa.repository.query.JpaQueryMethod.<init>(JpaQueryMethod.java:139)

AggregateJourneyFoodOrder.java

public class AggregateJourneyFoodOrder 
    private int totalHeadCount;
    private int totalBread;
    private int totalAchar;
    private int totalJam;
    private int totalOthers;
    private int totalPuri;
    private int totalRoti;
    private int totalThepla;
    private Date mealRetrievalDate;
    //Getter Setters and Constructors

JourneyFoodOrder.java

@Entity
public class JourneyFoodOrder
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long orderId;
        
    private Integer headCount;
        
    @DateTimeFormat(pattern = "dd/MM/yyyy")
    private Date dateOfOrderPlaced;
        
    @DateTimeFormat(pattern = "dd/MM/yyyy")
    private Date dateOfDeparture;
        
    @DateTimeFormat(pattern = "dd/MM/yyyy h:mm a")
    private Date mealRetrievalTime;
    
    @ManyToOne
    private UserEntity user; 
        
    private Integer thepla;
        
    private Integer puri;
        
    private Integer roti;
    
    private Integer achar;
        
    private Integer jam;
        
    private Integer bread;
        
    private Integer others;
        
    private Boolean isRemoved;
    //Getters and setters

我不明白我到底做错了什么。还建议是否查询如何对特定日期执行聚合操作。

【问题讨论】:

【参考方案1】:

我认为这与聚合函数没有任何关系,但与您尝试将 LIKE 运算符(和通配符修饰符 %)应用于 Date 类型的参数无关,这不起作用。

由于我不确定该构造应该意味着什么,因此我真的不能推荐其他替代方法:使用 String 或不同的比较运算符,例如 =&gt;=

注意:@Modifying 注释似乎是错误的,因为此查询是纯选择查询,应该按原样执行。

【讨论】:

“似乎不起作用”究竟是什么意思? 我将查询更改为 - ` @Modifying @Query("SELECT new AggregateJourneyFoodOrder(SUM(head_count), SUM(bread), SUM(achar), SUM(jam) ,SUM(others), SUM (puri), SUM(roti), SUM(thepla))" + " FROM JourneyFoodOrder WHERE isRemoved=false AND mealRetrievalTime = :mealRetrievalDate") List getOrdersByDateAndNotDisabled(@Param("mealRetrievalDate")Date mealRetrievalDate);` 但它不要改变我得到的错误。 @Andronicus 请让我知道我在这里做错了什么。我是 Spring Data JPA 的新手,正在努力从中获得结果。

以上是关于无法使用 Spring Data JPA 执行聚合函数并获取结果的主要内容,如果未能解决你的问题,请参考以下文章

使用 spring-data-jpa 获取这些数据如何更正确?

无法将 Spring Data MongoDB + Spring Data JPA 与 Spring Boot 一起使用

Spring Boot + Spring Data JPA + 事务无法正常工作

Spring Boot 自动配置无法与 spring-data-jpa 一起正常工作

如何禁用嵌入式数据库 Spring-boot spring-data-jpa

在 JPA 存储库(Spring Data Jpa)中执行自定义查询