我有这样的实体
@Entity
@Table(name = "PAYMENT")
public class PaymentSummary {
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "payment_id", nullable = false, unique = true)
private Long id;
@Column(name = "date_created", nullable = false)
private LocalDateTime createdDate;
@OneToOne
@JoinColumn(name = "webOrder_id")
private WebOrder webOrder;
@Enumerated(EnumType.STRING)
@Column(name = "payment_type")
private PaymentType paymentType;
@Column(name = "amount")
private Double amount;
@Column(name = "username")
private String username;
}
并且我有此实体的存储库
@Repository
public interface PaymentRepository extends JpaRepository<PaymentSummary, Long> {
List<PaymentSummary> findAllByCreatedDate(LocalDateTime localDate);
}
稍后我想检索今天创建的所有付款
List<PaymentSummary> payments = paymentRepository.findAllByCreatedDate(LocalDateTime.now());
并且它返回null,我知道是因为我通过了LocalDateTime.now(),所以它将在Date-minus-second之前找到精确值。我想列出所有今天的付款,并且仍然想保留LocalDateTime createdDate,如何处理这种情况,我是否需要编写本机查询或JPA支持呢?谢谢。
它将返回null
,因为LocalDateTime.now()
将创建当前日期和时间。就您而言,我认为您只需要按日期返回
[如果您只需要存储付款日期(年,月,日),不需要时间信息(时,分,...),则需要将LocalDateTime
更改为[C0 ]在您的实体上。
这样,您将有:
LocalDate
并且,使用:
List<PaymentSummary> findAllByCreatedDate(LocalDate localDate);
会工作。
如果您需要存储日期以及时间信息,则需要使用如下所示:
List<PaymentSummary> payments = paymentRepository.findAllByCreatedDate(LocalDate.now());
并使用类似的名称:
List<PaymentSummary> findAllByCreatedDateBetween(LocalDateTime startDateTime, LocalDateTime endDateTime);