3

When I try to persist Point datatype to postgres DB it is failing with error org.postgresql.util.PSQLException: ERROR: column "pointColumn" is of type point but expression is of type geometry

Here is my pojo snappit

@Type(type="org.hibernate.spatial.GeometryType")
@Column(columnDefinition="Point", nullable = true)
private Point pointColumn;

Here is my driver and dialect properties

driverClass=org.postgresql.Driver

dialect=org.hibernate.spatial.dialect.postgis.PostgisDialect

Here is my spatial version details

<dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-spatial</artifactId>
   <version>4.3</version>
</dependency>
<dependency>
   <groupId>com.vividsolutions</groupId>
   <artifactId>jts</artifactId>
   <version>1.13</version>
</dependency>
        

Even I have registered GeometryType to jdbc Configuration as below

GeometryType geometryType = new GeometryType();
configuration.registerTypeOverride(geometryType);

Even this issue is true for Polygon,LineString,Point,MultiPoint,MultiPolygon...

Am I missing any configuration or it is expected behaviour from Geometry Type implementation ?

Community
  • 1
  • 1
Sunil Kumar
  • 5,477
  • 4
  • 31
  • 38

2 Answers2

2

PosgreSQL has some in-built geometry support. You're trying to use the type point from the base pg, you need to change that to geometry as it is a geometry representing a point in PostGIS.

Jakub Kania
  • 15,665
  • 2
  • 37
  • 47
  • Jakub,I have changed my java type to Geometry instead of Point and My DB type is still remaining as Point.But still when I persist, I got same error. – Sunil Kumar Sep 11 '15 at 07:52
  • I dont understand when to use Postgis and Postgres dialect.I want to use Point,Polygon,LineSegment types.As Spatial provided GemotryType then I used Postgis dileact , which has GemotryType definitions. – Sunil Kumar Sep 11 '15 at 07:55
  • @SunilKumar Have you tried changing the db column to `geometry`? – Jakub Kania Sep 11 '15 at 08:22
  • After changing DB type to geometry it is working.I dont understand What is the use of other types like Point,Polygon,LineString.....Can you clarify,Gemotry type is Postgis type right ? And Point is of base postgres type ? – Sunil Kumar Sep 11 '15 at 11:14
  • Also What if DB is not accessible to me and the types are Point,Polygon...And How to work with such types in reverse engineering process. – Sunil Kumar Sep 11 '15 at 11:15
  • @SunilKumar `Geometry` and `Point` are different types. `Geometry` is only in PostGIS and that's what you should use. Please ask a new question for the rest of your questions and mark this one as accepted if the problem with storing was solved. I don't work with Java so I don't even know that much. – Jakub Kania Sep 11 '15 at 11:34
0

The right geometry classes to use are inside of com.vividsolutions.jts.geom package. H-S maps JTS geometries to DB native structure, so, don't use posgis classes. This is due to make applications DB independent (one of the main points of JPA API).

See H-S overview:

Hibernate Spatial uses the Java Topology Suite (JTS) as its geometry model. JTS is an implementation of the OpenGIS Simple Features Implementation Specification for SQLv. 1.1 (SFS). This specification is implemented in most RDBMS with spatial data support. It is also a direct precursor to a precursor to SQL/MM Part 3: Spatial (ISO/IEC 13249-3).

This is a gvNIX sample application (on-line demo) which handles geometries types. This is a JPA entity which declares 3 geometry properties:

...
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.Polygon;
...
...
public class Owner extends AbstractPerson {

    @Type(type = "org.hibernate.spatial.GeometryType")
    private Point location;

    @Type(type = "org.hibernate.spatial.GeometryType")
    private LineString distance;

    @Type(type = "org.hibernate.spatial.GeometryType")
    private Polygon area;
}

Good luck!!

jmvivo
  • 2,653
  • 1
  • 16
  • 20
  • 1
    @ Jmvivo : Even I have created same class structure and My Database have Point as column type.And When I try to persist it in DB through hibernate it is failing to convert( exception in description).What is your DB type ?,Is it working while persisting into DB ? – Sunil Kumar Sep 14 '15 at 13:38
  • As you can see in on-line demo, persistence works as expected. If you follow the instructions in project main page (_Run from Git_ section), you can get it working, so you can compare it with your project. – jmvivo Sep 14 '15 at 13:47