我想获取updated
日期或created
日期位于日期范围之间的记录。
实体类如下:
@Entity
public class Order {
@Id
@Setter(AccessLevel.NONE)
private String orderId;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created_at", nullable = false)
private Date created;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "updated_at", nullable = false)
private Date updated;
}
其上的存储库如下:
public interface OrderRepository
extends PagingAndSortingRepository<Order, String>, QuerydslPredicateExecutor<Order> {
public Page<Order> findByCreatedBetweenOrUpdatedBetween(Date startDate, Date endDate,
Pageable pageRequest);
}
问题是当我使用它时,出现以下错误:
原因:java.lang.IllegalArgumentException:无可用参数 在(2)之间更新的零件:[IsBetween,between]永不。在 org.springframework.util.Assert.isTrue(Assert.java:116)在 org.springframework.data.jpa.repository.query.ParameterMetadataProvider.next(ParameterMetadataProvider.java:123) 在 org.springframework.data.jpa.repository.query.JpaQueryCreator $ PredicateBuilder.build(JpaQueryCreator.java:247) 在 org.springframework.data.jpa.repository.query.JpaQueryCreator.toPredicate(JpaQueryCreator.java:206) 在 org.springframework.data.jpa.repository.query.JpaQueryCreator.create(JpaQueryCreator.java:119) 在 org.springframework.data.jpa.repository.query.JpaQueryCreator.create(JpaQueryCreator.java:56)
答案 0 :(得分:4)
您需要提供4个日期。
前两个用于创建之间,后两个用于更新之间:
public interface OrderRepository
extends PagingAndSortingRepository<Order, String>, QuerydslPredicateExecutor<Order> {
public Page<Order> findByCreatedBetweenOrUpdatedBetween(
Date startDateCreated, Date endDateCreated
, Date startDateUpdated, Date endDateUpdated
, Pageable pageRequest);
}
答案 1 :(得分:0)
如下编写findByCreatedBetweenOrUpdatedBetween
方法签名。
public interface OrderRepository
extends PagingAndSortingRepository<Order, String>, QuerydslPredicateExecutor<Order> {
public Page<Order> findByCreatedBetweenOrUpdatedBetween(Date startDate1, Date endDate1,
Date startDate2, Date endDate2, Pageable pageRequest);
}
答案 2 :(得分:0)
自动生成适用于简单查询:在这种情况下,仅手动定义查询可能会更容易,那么您只需要2个参数即可。
public interface OrderRepository
extends PagingAndSortingRepository<Order, String>, QuerydslPredicateExecutor<Order> {
@Query("select o from Order o where (o.startDate between ?1 and ?2) " +
"or (o.updatedDate between ?1 and ?2)")
public Page<Order> findByCreatedOrUpdatedBetween(Date startDate, Date endDate,
Pageable pageRequest);
}