休止状態および休止状態空間を使用するアプリケーションによってアクセスされる Postgres データベースに Geography 型の列を追加しました。休止状態でその列にアクセスするたびに、「タイプ org.postgresql.util.PGobject のオブジェクトを変換できません」というエラーが表示されます。古いバージョンの hibernate spatial を使用していましたが、最新バージョンでも Geography データ型をサポートしていないようです。これは本当にそうですか、それとも私が何かを逃しただけですか。この制限を回避する方法はありますか?
2808 次
1 に答える
0
Glassfish 3.1.2 + Toplink を使用して同様の問題が発生しました。そして、SessionCustomizerを実装して解決しました。地理データ型にも同じアプローチを使用できると思います。
PostGISJPACustomizer.java
public class PostGISJPACustomizer implements SessionCustomizer {
@Override
public void customize(Session s) throws Exception {
RelationalDescriptor desc01 = (RelationalDescriptor) s.getDescriptor(SentenceGPS.class);
DirectToFieldMapping mapping01 = (DirectToFieldMapping) desc01.getMappingForAttributeName("geom");
mapping01.setConverter(null);
RelationalDescriptor desc02 = (RelationalDescriptor) s.getDescriptor(SentenceLine.class);
DirectToFieldMapping mapping02 = (DirectToFieldMapping) desc02.getMappingForAttributeName("geom");
mapping02.setConverter(null);
RelationalDescriptor desc03 = (RelationalDescriptor) s.getDescriptor(SentencePolygon.class);
DirectToFieldMapping mapping03 = (DirectToFieldMapping) desc03.getMappingForAttributeName("geom");
mapping03.setConverter(null);
}
}
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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">
<persistence-unit name="onet-ejbPU" transaction-type="JTA">
<jta-data-source>jdbc/onet</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.session.customizer" value="com.util.PostGISJPACustomizer"/>
</properties>
</persistence-unit>
</persistence>
文GPS.java (部分)
@Entity
public class SentenceGPS implements Serializable {
@Column
private PGgeometry geom;
public void setGeom(LineString geom) {
this.geom = GeoUtils.getPGeo(geom);
}
public LineString getGeom() {
return (LineString) GeoUtils.getJTS(geom);
}
}
GeoUtils.java
public class GeoUtils {
public static PGgeometry getPGeo(Geometry g) throws SQLException {
String PGgeometryStr = "SRID=" + g.getSRID() + ";" + g.toString();
PGgeometry pg = new PGgeometry(PGgeometry.geomFromString(PGgeometryStr));
return pg;
}
// This function needs improvements due peformance issues
public static Geometry getJTS(PGgeometry g) throws ParseException, SQLException {
WKTReader reader = new WKTReader(new GeometryFactory(new PrecisionModel(PrecisionModel.FLOATING), g.getGeometry().getSrid()));
String geometryString = g.getGeometry().toString();
Geometry geom;
if (geometryString.indexOf(';') != -1) {
String[] temp = PGgeometry.splitSRID(geometryString);
int srid = Integer.parseInt(temp[0].substring(5));
geom = (Geometry) reader.read(temp[1]);
geom.setSRID(srid);
} else {
geom = (Geometry) reader.read(geometryString);
}
return geom;
}
}
于 2012-08-08T20:09:12.617 に答える