5

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

3 Answers 3

8

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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>

3 Comments

Thanks @Parth, but there is no org.hibernate.spatial.GeometryType class in hibernate-spatial 5.x
Did you add proper dialect in persistence.xml file. <property name="hibernate.dialect" value="org.hibernatespatial.postgis.PostgisDialect"/>
It can be type="org.hibernatespatial.GeometryUserType" also in it.
0

I downgraded to hibernate-spatial 4.3 version. Please see my configuration below.

Entity column:

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

The Polygon class can be found in com.vividsolutions.jts.geom package.

Dependencies:

<dependencies>
     <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-spatial</artifactId>
        <version>4.3</version>
    </dependency>
    <dependency>
        <groupId>org.postgis</groupId>
        <artifactId>postgis-jdbc</artifactId>
        <version>1.3.3</version>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.4.1208.jre7</version>
    </dependency>
</dependencies>

Repositories:

<repositories>
    <repository>
        <id>OSGEO GeoTools repo</id>
        <url>http://download.osgeo.org/webdav/geotools</url>
    </repository>
    <repository>
        <id>Hibernate Spatial repo</id>
        <url>http://www.hibernatespatial.org/repository</url>
    </repository>
</repositories>

Hibernate dialect:

org.hibernate.spatial.dialect.postgis.PostgisDialect

2 Comments

Interesting, I found out that hibernate-spartial 5.x actually knows how to handle JTS types natively so no type annotation required. I'll post my solution too.
I really did not want to downgrade because the application server within which I am running supplies hibernate 5.x and I would rather not fiddle with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.