hibernate 5 - 将postgis数据插入表中

时间:2017-06-11 08:50:22

标签: java postgresql hibernate postgis hibernate-spatial

我使用hibernate-spatial 5.2.10和postgis,我试图将Point数据类型持久化到包含两列的DB表:

  1. name = uid,type = bigint(主键,非null)
  2. name = center,type = point
  3. 这是我的代码:

    import com.vividsolutions.jts.geom.Coordinate;
    import com.vividsolutions.jts.geom.GeometryFactory;
    import com.vividsolutions.jts.geom.Point;
    import org.hibernate.Session;
    ... 
    
    public class TestGIS {
    
      @Test
      public void testSavePosition() {
        StandardServiceRegistry standardRegistry =
            new StandardServiceRegistryBuilder().configure("hibernate.gis-test.cfg.xml").build();
        Metadata metadata =
        new MetadataSources(standardRegistry).addAnnotatedClass(Position.class)
                                             .getMetadataBuilder()
                                             .build();
        SessionFactory sessionFactory = metadata.getSessionFactoryBuilder().build();
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        GeometryFactory factory = new GeometryFactory();
        Position position = new Position(1, factory.createPoint(new Coordinate(10.0, 20.0)));
        session.persist(position);
        session.flush();
        session.close();
        sessionFactory.close();
      }
    
      @Entity
      @Table(name = "positions")
      public static class Position {
    
        @Id
        @Column
        private long uid;
    
        @Column
        private Point center;
    
        public Position(long uid, Point center) {
          this.uid = uid;
          this.center = center;
        }
    
        public Position() {
        }
    
        // getters and setters
    
      }
    }
    

    这是配置代码:

    <?xml version='1.0' encoding='utf-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    
    <hibernate-configuration>
    
      <!-- a SessionFactory instance listed as /jndi/name -->
      <session-factory
          name="java:hibernate/SessionFactory">
    
        <!-- properties -->
        <property name="connection.driver_class">org.postgresql.Driver</property>
        <property name="dialect">org.hibernate.spatial.dialect.postgis.PostgisPG95Dialect</property>
        <property name="show_sql">true</property>
        <property name="connection.url">jdbc:postgresql://127.0.0.1:2345/gis_sandbox</property>
        <property name="connection.username">****</property>
        <property name="connection.password">****</property>
        <property name="jta.UserTransaction">java:comp/UserTransaction</property>
    
      </session-factory>
    
    
    </hibernate-configuration>
    

    当我运行上面的代码时,我得到了这个massege:

    错误org.hibernate.engine.jdbc.spi.SqlExceptionHelper - 错误:列“center”的类型为point,但表达式的类型为bytea

    我应该如何将Point数据类型插入此表?

0 个答案:

没有答案