简体   繁体   中英

How can I get a String from this Geometry Object with hibernate and PostGIS

I would like to return the output (Output is "0101000020E610000000000000000024400000000000002440" in geometry type) of this SQL Postgis query

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

in Java with hibernate.

I tried it with the following method:

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

    return geom;
}

This doesn't work for me. I get the following exception

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)

I think I understand what the problem is. I try to parse the result as a String but it's a Geometry object. But what can I do to get this output as String in Java? I already get an exception if I only add .list() behind the SQL query.

Thanks a lot for your ideas.

EDIT:

I have the correct Dialog specified in my 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>

I checked some of the related questions and here I found the inspiration to my answer.

I changed my method this way and now it works

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM