代码之家  ›  专栏  ›  技术社区  ›  Adam

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

  •  5
  • Adam  · 技术社区  · 9 年前

    我正在尝试用以下技术组合建立一个新项目

    • 休眠+空间5.0.1
    • PostgreSQL 9.4与PostGIS 2.1.8
    • 弹簧4.2.1
    • 第1.13节

    实体我发现了 org.hibernate.spatial.GeometryType 已删除,现在似乎已移动到 org.geolatte.geom.GeometryType 在地质标签-1.0罐中

    @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;
        }
    
    }
    

    弹簧设置

    <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 回复  |  直到 9 年前
        1
  •  5
  •   Adam    9 年前

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