因此,我有一个项目,其中我们使用springBoot和PostgreSQL 10与PostGis和hibernate.spatial一起进行空间查询。到目前为止一切都很好。
一个新的要求是查找实体,这些实体以任何可能的方式与查询的起始日期重叠(范围可能是包围、开始重叠、中间重叠、结束重叠)。
在PostgreSQL中,重叠操作符似乎非常适合这项工作。
当试图在我的JPA-查询中使用它时,像这样的实体"Sth“。
select sth from Sth sth where 1=1 and (sth.start, sth.end) overlaps (:begin, :end)
// set begin and end params..我得到一个..。
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)是否可以在不编写本机查询的情况下对JPA的日期使用重叠?
发布于 2020-06-30 12:49:40
因此,似乎有三件事,你需要做,使它的工作。
overlaps(start1, end1, start2, end2)。
public class PostgisDialect extends org.hibernate.spatial.dialect.postgis.PostgisPG95Dialect {
public PostgisDialect() {
super();
registerFunction("dateoverlaps", new StandardSQLFunction("overlaps", StandardBasicTypes.BOOLEAN));
}
}并将其指定为spring.jpa.properties.hibernate.dialect (或spring.jpa.database-platform♂️)。
= true:
select sth from Sth sth where 1=1 and dateoverlaps(sth.start, sth.end, :begin, :end) = true您可以通过使用coalesce函数来处理空值(即
select sth from Sth sth where 1=1 and dateoverlaps(coalesce(sth.start, sth.plannedStart), coalesce(sth.end, '3999-01-01'), :begin, :end) = true它在start为null时使用plannedStart,当end为null / open时使用长时间的将来日期。
https://stackoverflow.com/questions/62657855
复制相似问题