【问题标题】:hibernate spatial 5 - Postgis2.2 : Query issues休眠空间 5 - Postgis2.2:查询问题
【发布时间】:2016-12-03 01:39:58
【问题描述】:

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

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

在我的数据库中,我得到了这个专栏:

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

for each museum

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

@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 不一样? 关于如何让它发挥作用的任何想法?

谢谢!

【问题讨论】:

    标签: hibernate geometry hql postgis spatial


    【解决方案1】:

    我发现了问题。

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

    添加此 Setter 以具有相同的 SRID

    geom.setSRID(4326);
    

    然后我将查询更改为:

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

    并且还将我的 db 类更改为:

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

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

    玩得开心

    【讨论】:

      【解决方案2】:

      我能够使用休眠将“几何”数据存储在 postgis 表中。

      我的 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 列类型:几何

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-12-06
        • 2013-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-15
        • 1970-01-01
        相关资源
        最近更新 更多