[英]Problem calling npgsql stored function of a postgresql 10.10 database with postgis from EF Core 3.0
[英]PostgreSQL/PostGIS stored function + Eclipselink converter problem
我將PostgreSQL與PostGIS和Eclipselink一起使用。 在Postgres數據庫中,我有一些存儲的函數,正如我對Postgres的存儲函數所知,Eclipselink無法使用@NamedStoredProcedureQuery批注,StoredProcedureCall和StoredFunctionCall方法,因此我使用@NamedNativeQuery。 我寫了一個GeometryConverter,當我堅持時可以正常工作
em.persist(e);
我的實體,並與
@NamedNativeQuery(name = "getEntity", query = "SELECT * FROM GET_ENTITY(?)", resultClass = Entity.class)
這是我的GeometryConverter:
public Geometry convertDataValueToObjectValue(Object dataValue, Session session) {
if (dataValue == null) {
return null;
} else if (dataValue instanceof PGgeometry) {
return ((PGgeometry) dataValue).getGeometry();
} else {
log.severe("dataValue not instance of PGgeometry");
return null;
}
}
@Override
public PGgeometry convertObjectValueToDataValue(Object objectValue, Session session) {
if (objectValue == null) {
return null;
// } else if (objectValue instanceof Geometry) {
// return new PGgeometry((Geometry)objectValue);
} else if (objectValue instanceof Point) {
return new PGgeometry((Point)objectValue);
} else if (objectValue instanceof MultiPoint) {
return new PGgeometry((MultiPoint)objectValue);
} else if (objectValue instanceof LineString) {
return new PGgeometry((LineString)objectValue);
} else if (objectValue instanceof MultiLineString) {
return new PGgeometry((MultiLineString)objectValue);
} else if (objectValue instanceof Polygon) {
return new PGgeometry((Polygon)objectValue);
} else if (objectValue instanceof MultiPolygon) {
return new PGgeometry((MultiPolygon)objectValue);
} else if (objectValue instanceof GeometryCollection) {
return new PGgeometry((GeometryCollection)objectValue);
} else {
log.severe("objectValue not instance of Geometry");
return new PGgeometry();
}
}
@Override
public void initialize(DatabaseMapping dm, Session session) {
dm.getField().setSqlType(java.sql.Types.OTHER);
}
但是當我想使用這個存儲功能時
@NamedNativeQuery(name = "getEntityGeom", query = "SELECT * FROM GET_ENTITY_GEOM(?)")
? 參數是Postgis / Postgresql geometry
類型,它引發以下異常:
Internal Exception: org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of org.postgis.MultiLineString. Use setObject() with an explicit Types value to specify the type to use.
我可以看到轉換器沒有將org.postgis.MultiLineString轉換為org.postgis.PGgeometry。 我應該如何設置才能使用轉換器?
謝謝你的幫助! 塔瑪斯(Tamas J)
轉換器只能用於映射中以轉換數據。 您的本機查詢只有一個參數,沒有任何鍵入信息,因此EclipseLink不知道如何轉換它。
您也許可以定義一個StructConverter,這將始終轉換類型。 這就是EclipseLink支持Oracle空間類型的方式,因此它也應同樣適用於PostGIS。
否則,您可以嘗試使用PGgeometry值而不是Geometry類執行本機查詢。 您也可以嘗試直接使用DataReadQuery,這應允許您直接設置參數類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.