Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly map a polygon type with postgis and hibernate-spatial?

Assume I have the following table

CREATE TABLE foo (
  id BIGSERIAL PRIMARY KEY, 
  polygon GEOMETRY(POLYGON)
);

and entity class

@Table
@Entity
public class Foo {

   @Id
   @GeneratedValue(strategy = IDENTITY)
   private Long id;

   private Polygon polygon;

}

I managed to save a Foo entity, however, I can't select it them from the database. I get this exception:

java.lang.NumberFormatException: For input string: "PO"

Then, I added the following annotation on top of polygon field:

@Type(type = "org.hibernate.spatial.JTSGeometryType")

but it throws another exception saying that this type cannot be instantiated:

org.hibernate.MappingException: Could not instantiate Type: org.hibernate.spatial.JTSGeometryType

Please note that I use 5.1.0.Final version for hibernate and hibernate-spatial.

Thank you

like image 398
Bravo Avatar asked Jun 03 '16 11:06

Bravo


2 Answers

It seems hibernate-spartial 5.x knows how to handle JTS geometrical types natively so no type annotation required. Here is my working configuration.

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-spatial</artifactId>
  <version>5.2.3.Final</version>
</dependency>

MySQL table...

CREATE TABLE `stuff` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `coordinates` point NOT NULL,
  PRIMARY KEY (`id`),
  SPATIAL KEY `coordinates` (`coordinates`)
)

JPA entity...

import com.vividsolutions.jts.geom.Point;
...

@Basic(optional = false)
@NotNull
@Column(nullable = false, columnDefinition = "point")
private Point coordinates;

Hibernate dialect...

<property name="hibernate.dialect" value="org.hibernate.spatial.dialect.mysql.MySQLSpatialDialect"/>

That's all but please note that my application runs within WildFly 10 which supplies additional runtime dependencies like the MySQL driver.

like image 115
Sayo Oladeji Avatar answered Nov 20 '22 14:11

Sayo Oladeji


You should try giving column name also

@Entity<br/>
@Table(name = "table_name")<br/>
public class Foo {<br/>

@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;

@Type(type = "org.hibernate.spatial.GeometryType")
@Column(name = "the_geom", nullable = true,columnDefinition="Geometry")
 private Geometry geom;

@Type(type = "org.hibernate.spatial.GeometryType",columnDefinition="Geometry")
private Polygon polygon;
}

you should also be aware that as of Hibernate Spatial 4.0-M1, only the Geometry type is specified to Hibernate, and hence the @Column annotation must set columnDefinition="Geometry", and not Point or anything else. This may be fixed in the future.

With this anthology of modifications, I can finally write a Point to a database! The correct property specification is:

 @Column(columnDefinition="Geometry")
 @Type(type = "org.hibernate.spatial.GeometryType")
 private Point centerPoint;

Also check with dialect in hibernate.cfg.xml

Add following line to hibernate.cfg.xml

<property name="dialect">org.hibernate.spatial.dialect.postgis.PostgisDialect</property>
like image 30
prem30488 Avatar answered Nov 20 '22 13:11

prem30488