我已经有大约一周时间无法找出 Spring Boot 存储库中的问题了,并且我已经没有关于如何解决它的想法了。问题出在方法上findOrdersByFilters
。如果我第一次调用它并传递日期作为参数之一,那么一切都会正常工作,并且对数据库的请求会按应有的方式处理,但是如果在启动程序后首先传递除日期之外的任何内容,那么该请求停止正常工作并抛出错误。演示该问题的视频:https://youtu.be/-pWz3vgBpIc
2025-01-06T14:28:02.035+03:00 WARN 4997 --- [nio-8082-exec-5] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: 42846
2025-01-06T14:28:02.035+03:00 ERROR 4997 --- [nio-8082-exec-5] o.h.engine.jdbc.spi.SqlExceptionHelper : ERROR: cannot cast type bytea to date
Позиция: 910
2025-01-06T14:28:02.042+03:00 ERROR 4997 --- [nio-8082-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.dao.InvalidDataAccessResourceUsageException: JDBC exception executing SQL [select o1_0.id,o1_0.client_id,o1_0.comment,o1_0.date_create,o1_0.date_end,o1_0.machine,o1_0.manager_username,o1_0.order_status,o1_0.payment_status from orders o1_0 join clients c1_0 on o1_0.client_id=c1_0.id left join employee e1_0 on o1_0.manager_username=e1_0.username where (? is null or o1_0.id=?) and (? is null or c1_0.company_name like ('%'||?||'%') escape '') and (? is null or (e1_0.first_name is not null and e1_0.last_name is not null and (e1_0.first_name like ('%'||?||'%') escape '' or e1_0.last_name like ('%'||?||'%') escape '' or (e1_0.first_name||' '||e1_0.last_name) like ('%'||?||'%') escape '' or (e1_0.last_name||' '||e1_0.first_name) like ('%'||?||'%') escape ''))) and (? is null or o1_0.payment_status like ('%'||?||'%') escape '') and (? is null or o1_0.order_status like ('%'||?||'%') escape '') and (? is null or o1_0.comment like ('%'||?||'%') escape '') and (cast(? as date) is null or o1_0.date_end=cast(? as date)) and (? is null or o1_0.manager_username=?) order by o1_0.date_create desc] [ERROR: cannot cast type bytea to date
Позиция: 910] [n/a]; SQL [n/a]] with root cause
OrderRepo.java
package ru.limon4egtop.printingCRM.repos;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import ru.limon4egtop.printingCRM.dto.OrderFilterDto;
import ru.limon4egtop.printingCRM.models.Orders;
import java.util.List;
@Repository
public interface OrderRepo extends JpaRepository<Orders, Long> {
@Query("""
SELECT o FROM Orders o
JOIN Clients c ON o.clientId = c.id
LEFT JOIN Employee e ON o.managerUsername = e.username
WHERE (:#{#filter.id} IS NULL OR o.id = :#{#filter.id})
AND (:#{#filter.companyName} IS NULL OR c.companyName LIKE CONCAT('%', :#{#filter.companyName}, '%'))
AND (
:#{#filter.managerName} IS NULL OR
(
e.firstName IS NOT NULL AND e.lastName IS NOT NULL AND
(
e.firstName LIKE CONCAT('%', :#{#filter.managerName}, '%') OR
e.lastName LIKE CONCAT('%', :#{#filter.managerName}, '%') OR
CONCAT(e.firstName, ' ', e.lastName) LIKE CONCAT('%', :#{#filter.managerName}, '%') OR
CONCAT(e.lastName, ' ', e.firstName) LIKE CONCAT('%', :#{#filter.managerName}, '%')
)
)
)
AND (:#{#filter.paymentStatus} IS NULL OR o.paymentStatus LIKE CONCAT('%', :#{#filter.paymentStatus}, '%'))
AND (:#{#filter.orderStatus} IS NULL OR o.orderStatus LIKE CONCAT('%', :#{#filter.orderStatus}, '%'))
AND (:#{#filter.comment} IS NULL OR o.comment LIKE CONCAT('%', :#{#filter.comment}, '%'))
AND (CAST(:#{#filter.dateEnd} AS DATE) IS NULL OR o.dateEnd = CAST(:#{#filter.dateEnd} AS DATE))
AND (:#{#filter.currentUsername} IS NULL OR o.managerUsername = :#{#filter.currentUsername})
ORDER BY o.dateCreate DESC
""")
List<Orders> findOrdersByFilters(@Param("filter") OrderFilterDto filter);
}
OrderServiceImp.java
package ru.limon4egtop.printingCRM.Services.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ru.limon4egtop.printingCRM.Services.OrderService;
import ru.limon4egtop.printingCRM.dto.OrderFilterDto;
import ru.limon4egtop.printingCRM.models.Orders;
import ru.limon4egtop.printingCRM.repos.OrderRepo;
import java.util.List;
@Service
public class OrderServiceImp implements OrderService {
private OrderRepo orderRepo;
@Autowired
public OrderServiceImp(OrderRepo orderRepo) {
this.orderRepo = orderRepo;
}
@Override
public List<Orders> getOrdersByFilters(final OrderFilterDto orderFilterDto) {
System.out.println(orderFilterDto.toString());
return this.orderRepo.findOrdersByFilters(orderFilterDto);
}
}
OrderFilterDto.java
package ru.limon4egtop.printingCRM.dto;
import java.time.LocalDate;
public class OrderFilterDto {
private Long id;
private String companyName;
private String managerName;
private String paymentStatus;
private String orderStatus;
private String comment;
private LocalDate dateEnd;
private String currentUsername;
public OrderFilterDto(Long id, String companyName, String managerName, String paymentStatus, String orderStatus, String comment, LocalDate dateEnd, String currentUsername) {
this.id = id;
this.companyName = companyName;
this.managerName = managerName;
this.paymentStatus = paymentStatus;
this.orderStatus = orderStatus;
this.comment = comment;
this.dateEnd = dateEnd;
this.currentUsername = currentUsername;
}
@Override
public String toString() {
return "OrderFilterDto {" +
"id=" + id +
", companyName='" + companyName + '\'' +
", managerName='" + managerName + '\'' +
", paymentStatus='" + paymentStatus + '\'' +
", orderStatus='" + orderStatus + '\'' +
", comment='" + comment + '\'' +
", dateEnd=" + dateEnd +
", currentUsername=" + currentUsername +
'}';
}
}