30

After upgrading Hibernate-spatial to Version 5.0.0.CR2 the following declaration doesn't work anymore:

@Column(columnDefinition = "geometry(Point,4326)")
@Type(type = "org.hibernate.spatial.GeometryType")
private Point position;

with an:

org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [org.hibernate.spatial.GeometryType]

As I can see the class doesn't exist in the Jar-File anymore. What happend to the GeometryType and how is it replaced? Or is there another jar-file to include?

Edit: For clarification. I am using Hibernate-Spatial in Combination with a PostgreSQL-Postgis database.

7 Answers 7

41

Well the solution is too easy to see. Simply delete the @Type annotation, so the declaration looks like this:

@Column(columnDefinition = "geometry(Point,4326)")
private Point position;

Source:

Note the @Type annotation. This informs Hibernate that the location attribute is of type Geometry. The @Type annotation is Hibernate specific, and the only non-JPA annotation that is required. (In future versions of Hibernate (version 5 and later) it will no longer be necessary to explicitly declare the Type of Geometry-valued attributes.)

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

2 Comments

Hibernate documentation and every other post did not work for hibernate 5, but your answer did! Thanks!
I also had to use the correct GeometryFactory to creat the Point object. Using new GeometryFactory(new PrecisionModel(), 4326) worked for me.
9

The solution suggested by Denis above did not work for me on Hiberate 5 spatial and Mysql. However the following annotations worked for me

  @Column(name = "location",columnDefinition="Geometry")
  private Geometry location;


  @Column(name = "pointlocation",columnDefinition="Point")
  private Point pointlocation;

If you are running Hibernate 5.3+ you can skip out on column definitions

  @Column(name = "location")
  private Geometry location;


  @Column(name = "pointlocation")
  private Point pointlocation;

1 Comment

this one, helps me
5

For Hibernate Spatial 5.2.x, all you need is the below in your entity.

private Point location;

You don't need columnDefinition or Type like the above solutions mentioned.

Some additional details and checks to see if the above works

  • As soon as you run your setup and use desc table_namein mysql, you should see the field type of geometry. This indicates that your hibernate to database mapping is working fine.
  • Now try to create an entity and repo.save(entity) from java and your entity should save fine without any errors.

If the above setup didn't work well, you will typically get the error

Data truncation: Cannot get geometry object from data you send to the GEOMETRY field Blockquote

Hope that helps someone save a 6 hours that I wasted !

1 Comment

Perfect, exactly what I was looking for. The other answers above wouldn't work with Hibernate 5.3, as soon as I removed the columnDefinition it worked perfectly.
4

So I've been battling this problem for a few days, the thing is that I wanted the database type to be from the postgis type in order for me to run efficient distance queries for nearest neighbour etc. I finally figured out how to do it and wanted to share it with the community so I created a demo project containing the solution to the problem.

My setup is a PostgreSQL database with Postgis, Spring boot jpa, Hibernate 5+, Hibernate-spatial 5+ and also added converting to rest output, which again required a special module from jackson.

the project is found at https://github.com/Wisienkas/springJpaGeo

the important code you was asking for was this:

@type(type = "jts_geometry")
private Point point;

2 Comments

What is the type "jts_geometry"? I thought this value was supposed to be a fully qualified class name?
2

Apparently your Hibernate libraries need to be at the same version too.

I was using postgis / springboot / hibernate-spatial.

Initially Hibernate-Spatial was @ 5.4.10.Final, and didn't work even with the solutions here.

I then looked at what version of hibernate was in my maven dependencies ( hibernate-commons-annotations-5.1.0.Final.jar) and remembered seeing somewhere that the versions of hibernate need to match.

So I downgraded my Hibernate-Spatial, to 5.1 and the following mapping statement works without any more info:

// Geometry Object is from the following import
import com.vividsolutions.jts.geom.*;

@Column(name="domain_loc")
private Geometry location;

3 Comments

You saved me! I just done some spring-boot upgrade and it came with hibernate upgrade. So i just checked those dependencies and downgraded the hibernate-spatial to 5.1.0.Final. It works now!
Glad it also helped you!!
I tried this and it did not work. I have two versions of commos-annotations. Do you think this is causing this?
1

For me, the data was being stored as expected in PostgreSQL, but retrieving it using a REST API was failing with Deserialization error. This is what worked for me with PostgreSQL 12 & Spring Boot.

Included this in the application.properties file

spring.jpa.database-platform=org.hibernate.spatial.dialect.postgis.PostgisDialect

pom.xml changes

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.4.20.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-spatial</artifactId>
    <version>5.4.20.Final</version>
</dependency>
<dependency>
    <groupId>org.n52.jackson</groupId>
    <artifactId>jackson-datatype-jts</artifactId>
    <version>1.2.9</version>
</dependency>

Create the below Bean with @Configuration annnotation

import org.n52.jackson.datatype.jts.JtsModule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class BeanConfig {

    @Bean
    public JtsModule jtsModule() {
        return new JtsModule();
    }

}

And in your @Entity class, make sure

import org.n52.jackson.datatype.jts.GeometryDeserializer;
import org.n52.jackson.datatype.jts.GeometrySerializer;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.locationtech.jts.geom.Point;
    
@Column(name="the_geom", columnDefinition = "POINT")
@JsonSerialize(using = GeometrySerializer.class)
@JsonDeserialize(contentUsing = GeometryDeserializer.class)
private Point point;

After these changes, my Rest API returns data in the expected format

"point": {
            "type": "Point",
            "coordinates": [
                53.85151,
                -1.55973
            ]
        }

Comments

0

I am using hibernate 4.3.6 and hibernate spatial 5.2.2. The solution suggested above didn't work for me. However the below code worked If I add hibernate spatial 4.3

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

Comments

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.