简体   繁体   English

弹簧(引导)与 JPA / Hibernate 和 PostgreSQL - 使用重叠日期

[英]Spring(Boot) with JPA / Hibernate and PostgreSQL - use Overlaps for dates

So i have a project where we use springBoot and PostgreSQL 10 with PostGis and hibernate.spatial for spatial queries.所以我有一个项目,我们使用 springBoot 和 PostgreSQL 10 以及 PostGis 和 hibernate.spatial 进行空间查询。 Everything works fine so far.到目前为止一切正常。

A new requirement is to find entities, which start-end dates overlap the start-end dates of the query in any possible way (ranges might be enclosing, start-overlap, inbetween, end-overlap).一个新的要求是找到实体,这些实体的开始结束日期以任何可能的方式与查询的开始结束日期重叠(范围可能是封闭、开始重叠、中间、结束重叠)。

In PostgreSQL theres the Overlaps operator that seems a pretty good fit for the job.在 PostgreSQL 中, Overlaps运算符似乎非常适合这项工作。

When trying to use it within my JPA-Query for an Entity "Sth" like this..当尝试在我的 JPA-Query 中使用它来处理这样的实体“某事”时..

select sth from Sth sth where 1=1 and (sth.start, sth.end) overlaps (:begin, :end)
// set begin and end params..

i get one of..我得到一个..

antlr.NoViableAltException: unexpected token: overlaps

antlr.NoViableAltException: unexpected AST node: (

org.postgresql.util.PSQLException: FEHLER: rt_raster_from_wkb: wkb size (5)  < min size (61)

Is it possible to use overlaps for dates with JPA without writing a native query?是否可以在不编写本机查询的情况下使用 JPA 的日期重叠?

So seems like there are three things you need to do to make it work.所以看起来你需要做三件事才能让它发挥作用。

  1. Its probably not possible to use overlaps as an operator, but luckily it seems it can also be used as a function: overlaps(start1, end1, start2, end2)它可能无法使用重叠作为运算符,但幸运的是它似乎也可以用作 function: overlays overlaps(start1, end1, start2, end2)

  2. Overlaps is not mapped by any of the hibernate-core PostgreSQL[NN]Dialects.任何 hibernate-core PostgreSQL[NN] 方言都不会映射重叠。 But it is mapped by hibernate-spatial PostgisPG[NN]Dialects, where it maps to the st_overlaps-function for spatial overlapping.但它由 hibernate-spatial PostgisPG[NN]Dialects 映射的,它映射到 st_overlaps 函数以进行空间重叠。 So you need to use your own custom Dialect that registers the overlaps function with an alias like so:因此,您需要使用自己的自定义方言来注册重叠 function ,别名如下:


public class PostgisDialect extends org.hibernate.spatial.dialect.postgis.PostgisPG95Dialect {

    public PostgisDialect() {
        super();
        registerFunction("dateoverlaps", new StandardSQLFunction("overlaps", StandardBasicTypes.BOOLEAN));
    }

}

and specify it as your spring.jpa.properties.hibernate.dialect (or spring.jpa.database-platform ♂️). and specify it as your spring.jpa.properties.hibernate.dialect (or spring.jpa.database-platform ♂️).

  1. Your JPA query must include a = true like this:您的 JPA 查询必须包含 a = true ,如下所示:

select sth from Sth sth where 1=1 and dateoverlaps(sth.start, sth.end, :begin, :end) = true

You might addtionally enhance this by using the coalesce function to handle null-values, ie您可以通过使用coalesce function 来处理空值来进一步增强这一点,即

select sth from Sth sth where 1=1 and dateoverlaps(coalesce(sth.start, sth.plannedStart), coalesce(sth.end, '3999-01-01'), :begin, :end) = true

which uses plannedStart when start is null and a long-in-the-future-date when end is null / open.当 start 为 null 时使用plannedStart ,当 end 为 null / open 时使用 long-in-the-future-date 。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring 引导 JPA/休眠 PostgreSQL - Spring Boot JPA/Hibernate PostgreSQL Spring 启动 + Hibernate JPA 配置使用EntityManager - Spring Boot + Hibernate JPA configuration to use EntityManager 如何配置Spring Boot,Spring JPA,Spring Test和Hibernate创建,使用和删除给定的PostgreSQL模式? - How can I configure Spring Boot, Spring JPA, Spring Test, and Hibernate to create, use, and drop a given PostgreSQL schema? Spring Data JPA + Hibernate +PostgreSQL - Spring Data JPA + Hibernate +PostgreSQL Spring boot Jpa:默认休眠? - Spring boot Jpa: hibernate as default? Spring Boot + JPA + Hibernate + PostgreSQL pessimistic_read与JPA本机查询 - Spring boot + JPA + Hibernate + PostgreSQL pessimistic_read with JPA native query 无法使用 spring-boot、spring-data-jpa 和 hibernate 空间在 ZE4728Fdb044B249BCBADE9 上进行地理空间查询(内部) - Unable to make geospatial query (within) with spring-boot, spring-data-jpa and hibernate spatial on postgresql db 如何在多对多关系中的一个操作中保存多个实体 [Spring Boot 2,JPA,Hibernate,PostgreSQL] - How to save multiple entity in one action related in Many to Many Relationship [Spring Boot 2, JPA, Hibernate, PostgreSQL] 如何在 Spring 引导应用程序(休眠 + JPA)中从 Postgresql 中提取图像(lob)? - How to extract images (lob) from Postgresql in a Spring Boot app (Hibernate + JPA)? Spring Boot JPA PostgreSQL重试策略 - Spring Boot JPA PostgreSQL Retry Policy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM