java - 通过Spring Boot JPA + Postgresql + H2执行间隔

标签 java postgresql spring-boot jpa h2

我正在尝试在 Spring Boot 中创建 JPA 存储库查询。

  • 我使用的是 spring-data-jpa 2.1.5、spring-boot 2.1.3、postgresql 驱动程序 42.2.5、h2database 1.4.200、Java 1.8

我的条件是:

  • 我必须使用 PostgreSQL 进行生产,使用 H2 进行单元(集成)测试。
  • 我不应该使用 native 查询 (postgres x h2)。
  • 我需要从客户表中获取客户记录
  • 但仅限于当前日期前至少 24 小时注册的客户。
  • 所以我的意思是数据库日期。我无法使用 Java 将任何参数传递到查询中。

我的客户实体如下所示:

@Entity
@Table(name = "customer")
public class Customer {

   @Id
   @Column(name = "cust_no")
   private int custNo;

   @Temporal(TemporalType.TIMESTAMP)
   @Column(name = "registration_timestamp")
   private Date registrationTimeStamp;
}

我的客户存储库将如下所示:

@Repository
public interface CustomerRepository extends JpaRepository<Customer, Integer> {

   @Query(value = "SELECT c FROM Customer c WHERE c.registrationTimeStamp <= SOMETHING")
   List<Customer> getCustomers24HoursAfterRegistration();

}

我真的不知道我应该把那个地方放在什么地方指的是东西。我想我可以使用 CURRENT_TIMESTAMP - SOMETHING_REFERS_24_HOURS 之类的东西。 所有的例子都是关于nativeQuery的。或者从 Java 传递参数。我无法使用 INTERVAL,因为 Spring JPA 不支持。

我不敢相信这样一个简单的过程不在 Spring JPA 中,它同时支持两种 RDBMS(PostgreSql 和 H2)。我想问一下有什么办法可以做到吗?

最佳答案

如果您不想使用 native 查询,那么您必须使用查询方法

你可以做类似的事情

 List<User> findByCreatedAtGreaterThanEqual(Data createdAt);

有关更多信息,您可以查看( https://www.baeldung.com/spring-data-jpa-query-by-datehttps://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods )

关于java - 通过Spring Boot JPA + Postgresql + H2执行间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59670531/

相关文章:

python - python 中的 sql INSERT(postgres、游标、执行)

java - 如何覆盖客户端 Soap 日志记录 - Spring Boot

java - 为什么在 Java 的同一个类中不能有抽象方法和静态方法

java - future 实例列表

java - Ubuntu:数据节点无法启动

sql - 如何在单独的列中更新 SQL 查询的结果

database - 优化 Redshift 查询的大 IN 条件

在 azure 管道上运行时 Spring @Value FileNotFound

java - GraphQL api 使用 Spring Boot Resttemplate 导致 {"errors":[ {"message" :"No query string was present"}]} always

java - 无法检测 Spring AOP 中的类级别自定义注释