开发者

PostgreSQL/PostGIS stored function + Eclipselink converter problem

I use PostgreSQL with PostGIS and Eclipselink. In the Postgres database I have some stored functions, as I know with Postgres stored functions + Eclipselink doesn't work the @NamedStoredProcedureQuery annotation, StoredProcedureCall and StoredFunctionCall methods, therefore i use @NamedNativeQuery. I wrote a GeometryConverter, that works fine when I persist

em.persist(e);

my entity, and with

@NamedNativeQuery(name = "getEntity", query = "SELECT * FROM GET_ENTITY(?)", resultClass = Entity.class)

Here is my 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 v开发者_高级运维oid initialize(DatabaseMapping dm, Session session) {
        dm.getField().setSqlType(java.sql.Types.OTHER);
    }

But when I want to use this stored function


@NamedNativeQuery(name = "getEntityGeom", query = "SELECT * FROM GET_ENTITY_GEOM(?)")

the ? parameter is a Postgis/Postgresql geometry type, it throws the following exception:

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.

I can see that the converter doesn't convert the org.postgis.MultiLineString to org.postgis.PGgeometry. What should I set to work my converter?

Thanks for your help! Tamas J


A Converter can only be used in a mapping to convert data. Your native query just has an argument, with no typing info, so EclipseLink has no idea how to convert it.

You may be able to define a StructConverter instead, this will always convert the type. This is how EclipseLink supports Oracle spatial types, so it should in thoery work for PostGIS as well.

Otherwise, you could try executing the native query with the PGgeometry value instead of the Geometry class. You could also try using a DataReadQuery directly, which should allow you to set the argument type directly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜