gabriel119435
gabriel119435

Reputation: 6802

Retrieving data from PostGis using Hibernate Spatial

I'm already set up with Wildfly 10, Hibernate Spatial 5.0.1 and PostGis. I can insert data into database sucessufully (checked over psql command line) but when I try to read from database, I get the following error:

Caused by: java.lang.IllegalStateException: Received object of type byte[]

Here's my entity:

import com.vividsolutions.jts.geom.Point;

 import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.Id;

@Entity
public class Location {

 /*Empty constructor used on Hibernate*/
 public Location() {
 }

 public Location(String name, Point point) {
    this.name = name;
    this.location = point;
 }

 @Id
 private String name;

 @Column(nullable = false)
 private Point location;
}

and my method:

public Location findByName(final String name) {
    Query query = entityManager.createQuery("select l from Location l where l.name=:name", Location.class);
    query.setParameter("name", name);
    List<Location> result = query.getResultList();
    if (result != null && result.size() > 0)
        return result.get(0);
    return null;
}

Remembering, I can correctly insert data into it, but I can't read. First I thought about some inconsistency on Hibernate Spatial's notations, some broken dependency on pom.xml or something wrong on persistence.xml but since I can write I don't think this is the case. Here on method find he queries exactly as I did. What I'm doing wrong?

Upvotes: 0

Views: 560

Answers (1)

Arturo Volpe
Arturo Volpe

Reputation: 3627

Your issue is the same as this question, basically you need to install postgis and all dependencies in the server (jboss/wildfly) and set your deps as provided.

Here is the body of the answer:

You need to add Postgis, hibernate-spatial and the other related libs to your Wildfly before deploying your application, this is because the hibernate that you are using is the bundled with the wildfly but the other libraries are bundled with your war, so they are using different classloaders.

If you check this code you can see that the cast is correct, and the error message make no sense, this is tipically a issue with classloaders.

To fix this issue you need to add all libs to your wildfly, to do this you can do:

cd $JBOSS_PATH/modules/system/layers/base/org/hibernate/main
mvn dependency:copy -Dartifact=org.hibernate:hibernate-spatial:5.0.7.Final:jar -DoutputDirectory=.
mvn dependency:copy -Dartifact=org.geolatte:geolatte-geom:1.0.1:jar -DoutputDirectory=.
mvn dependency:copy -Dartifact=com.vividsolutions:jts:1.13:jar -DoutputDirectory=.

Edit the module.xml file to add your deps:

<resource-root path="hibernate-spatial-5.0.7.Final.jar"/>
<resource-root path="jts-1.13.jar"/>
<resource-root path="geolatte-geom-1.0.1.jar"/>

And in the dependencies tag, add:

<module name="org.slf4j"/>

Also if you are using postgresql, you need to add in the dependencies tag:

<module name="org.postgresql"/>

Use the correct version in the mvn command to download the correct jar.

Upvotes: 2

Related Questions