4

I can insert geometry data into database from my code, but I can only query the data using an SQL editor, like PGAdmin III. I can't retrieve geometry data back to my code. Every attempt ends up with: "Received object of type org.postgresql.util.PGobject". Even running a simple query to test if two geometry values are equal, I get this error.

2017-09-11 19:04:47,771 WARNING [javax.enterprise.resource.webcontainer.jsf.lifecycle] (default task-37) /welcomePrimefaces.xhtml @46,106 actionListener="#{controlador.testar()}": java.lang.IllegalStateException: Received object of type org.postgresql.util.PGobject: javax.el.ELException: /welcomePrimefaces.xhtml @46,106 actionListener="#{controlador.testar()}": java.lang.IllegalStateException: Received object of type org.postgresql.util.PGobject
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:111)
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:147)
    at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:814)
    at javax.faces.component.UICommand.broadcast(UICommand.java:300)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:658)
    at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
    at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
    at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
    at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
    at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
    at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
    at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
    at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
    at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
    at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at org.wildfly.extension.undertow.deployment.GlobalRequestControllerHandler.handleRequest(GlobalRequestControllerHandler.java:68)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)
    at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:81)
    at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:138)
    at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:135)
    at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
    at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
    at org.wildfly.extension.undertow.security.SecurityContextThreadSetupAction.lambda$create$0(SecurityContextThreadSetupAction.java:105)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
    at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)
    at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
    at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:104)
    at io.undertow.server.Connectors.executeRootHandler(Connectors.java:326)
    at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:812)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalStateException: Received object of type org.postgresql.util.PGobject
    at org.hibernate.spatial.dialect.postgis.PGGeometryTypeDescriptor.toGeometry(PGGeometryTypeDescriptor.java:123)
    at org.hibernate.spatial.dialect.postgis.PGGeometryTypeDescriptor$2.doExtract(PGGeometryTypeDescriptor.java:87)
    at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:235)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:231)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:222)
    at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:296)
    at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2840)
    at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1735)
    at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1661)
    at org.hibernate.loader.Loader.getRow(Loader.java:1550)
    at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:733)
    at org.hibernate.loader.Loader.processResultSet(Loader.java:978)
    at org.hibernate.loader.Loader.doQuery(Loader.java:936)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:342)
    at org.hibernate.loader.Loader.doList(Loader.java:2622)
    at org.hibernate.loader.Loader.doList(Loader.java:2605)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2434)
    at org.hibernate.loader.Loader.list(Loader.java:2429)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:501)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:371)
    at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:216)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1339)
    at org.hibernate.internal.QueryImpl.list(QueryImpl.java:87)
    at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:606)
    at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:483)
    at org.jboss.as.jpa.container.QueryNonTxInvocationDetacher.getResultList(QueryNonTxInvocationDetacher.java:58)
    at br.gov.rio.iplanrio.projetox.controle.Controlador.testar(Controlador.java:121)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at javax.el.ELUtil.invokeMethod(ELUtil.java:311)
    at javax.el.BeanELResolver.invoke(BeanELResolver.java:415)
    at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:256)
    at com.sun.el.parser.AstValue.invoke(AstValue.java:285)
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
    at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
    at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
    at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
    at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    ... 49 more

2017-09-11 19:04:47,800 SEVERE [javax.enterprise.resource.webcontainer.jsf.context] (default task-37) java.lang.IllegalStateException: Received object of type org.postgresql.util.PGobject
    at org.hibernate.spatial.dialect.postgis.PGGeometryTypeDescriptor.toGeometry(PGGeometryTypeDescriptor.java:123)
    at org.hibernate.spatial.dialect.postgis.PGGeometryTypeDescriptor$2.doExtract(PGGeometryTypeDescriptor.java:87)
    at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:235)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:231)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:222)
    at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:296)
    at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2840)
    at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1735)
    at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1661)
    at org.hibernate.loader.Loader.getRow(Loader.java:1550)
    at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:733)
    at org.hibernate.loader.Loader.processResultSet(Loader.java:978)
    at org.hibernate.loader.Loader.doQuery(Loader.java:936)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:342)
    at org.hibernate.loader.Loader.doList(Loader.java:2622)
    at org.hibernate.loader.Loader.doList(Loader.java:2605)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2434)
    at org.hibernate.loader.Loader.list(Loader.java:2429)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:501)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:371)
    at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:216)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1339)
    at org.hibernate.internal.QueryImpl.list(QueryImpl.java:87)
    at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:606)
    at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:483)
    at org.jboss.as.jpa.container.QueryNonTxInvocationDetacher.getResultList(QueryNonTxInvocationDetacher.java:58)
    at br.gov.rio.iplanrio.projetox.controle.Controlador.testar(Controlador.java:121)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at javax.el.ELUtil.invokeMethod(ELUtil.java:311)
    at javax.el.BeanELResolver.invoke(BeanELResolver.java:415)
    at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:256)
    at com.sun.el.parser.AstValue.invoke(AstValue.java:285)
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
    at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
    at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
    at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
    at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:147)
    at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:814)
    at javax.faces.component.UICommand.broadcast(UICommand.java:300)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:658)
    at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
    at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
    at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
    at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
    at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
    at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
    at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
    at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
    at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
    at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at org.wildfly.extension.undertow.deployment.GlobalRequestControllerHandler.handleRequest(GlobalRequestControllerHandler.java:68)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)
    at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:81)
    at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:138)
    at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:135)
    at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
    at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
    at org.wildfly.extension.undertow.security.SecurityContextThreadSetupAction.lambda$create$0(SecurityContextThreadSetupAction.java:105)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
    at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)
    at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
    at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:104)
    at io.undertow.server.Connectors.executeRootHandler(Connectors.java:326)
    at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:812)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

I'm using: - Hibernate 5.2 - PostgreSQL 9.6 - Postgis 2.3 - JDBC driver PostgreSQL 42.1.4 - Wildfly 11 (CR1)

Here is the model class:

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.Polygon;
import javax.persistence.Column;

@Entity
public class PontodeInteresse implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Long id;

    private String descricao;

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

    @Column(columnDefinition = "geometry(LineString,4326)")
    private LineString linha;

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

    @Column(columnDefinition = "geometry(Polygon,4326)")
    private Polygon poligono;

The POM file:

<dependencies>
    <dependency>
        <groupId>org.primefaces</groupId>
        <artifactId>primefaces</artifactId>
        <version>6.1</version>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>6.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.vividsolutions</groupId>
        <artifactId>jts</artifactId>
        <version>1.13</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-spatial</artifactId>
        <version>5.2.10.Final</version>
   </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.2.10.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.2.10.Final</version>
    </dependency>
    <dependency>
       <groupId>dom4j</groupId>
       <artifactId>dom4j</artifactId>
       <version>1.6.1</version>
       <scope>provided</scope>
    </dependency>
</dependencies>

Inside persistence.xml file, I'm using this dialect property:

<property name="hibernate.dialect" value="org.hibernate.spatial.dialect.postgis.PostgisPG95Dialect"/>

PS: I have already tried the PostgisDialect

Finally, the method with a simple test query:

public void testar() {
    try {
        GeometryFactory gf = new GeometryFactory();

        LinearRing shell = gf.createLinearRing(new Coordinate[] {
                                    new Coordinate(-43.56006145477295,-22.8861191211294), new Coordinate(-43.55607032775879,-22.8861191211294),
                                    new Coordinate(-43.55607032775879,-22.88279796969916), new Coordinate(-43.56006145477295,-22.88279796969916),
                                    new Coordinate(-43.56006145477295,-22.8861191211294)});
        LinearRing[] holes = new LinearRing[0];
        Polygon polygon = gf.createPolygon(shell, holes);
        polygon.setSRID(4326);

        Query q = entityManager.createQuery("from PontodeInteresse where equals(geometry, :geo) = true").setParameter("geo", polygon);
        List<PontodeInteresse> pontos = q.getResultList();            

    } catch(Exception e) {
        System.out.println(e.getMessage());
    }//try
}

I'm completely lost here, running out of ideas. Please help. Thanks

Maverick_Java
  • 5,665
  • 2
  • 13
  • 15
  • createQuery() expects a JPQL query. JPQL is not the same language as SQL. So, just like you can't compile Java with a C compiler, you can't execute a SQL query with createQuery(). – JB Nizet Sep 11 '17 at 21:44
  • @JBNizet, could you tell me please what is wrong with my JPQL ? – Maverick_Java Sep 11 '17 at 21:49
  • Sorry. I read too fast and missed the fact that you were using the PostGis dialect. My apologies. Instead of just posting the message of the exception, you should post its complete and exact stack trace though. – JB Nizet Sep 11 '17 at 21:54
  • Ok @JBNizet. Good advice. I've just updated the question to provide the stack trace. – Maverick_Java Sep 11 '17 at 22:06
  • 1
    The `org.postgresql:postgresql` and `org.hibernate:*` dependencies should be marked as `provided`. It could be seeing a type from two different class loaders. – James R. Perkins Sep 11 '17 at 22:30
  • @JamesR.Perkins, as I marked the dependencies, I got the error: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] Caused by: org.hibernate.boot.registry.selector.spi.StrategySelectionException: Unable to resolve name org.hibernate.spatial.dialect.postgis.PostgisDialect] as strategy [org.hibernate.dialect.Dialect]"}} – Maverick_Java Sep 12 '17 at 00:41
  • The stacktrace strongly suggests that @JamesR.Perkins is on the right track. It's a classloader issue since the code is designed to actually expect a PGobject, and this exception is thrown only when an instance of an unexpected class is received. Maybe this can help: https://stackoverflow.com/questions/35504016/spatial-hibernate-5-on-wildfly-10 – Karel Maesen Sep 12 '17 at 15:47
  • Hi @KarelMaesen. I tried to run the sample project provided by Toastor, posted in the link you suggested but I got the same error. I can deploy the project and insert geometry data from the interface provided. But I can't get the geometry data back from Database. – Maverick_Java Sep 13 '17 at 18:46
  • @Maverick_Java you need to add postgis, hibernate-spatial, etc to your server and mark all dependencies as `provided`, check the code here: https://github.com/hibernate/hibernate-orm/blob/61932bbeb778d77b1673810191b238ab8b94b4e3/hibernate-spatial/src/main/java/org/hibernate/spatial/dialect/postgis/PGGeometryTypeDescriptor.java#L109 and see https://www.volpe.com.py/hibernate/2016/06/17/wildfly-10-hibernate-spatial.html to see how to add the lib to wildfly – Arturo Volpe Sep 14 '17 at 02:09
  • @ArturoVolpe. Thank you very much ! Please move your comments to an answer so I can give you +100 reputation offered for the solution. Life saver ! – Maverick_Java Sep 14 '17 at 04:11

2 Answers2

3

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.

Arturo Volpe
  • 3,442
  • 3
  • 25
  • 40
  • Thanks again Arturo. I'll leave here some details about what I did: – Maverick_Java Sep 14 '17 at 15:41
  • Thanks again Arturo. I'll leave here some details about what I did: I'm using Wildfly 11 CR1, which comes with Hibernate 5.1.10.Final. So, in order to figure out what dependencies should be downloaded, I used "mvn dependency:tree". I saw that in my case I had to download geolatte-geom-1.0.6.jar, hibernate-spatial:5.1.10.Final, jts-1.13.jar and postgis-jdbc-2.2.1.jar. Then I kept following your instructions. – Maverick_Java Sep 14 '17 at 15:57
  • @Maverick_Java awesome!, you added the postgis jdbc directly in the module or in another module? – Arturo Volpe Sep 14 '17 at 17:28
  • I added it into the same module. I'll post a complete example as soon as I get home. – Maverick_Java Sep 14 '17 at 17:33
  • @Maverick_Java Could you post the output of the "mvn dependency:tree" because since version 5.0 Hibernate Spatial no longer needs the postgis-jdbc jar. So I'm surprised it still shows up. – Karel Maesen Sep 14 '17 at 20:12
  • Good point @KarelMaesen. I did the "mvn" in order to get geolatte and JTS versions. – Maverick_Java Sep 15 '17 at 18:07
  • I had the same issue with tomcat -- version of postgres lib in the tomcat/lib folder was not the same as the "provided" version in my application's pom file. I was using the older postgres groupId instead of the newer org.postgresql groupId – Jaymes Bearden Apr 08 '19 at 20:57
0

This does not only apply to Wildfly: I was able to fix the same issue on Tomcat by moving the PostgreSQL jar from [tomcat]\webapps[WebApp]\WEB-INF\lib to [tomcat]\lib.

btw: In my case, the problem only appeared on the customer's system; our development server was OK - there was no noticeable difference between both configurations.

Yaser Darzi
  • 1,480
  • 12
  • 24
larissus
  • 1
  • 1