hibernate spatial 5 - Postgis2.2:查询问题

hibernate spatial 5 - Postgis2.2 : Query issues

刚开始使用 postgis 和 hibernate statial,我在查询时遇到了一些问题。

目标:从(其)几何类型对象中获取博物馆

在我的数据库中,我得到了这一列:

name: geom  
type: geometry(Point,4326))
that contains something like: 0101000020E6100000004C8E1516D(...) 

for each museum

然后,我有一个博物馆 class 有:

@Column(name = "geom", columnDefinition = "geometry(Point,4326)")
private Geometry           geometry;

这是我的查询:

WKTReader fromText = new WKTReader();
        try {
            //LON and LAT are the museum's coordinates
            Geometry geom = fromText.read("POINT("+lon+" "+lat+")");
            Session hibernateSession = getCurrentSession();

            Museum result = hibernateSession
                    .createQuery("from Museum where geometry = :geometry")
                    .setParameter("geometry", geom).uniqueResult();
            return result;


        } catch (ParseException e) {
            (...)
        }

但是当我尝试执行它时,我得到了这个错误:

ERROR: operator is not unique: geometry = bytea
Indice : Could not choose a best candidate operator. You might need to add explicit type casts.

所以我在想,也许来自 hibernate 的 Geometry 和来自 Postgis 的 Geometry 不一样? 知道如何让它发挥作用吗?

谢谢!

我发现了问题。

首先,我要确保我的 .properties 文件中包含 postgis 方言。

添加此 Setter 以具有相同的 SRID

geom.setSRID(4326);

然后我将查询更改为:

.createQuery("from Museum where equals(geometry, :geometry) = true")

并且还将我的数据库 class 更改为:

@Column(name = "geom", columnDefinition = "Geometry")

现在完美运行。这可能会帮助遇到同样问题的人...

玩得开心

我能够使用休眠在 postgis table 中存储 'geometry' 数据。

我的pom.xml

       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-spatial</artifactId>
            <version>5.4.2.Final</version>
        </dependency>

休眠实体:

 @Entity
 public class XYZ {

  @EmbeddedId
  private Key id;
  @Column(columnDefinition = "Geometry")
  private Point startLoc;
  @Column(columnDefinition = "Geometry")
  private Point endLoc;
 }  

DAOImpl:

    Point start = new GeometryFactory().createPoint(newCoordinate(151.203446, 
             -33.867347, 1550285443));
    start.setSRID(4283);
    Point end= new GeometryFactory().createPoint(new Coordinate(151.203446, 
    -33.867347, 1550285443));
    end.setSRID(4283);
    summary.setId(key);
    try {
        summary.setStartLoc( start);
        summary.setEndLoc( start);
    } catch (Exception e){
    }
    iSaferJourneySummaryDAO.save(summary);
  } 
  • application.properties 中的重要一点: spring.jpa.properties.hibernate.dialect=org.hibernate.spatial.dialect.postgis.PostgisDialect

PostGIS 列类型:几何