Hibernate Spatial 4和PostGIS 2.0


10

我在整合这些技术时遇到了一些问题:

  • 休眠空间4.0-M1
  • PostGIS 2.0.2(使用已编译的JDBC 2.0.2)
  • 休眠4.1.1

具体错误是:

Caused by: org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of org.postgis.PGgeometry. Use setObject() with an explicit Types value to specify the type to use.

实体注释为:

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

Point创建示例为:

Location location = new Location();
WKTReader fromText = new WKTReader();
Point geom = null;
try {
    geom = (Point) fromText.read("POINT(-56.2564083434446 -34.8982159791812)");
} catch (ParseException e) {
    throw new RuntimeException("Not a WKT string:" + "SRID=4326;POINT(-56.2564083434446 -34.8982159791812)");
}
if (!geom.getGeometryType().equals("Point")) {
    throw new RuntimeException("Geometry must be a point. Got a " + geom.getGeometryType());
}
location.setGeom(geom);
locationDAO.insert(location);

Answers:


5

我使用的是Tomcat,它是连接池设施。我只是通过JNDI向我的应用程序公开了一个数据源。

这对我有用:

  • 当我将Maven依赖项包含在hibernate-spatial中时,它对hibernate本身,postgresql的jdbc和postgis的jdbc具有传递性依赖。因此,我要做的是删除这些依赖项(已过时)。我的pom看起来像这样:
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-spatial</artifactId>
    <version>4.0</version>
    <exclusions>
        <exclusion>
            <artifactId>hibernate-core</artifactId>
            <groupId>org.hibernate</groupId>
        </exclusion>
        <exclusion>
            <artifactId>postgis-jdbc</artifactId>
            <groupId>org.postgis</groupId>
        </exclusion>
        <exclusion>
            <artifactId>postgresql</artifactId>
            <groupId>postgresql</groupId>
        </exclusion>
    </exclusions>
</dependency>

Postgis jdbc是所需的postgresql jdbc的扩展。

  • 然后,我克隆了postgis存储库并编译了它们的jdbc扩展名。只需mvn packagejava/jdbc目录中运行即可。阅读其自述文件。
  • 然后,我将最新的postgresql-jdbc和最新编译的postgis jdbc放置在tomcat的lib目录中
  • 在tomcat的服务器配置上,我将数据库url更改为jdbc:postgresql_postGIS://localhost:5432/mydatabase。注意postgresql_postGIS零件。我也将驱动程序类更改为org.postgis.DriverWrapper。这是一个向本地jdbc注册postgis类型的包装器。

这是我在tomcat中的最终资源配置:

<Resource auth="Container"
          maxActive="120" maxIdle="10" name="jdbc/myapp"
          username="myuser" password="mypassword"
          poolPreparedStatements="true" type="javax.sql.DataSource" 
          driverClassName="org.postgis.DriverWrapper"
          validatingQuery="select 1"
          url="jdbc:postgresql_postGIS://localhost:5432/myapp"/>

postgis jdbc的自述文件中通常描述了此过程。因此,请确保您已阅读

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.