应用程序属性:
spring.datasource.url=jdbc:postgresql://localhost:5432/test_postgis
spring.datasource.username=postgres
spring.datasource.password=1234
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.spatial.dialect.postgis.PostgisDialect
spring.jpa.hibernate.ddl-auto=update
依赖关系:
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation group: 'org.hibernate.orm', name: 'hibernate-spatial', version: '6.5.0.Final'
//dont know if its necessary nothing changes with or without
implementation group: 'org.locationtech.jts', name: 'jts-core', version: '1.19.0'
实体:
import org.locationtech.jts.geom.Polygon;
import jakarta.persistence.*;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@Entity
@Table(name = "GEOM")
public class Geom {
@Id
@Column(name = "ID_GEOM")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "POLYG", columnDefinition = "geometry(Polygon,4326)")
private Polygon polyg;
}
表:
CREATE TABLE geom (
id_geom SERIAL PRIMARY KEY,
polyg GEOMETRY(POLYGON, 4326)
);
编译错误:
org.hibernate.boot.registry.selector.spi.StrategySelectionException:
Unable to resolve name [org.hibernate.spatial.dialect.postgis.PostgisDialect]
as strategy [org.hibernate.dialect.Dialect]
尝试从 org.hibernate.orm 更改为旧的 org.hibernate,仍然遇到相同的错误
尝试删除 spring.jpa.properties.hibernate.dialec 属性,然后它编译并运行,但是当我尝试请求它时,它给出以下错误:
[nio-8080-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : Ignoring exception,
response committed already: org.springframework.http.converter.HttpMessageNotWritableException
Could not write JSON: Infinite recursion (StackOverflowError)
[nio-8080-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved
[org.springframework.http.converter.HttpMessageNotWritableException:
Could not write JSON: Infinite recursion (StackOverflowError)]
响应 JSON:
[{"id":1,"polyg":{"envelope":{"envelope":{"envelope":{"envelope":..........}}}}}}}}}
尝试了不同的JTS,例如vividsolution jts,但它似乎已移至jts-core
尝试了不同的和旧版本的休眠空间甚至尝试了 PgPolygon
没有任何效果
编辑:已经尝试了在以下位置找到的所有内容:
您应该删除
spring.jpa.properties.hibernate.dialect=org.hibernate.spatial.dialect.postgis.PostgisDialect
。您不需要为 Hibernate 6 及更高版本指定空间方言,标准解析机制应该只适用于 Postgresql + Postgis。