[英]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.所以看起来你需要做三件事才能让它发挥作用。
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)
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
♂️).
= true
like this:= 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.