【问题标题】:PostgreSQL/PostGIS stored function + Eclipselink converter problemPostgreSQL/PostGIS存储函数+Eclipselink转换器问题
【发布时间】:2011-02-08 22:27:13
【问题描述】:

我将 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。我应该设置什么来使用我的转换器?

感谢您的帮助! 塔马斯J

【问题讨论】:

    标签: postgresql stored-procedures eclipselink converter postgis


    【解决方案1】:

    转换器只能在映射中用于转换数据。您的本机查询只有一个参数,没有输入信息,因此 EclipseLink 不知道如何转换它。

    您可以改为定义一个 StructConverter,这将始终转换类型。这就是 EclipseLink 支持 Oracle 空间类型的方式,因此理论上它也应该适用于 PostGIS。

    否则,您可以尝试使用 PGgeometry 值而不是 Geometry 类执行本机查询。您也可以尝试直接使用 DataReadQuery,这应该允许您直接设置参数类型。

    【讨论】:

    • 感谢您的帮助! StructConverter 和 DataReadQuery 不起作用,或者我做错了什么,但使用 PGgeometry 而不是 Geometry 有效。
    猜你喜欢
    • 1970-01-01
    • 2012-07-04
    • 2020-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-02
    • 2010-11-25
    相关资源
    最近更新 更多