0

我想返回此 SQL Postgis 查询的输出(几何类型的输出为“0101000020E610000000000000000024400000000000002440”)

select ST_GeometryFromText('POINT(10 10)',4326)

在 Java 中与休眠。

我尝试了以下方法:

public String computeGeomFromLongitudeLatitude() {
    Session session = getSession();
    String geom = (String) session
            .createSQLQuery(
                    "select ST_GeometryFromText('POINT(10 10)',4326)").list().get(0);
    closeSession();

    return geom;
}

这对我不起作用。我得到以下异常

Exception in thread "main" org.hibernate.MappingException: No Dialect mapping for JDBC type: 1111
    at org.hibernate.dialect.TypeNames.get(TypeNames.java:76)
    at org.hibernate.dialect.TypeNames.get(TypeNames.java:99)
    at org.hibernate.dialect.Dialect.getHibernateTypeName(Dialect.java:563)
    at org.hibernate.loader.custom.CustomLoader$Metadata.getHibernateType(CustomLoader.java:660)
    at org.hibernate.loader.custom.CustomLoader$ScalarResultColumnProcessor.performDiscovery(CustomLoader.java:564)
    at org.hibernate.loader.custom.CustomLoader.autoDiscoverTypes(CustomLoader.java:580)
    at org.hibernate.loader.Loader.getResultSet(Loader.java:1986)
    at org.hibernate.loader.Loader.doQuery(Loader.java:829)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:289)
    at org.hibernate.loader.Loader.doList(Loader.java:2463)
    at org.hibernate.loader.Loader.doList(Loader.java:2449)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2279)
    at org.hibernate.loader.Loader.list(Loader.java:2274)
    at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:331)
    at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:1585)
    at org.hibernate.internal.AbstractSessionImpl.list(AbstractSessionImpl.java:224)
    at org.hibernate.internal.SQLQueryImpl.list(SQLQueryImpl.java:156)
    at database.BerlinPolygonDAO.computeGeomFromLongitudeLatitude(BerlinPolygonDAO.java:305)
    at database.BerlinPolygonDAO.main(BerlinPolygonDAO.java:330)

我想我明白问题所在。我尝试将结果解析为字符串,但它是几何对象。但是我该怎么做才能在Java中将此输出作为字符串?如果我只在 SQL 查询后面添加 .list() ,我已经得到了一个例外。

非常感谢您的想法。

编辑:

我在 hibernate.cfg 中指定了正确的对话框

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/GIS</property>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="hibernate.connection.username">postgres</property>
        <property name="hibernate.connection.password">mysql15</property>
        <property name="hibernate.current_session_context_class">thread</property>
        <property name="hibernate.hbm2ddl.auto">create</property>
        <property name="hibernate.show_sql">false</property>

        <mapping class="database.tables.flickr.User" />
        <mapping class="database.tables.flickr.Photo" />
        <mapping class="database.tables.flickr.Tag" />
        <mapping class="database.tables.OwnPolygon" />
    </session-factory>
</hibernate-configuration>
4

1 回答 1

1

我检查了一些相关问题,在这里我找到了答案的灵感。

我以这种方式改变了我的方法,现在它可以工作了

public String computeGeomFromLongitudeLatitude() {
    Session session = getSession();
    SQLQuery q =  session
            .createSQLQuery(
                    "select ST_GeometryFromText('POINT(10 10)',4326) as geom");
    q.addScalar("geom", StringType.INSTANCE); 
    String result = (String) q.list().get(0);

    closeSession();

    return result;
}
于 2013-05-24T19:01:32.437 回答