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

时间:2016-07-28 15:04:19

标签: hibernate geometry hql postgis spatial

刚开始使用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.

所以我在想,也许来自冬眠的几何图形和Postgis的几何图形是不一样的? 关于如何使其发挥作用的任何想法?

谢谢!

2 个答案:

答案 0 :(得分:2)

我发现了问题。

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

添加此Setter以具有相同的SRID

geom.setSRID(4326);

然后我将查询更改为:

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

还将我的db类改为:

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

现在完美运作。这可能会帮助有同样问题的人......

玩得开心

答案 1 :(得分:1)

我能够使用休眠功能将'geometry'数据存储在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列类型:几何