Reputation: 3164
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.
Upvotes: 30
Views: 31847
Reputation: 113
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
]
}
Upvotes: 1
Reputation: 7395
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;
Upvotes: 9
Reputation: 166
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;
Upvotes: 2
Reputation: 176
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;
Upvotes: 0
Reputation: 3503
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
desc table_name
in mysql, you should see the field type of geometry. This indicates that your hibernate to database mapping is working fine.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 !
Upvotes: 5
Reputation: 1751
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;
Upvotes: 4
Reputation: 3164
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;
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.)
Upvotes: 41