私は休止状態の空間について学んでおり、このチュートリアルに従おうとしています
MultiPolygon を Postgres データベースに永続化しようとしています。
私のデータベーススクリプト:
create database multipolygon_test;
create extension postgis;
create extension postgis_topology;
create table test(
id bigint
);
SELECT AddGeometryColumn ('public','test','the_geom',4326,'MULTIPOLYGON',2);
私は自分のプロジェクトでこれらの依存関係を使用しています:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-spatial</artifactId>
<version>4.3</version>
<exclusions>
<exclusion>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
</exclusion>
<exclusion>
<groupId>org.postgis</groupId>
<artifactId>postgis-jdbc</artifactId>
</exclusion>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.7.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.7.Final</version>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901.jdbc4</version>
</dependency>
<dependency>
<groupId>org.postgis</groupId>
<artifactId>postgis-jdbc</artifactId>
<version>1.3.3</version>
</dependency>
私のエンティティクラスは次のようになります。
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.Type;
import com.vividsolutions.jts.geom.MultiPolygon ;
@Entity
@Table(name = "test")
public class Test implements Serializable {
private static final long serialVersionUID = -8097794061468336996L;
@Id
private long id;
@Column(name = "the_geom", columnDefinition = "org.postgis.MultiPolygon")
@Type(type = "org.hibernate.spatial.GeometryType")
private MultiPolygon geometry;
//--- getters & setters ommited
私の持続性xml
<persistence
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit
name="org.hibernate.events.jpa"
transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>test.Test</class>
<properties>
<property
name="hibernate.dialect"
value="org.hibernate.spatial.dialect.postgis.PostgisDialect" />
<property
name="hibernate.connection.driver_class"
value="org.postgresql.Driver" />
<property
name="hibernate.connection.url"
value="jdbc:postgresql://localhost:5432:multipolygon_test" />
<property
name="hibernate.connection.username"
value="" />
<property
name="hibernate.connection.password"
value="" />
<property
name="hibernate.connection.pool_size"
value="5" />
<property
name="hibernate.show_sql"
value="true" />
<property
name="hibernate.format_sql"
value="true" />
<property
name="hibernate.max_fetch_depth"
value="5" />
<property
name="hibernate.hbm2ddl.auto"
value="update" />
</properties>
</persistence-unit>
</persistence>
JPAUtil クラス:
public class JPAUtil {
private static final EntityManagerFactory emFactory;
static {
try {
emFactory = Persistence.createEntityManagerFactory("org.hibernate.events.jpa");
} catch (Throwable ex) {
System.err.println("Cannot create EntityManagerFactory.");
throw new ExceptionInInitializerError(ex);
}
}
public static EntityManager createEntityManager() {
return emFactory.createEntityManager();
}
public static void close() {
emFactory.close();
}
}
呼び出し :
public static void main(String[] args) {
String polygon = "MULTIPOLYGON(((30 20, 45 40, 10 40, 30 20)),((15 5, 40 10, 10 20, 5 10, 15 5)))";
WKTReader fromText = new WKTReader(new GeometryFactory(new PrecisionModel(), 4326));
Geometry geom = null;
try {
geom = fromText.read(polygon);
} catch (ParseException e) {
throw new RuntimeException("Not a WKT string:" + polygon);
}
EntityManager em = JPAUtil.createEntityManager();
em.getTransaction().begin();
Test test = new Test();
test.setId(1);
test.setGeometry(((com.vividsolutions.jts.geom.MultiPolygon) geom));
em.persist(test);
em.getTransaction().commit();
em.close();
JPAUtil.close();
}
例外 :
Caused by: org.postgresql.util.PSQLException: Unknown type geometry.
at org.postgresql.jdbc2.AbstractJdbc2Statement.setPGobject(AbstractJdbc2Statement.java:1603)
at org.postgresql.jdbc2.AbstractJdbc2Statement.setObject(AbstractJdbc2Statement.java:1795)
at org.postgresql.jdbc3g.AbstractJdbc3gStatement.setObject(AbstractJdbc3gStatement.java:37)
at org.postgresql.jdbc4.AbstractJdbc4Statement.setObject(AbstractJdbc4Statement.java:46)
at org.hibernate.spatial.dialect.AbstractJTSGeometryValueBinder.bind(AbstractJTSGeometryValueBinder.java:48)
at org.hibernate.spatial.dialect.AbstractJTSGeometryValueBinder.bind(AbstractJTSGeometryValueBinder.java:39)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:286)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:281)
at org.hibernate.type.AbstractSingleColumnStandardBasicType.nullSafeSet(AbstractSingleColumnStandardBasicType.java:56)
at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2843)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3121)
... 12 more
何が問題なのかわかりません。MultiPolygon を com.vividsolutions.jts.geom.Geometry、org.postgis.Geometry、org.postgis.MuliPolygon に置き換えようとしましたが、columnDefiniton を「Geometry(multipolygon, 4326)」として指定しようとしましたが、役に立ちませんでした。
私が考えることができる唯一のことは、依存関係が台無しになっているということですが、休止状態 4.2.0 で休止状態空間 4.0.1 を使用してみました。そしてそれは助けにはなりませんでした。