Hibernate 5.0.1将PostGIS数据存储为字节而不是空间类型

时间:2015-09-18 08:39:37

标签: java postgresql postgis hibernate-spatial hibernate-5.x

我正在尝试使用以下技术组合设置新项目

  • Hibernate + spatial 5.0.1
  • PostgreSQL 9.4 with PostGIS 2.1.8
  • Spring 4.2.1
  • JTS 1.13

实体。我发现org.hibernate.spatial.GeometryType已被删除,现在似乎已移至geo-latte-1.0.jar中的org.geolatte.geom.GeometryType

@Entity
@Table(name = "AREA")
@NamedQueries(@NamedQuery(name = "findAllAreas", query = "SELECT t FROM Area t"))
public class Area {
    private long id;
    private String info;
    private Geometry location;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    @Column(name = "INFO")
    public String getInfo() {
        return info;
    }
    public void setInfo(String info) {
        this.info = info;
    }
    @Column(name = "POLYGON")
    @Type(type = "org.geolatte.geom.GeometryType")
    public Geometry getLocation() {
        return location;
    }
    public void setLocation(Geometry location) {
        this.location = location;
    }

}

Spring setup

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan">
        <array>
            <value>org.example.entity</value>
        </array>
    </property>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.dialect">org.hibernate.spatial.dialect.postgis.PostgisDialect
            </prop>
        </props>
    </property>
</bean>

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.postgresql.Driver" />
    <property name="url" value="jdbc:postgresql://localhost:5432/sadb" />
    <property name="username" value="username" />
    <property name="password" value="password" />
</bean>

数据库设置

  • Windows ZIP版
  • 在PostGIS安装中复制
  • 新的PostGIS数据库并创建了所有扩展名postgis,postgis_topology,fuzzystrmatch,postgis_tiger_geocoder

结果

我可以坚持并检索区域实体而不会出现问题。然而,列上的类型似乎是字节数据,而不是PostGIS空间类型..任何想法我做错了什么?

sadb=> \d+ area;
                                 Table "public.area"
 Column  |          Type          | Modifiers | Storage  | Stats target | Description
---------+------------------------+-----------+----------+--------------+------------
 id      | bigint                 | not null  | plain    |              |
 info    | character varying(255) |           | extended |              |
 polygon | bytea                  |           | extended |              |

1 个答案:

答案 0 :(得分:5)

使用Hibernate 5+,您不需要@Type注释。包括它似乎导致我看到的错误。删除它可以解决问题。多边形列如何具有PostGIS类型的几何体。