Search code examples
hibernategeometryhqlpostgisspatial

hibernate spatial 5 - Postgis2.2 : Query issues


Just started with postgis and hibernate statial and I'm gettin some issues making queries.

Goal : Get a museum from a (its) geometry type object

In my DB I got this column:

name: geom  
type: geometry(Point,4326))
that contains something like: 0101000020E6100000004C8E1516D(...) 

for each museum

Then, I've a museum class with:

@Column(name = "geom", columnDefinition = "geometry(Point,4326)")
private Geometry           geometry;

And here's my query:

WKTReader fromText = new WKTReader();
        try {
            //LON and LAT are the museum's coordinates
            Geometry geom = fromText.read("POINT("+lon+" "+lat+")");
            Session hibernateSession = getCurrentSession();

            Museum result = hibernateSession
                    .createQuery("from Museum where geometry = :geometry")
                    .setParameter("geometry", geom).uniqueResult();
            return result;


        } catch (ParseException e) {
            (...)
        }

But when I try to execute it, I got this Error:

ERROR: operator is not unique: geometry = bytea
Indice : Could not choose a best candidate operator. You might need to add explicit type casts.

So I was thinking, maybe Geometry from hibernate and Geometry from Postgis aren't the same? Any idea on how I could make it work?

Thanks!


Solution

  • I found the problem.

    First I'd to make sure I'd the postgis dialect on my .properties file.

    Added this Setter to have same SRID

    geom.setSRID(4326);
    

    Then I changed my query to:

    .createQuery("from Museum where equals(geometry, :geometry) = true")
    

    And also changed my db class to:

    @Column(name = "geom", columnDefinition = "Geometry")
    

    Works perfectly now. This might help someone with the same problem...

    Have fun