无法在Spring和Hibernate 5.0中执行空间查询 [英] Can't execute spatial query with spring and hibernate 5.0

查看:74
本文介绍了无法在Spring和Hibernate 5.0中执行空间查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的模特

package objects;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.Point;

import javax.persistence.*;

/**
 * Created by michael on 29/10/15.
 */
@Entity
public class Location {

    public Geometry getShape() {
        return shape;
    }

    public void setShape(Geometry shape) {
        this.shape = shape;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private Geometry shape;

    public Location() {
    }

    @Override
    public String toString() {
        return "Location{" +
                "id=" + id +
                ", shape=" + shape +
                '}';
    }
}

这是我的控制器

@RestController
public class ObjectController {
@Bean
public Module jtsModule() {
    return new JtsModule();
}

@Autowired
private LocationRepository repository;


@RequestMapping(name = "/shape", method = RequestMethod.POST)
public Collection<Location> createObjects() throws JsonProcessingException, ParseException {
    WKTReader wktReader = new WKTReader();
    Geometry geom = wktReader.read("POINT(-105 -105)");
    Geometry filter = wktReader.read("POLYGON((-107 39, -102 41, -107 41, -107 39))");
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new JtsModule());
    Location location = new Location();
    GeometryFactory factory = new GeometryFactory();
    GeometricShapeFactory f = new GeometricShapeFactory(factory);
    location.setShape(geom);
    repository.save(location);
    f.setCentre(new Coordinate(50, 50));
    f.setSize(100);
    Polygon circle = f.createCircle();

    return repository.findWithin(filter);

}

}

这是我的资料库

@Repository
public interface LocationRepository extends CrudRepository<Location, Long> {
    @Query("select l from Location l where within(l.shape, ?) = true")
    List<Location> findWithin(Geometry geometry);
}

这是我的配置

spring:
  profiles: production

  datasource:
    platform: postgres
    url: jdbc:postgresql://192.168.99.100:5432/db
    username: user
    password: password

  database:
    driverClassName: org.postgresql.Driver

  jpa:
    database: POSTGRESQL
    platform: postgres
    show-sql: true
    ddl-auto: update
    hibernate:
      spatial:
        dialect:
          postgis: PostgisDialect
---

spring:
  profiles: development

  datasource:
  platform: h2
  url: jdbc:h2:mem:test

  jpa:
    hibernate:
      show-sql: true
      spatial:
        dialect:
          h2geodb: GeoDBDialect

我正在使用postgis,如果我删除查询并仅保存几何图形,它将可以正常工作.所以我想空间支撑确实有效.

I'm using postgis and if I would remove query and only save geometries it would work fine. So I guess the spatial support is really working.

推荐答案

好的,所以问题出在配置上.

Okay, so the problem was in configuration.

正确的配置应如下所示

  jpa:
    database: POSTGRESQL
    database-platform: org.hibernate.spatial.dialect.postgis.PostgisDialect
    show-sql: true
    hibernate:
      ddl-auto: update

这篇关于无法在Spring和Hibernate 5.0中执行空间查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆