如何使用postgis和hibernate-spatial正确映射多边形类型?

时间:2016-06-03 11:16:02

标签: java hibernate postgresql postgis hibernate-spatial

假设我有下表

CREATE TABLE foo (
  id BIGSERIAL PRIMARY KEY, 
  polygon GEOMETRY(POLYGON)
);

和实体类

@Table
@Entity
public class Foo {

   @Id
   @GeneratedValue(strategy = IDENTITY)
   private Long id;

   private Polygon polygon;

}

我设法保存了一个Foo实体,但是,我无法从数据库中选择它们。我得到了这个例外:

java.lang.NumberFormatException: For input string: "PO"

然后,我在多边形字段顶部添加了以下注释:

@Type(type = "org.hibernate.spatial.JTSGeometryType")

但它会抛出  另一个例外,说这种类型无法实例化:

org.hibernate.MappingException: Could not instantiate Type: org.hibernate.spatial.JTSGeometryType

请注意我使用5.1.0.Final版本用于hibernate和hibernate-spatial。

谢谢

3 个答案:

答案 0 :(得分:7)

似乎hibernate-spartial 5.x知道如何本地处理JTS几何类型,因此不需要类型注释。这是我的工作配置。

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-spatial</artifactId>
  <version>5.2.3.Final</version>
</dependency>

MySQL表...

CREATE TABLE `stuff` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `coordinates` point NOT NULL,
  PRIMARY KEY (`id`),
  SPATIAL KEY `coordinates` (`coordinates`)
)

JPA实体......

import com.vividsolutions.jts.geom.Point;
...

@Basic(optional = false)
@NotNull
@Column(nullable = false, columnDefinition = "point")
private Point coordinates;

Hibernate方言......

<property name="hibernate.dialect" value="org.hibernate.spatial.dialect.mysql.MySQLSpatialDialect"/>

请注意,我的应用程序在WildFly 10中运行,它提供了额外的运行时依赖性,如MySQL驱动程序。

答案 1 :(得分:0)

你应该尝试给出列名

@Entity<br/>
@Table(name = "table_name")<br/>
public class Foo {<br/>

@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;

@Type(type = "org.hibernate.spatial.GeometryType")
@Column(name = "the_geom", nullable = true,columnDefinition="Geometry")
 private Geometry geom;

@Type(type = "org.hibernate.spatial.GeometryType",columnDefinition="Geometry")
private Polygon polygon;
}

你还应该知道,从Hibernate Spatial 4.0-M1开始,只有几何类型被指定给Hibernate,因此@Column注释必须设置columnDefinition="Geometry",而不是Point或其他任何东西。这可能会在将来修复。

通过这个修改选集,我终于可以写一个数据库了!正确的属性规范是:

 @Column(columnDefinition="Geometry")
 @Type(type = "org.hibernate.spatial.GeometryType")
 private Point centerPoint;

还要在 hibernate.cfg.xml

中查看方言

将以下行添加到 hibernate.cfg.xml

<property name="dialect">org.hibernate.spatial.dialect.postgis.PostgisDialect</property>

答案 2 :(得分:0)

I downgraded to hibernate-spatial 4.3 version. Please see my configuration below.

Entity column:

@Column(columnDefinition = "Geometry")
@Type(type = "org.hibernate.spatial.GeometryType")
private Polygon polygon;

The Polygon class can be found in com.vividsolutions.jts.geom package.

Dependencies:

<dependencies>
     <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-spatial</artifactId>
        <version>4.3</version>
    </dependency>
    <dependency>
        <groupId>org.postgis</groupId>
        <artifactId>postgis-jdbc</artifactId>
        <version>1.3.3</version>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.4.1208.jre7</version>
    </dependency>
</dependencies>

Repositories:

<repositories>
    <repository>
        <id>OSGEO GeoTools repo</id>
        <url>http://download.osgeo.org/webdav/geotools</url>
    </repository>
    <repository>
        <id>Hibernate Spatial repo</id>
        <url>http://www.hibernatespatial.org/repository</url>
    </repository>
</repositories>

Hibernate dialect:

org.hibernate.spatial.dialect.postgis.PostgisDialect