0

I want to json data save to database but first I need to parse JSON data. I did vector type parser but I could not coordinate.

Example : geoJson

{"coordinates":[-48.287108838558,-15.679686963558],"type":"Point"}

I saved to vector type. my Geojson parse code :

public class GeoJSON{

 private String type;
private String data;
public String getType() {return type;}
public void setType(String type) {this.type = type;}

public String getData() {return data;}
public void setData(String data) { this.data = data; }

 public GeoJSON() {
}

public GeoJSON(JSONObject json) {
    parse(json);
}

public GeoJSON parse(JSONObject json) {
    StringWriter out = new StringWriter();
    json.write(out);
    this.data = out.toString();
    this.type = json.getString("type");

    try {
        out.close();
    } catch (IOException ex) {
        System.out.println(ex.getMessage());
        throw new RuntimeException(ex);
    }

    return this;
}

After Mycontroller in insert method :

public boolean insert(GeoJSON item)
{

SavegeojsonEntity theEvent = new SavegeojsonEntity();

    boolean success;
    try {
        String vectorType = item.getType();
        EntityManager em = HibernateSpatialJPA.createEntityManager();
        em.getTransaction().begin();
        theEvent.setVectorType(vectorType);

        if(vectorType.equals("Point"))
        {
            Geometry geom = wktToGeometry(item.getData());
            System.out.println("geomPo "+geom);
            theEvent.setGeom((Point)geom);
        }
        em.persist(theEvent);
        em.getTransaction().commit();
        em.close();
        success=true;
        HibernateSpatialJPA.close();
    }
    catch (Exception ex)
    {
        ex.printStackTrace();
        success = false;
    }
    return success;
}
 private Geometry wktToGeometry(String wktPoint) {
    WKTReader fromText = new WKTReader();
    Geometry geom = null;
    try {
        geom = fromText.read(wktPoint);
    } catch (com.vividsolutions.jts.io.ParseException e) {
        throw new RuntimeException("Not a WKT string:" + wktPoint);
    }
    return geom;
}

My SavegeojsonEntity Class :

Point (com.vividsolutions.jts.geom.*;)

 private Point geom;
@Basic
@Column(columnDefinition="Geometry",name = "geom", nullable = true, insertable = true, updatable = true)
@Type(type="org.hibernate.spatial.GeometryType")
public Point getGeom() {return geom;}
public void setGeom(Point geom) {this.geom = geom;}`

the method inserts

System.out.println("geomPo "+geom); geomPo = null

because

item.getData() value: "coordinates":[-48.287108838558,-15.679686963558],"type":"Point"

I want to item.getData() value : -48.287108838558,-15.679686963558

This is my stracktrace :

javax.persistence.RollbackException: Error while committing the transaction
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:92)
at com.springapp.model.SavegeojsonManager.insert(SavegeojsonManager.java:55)
at com.springapp.model.SavegeojsonManager.insert(SavegeojsonManager.java:26)
at com.springapp.mvc.HSpatialController.saveGeoJson(HSpatialController.java:46)
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:497)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:781)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:721)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2430)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2419)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.UnsupportedOperationException
at org.hibernate.spatial.GeometrySqlTypeDescriptor.getBinder(GeometrySqlTypeDescriptor.java:52)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:305)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:300)
at org.hibernate.type.AbstractSingleColumnStandardBasicType.nullSafeSet(AbstractSingleColumnStandardBasicType.java:57)
at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2705)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2959)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3403)
at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:88)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:354)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:275)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:326)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1214)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:403)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:75)
... 40 more

Please. Thanks in advance.

Rahul Gopi
  • 414
  • 1
  • 16
  • 31
develop
  • 131
  • 6
  • 16
  • Please specify versions. looks like duplicate [http://stackoverflow.com/questions/12236428/hibernate-persist-failure-with-postgis-geometry] – sibnick Aug 15 '15 at 17:46
  • Postgresql 9.4 , hibernate 4.1.6. I deleted wktGeometry method and cascade. I wrote instead of `JSONObject jsonRootObject = new JSONObject(item.getData()); JSONArray jsonArray = jsonRootObject.getJSONArray("coordinates");` and code output : [33.451172411442,37.889648973942] . I want to save postgresql column but how ? @sibnick – develop Aug 15 '15 at 18:23

1 Answers1

2

UPDATED

Alright so after some investigation trough remote desktop we finally did it.

Conclusion was that there were wrong annotation on SavegeojsonEntity.

  1. Annotations works on fields not on get method
  2. There was wrong dialect: instead of:

<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL82Dialect"/>

the most appopriate one is

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

And ofcourse this also has to be done to made proper Point.

There is something wrong in your GeoJSON

    public class GeoJSON{

    private String type;
    private String data;
    private String pointRepresentation;
    public String getType() {return type;}
    public void setType(String type) {this.type = type;}
    public String getPointRepresentation(){return this.pointRepresentation;}

    public String getData() {return data;}
    public void setData(String data) { this.data = data; }

     public GeoJSON() {
    }

    public GeoJSON(JSONObject json) {
        parse(json);
    }

    public GeoJSON parse(JSONObject json) {
        JSONArray jsonArray = json.getJSONArray("coordinates");
        this.type = json.getString("type");
        data = "";
        for (int i = 0; i < jsonArray.length(); i++) {
            data += jsonArray.get(i) + ",";
        }
        if (data != null && !data.isEmpty()) {
            data = data.substring(0, data.length() - 1);
            pointRepresentation = type + "(" + data + ")";
        }
        return this;
    }
}

and here put pointRepresentation:

if(vectorType.equals("Point"))
        {
            Geometry geom = wktToGeometry(item.getPointRepresentation());
            System.out.println("geomPo "+geom);
            theEvent.setGeom((Point)geom);
        }

and check the results.

Paweł Głowacz
  • 2,926
  • 3
  • 18
  • 25
  • I did and result `JSONArray[0] not a string.` and I cant see `System.out.println("geomPo "+geom);` @PawelGlowacz – develop Aug 15 '15 at 19:42
  • I did like you said @PawelGlowacz – develop Aug 15 '15 at 19:48
  • Any improvements ? Now you should have this `Point(-48.287108838558,-15.679686963558)` when you pass to `wktToGeometry` method. And then it should parse as `Point` object. Any new errors ? – Paweł Głowacz Aug 15 '15 at 19:50
  • I certainly do as you say. GeoJson class in parse method for I wrote `System.out.println("dataa"+pointRepresentation);` to check but I can see code output and I still same error . This is error : `JSONArray[0] not a string.` @PawelGlowacz – develop Aug 15 '15 at 20:03
  • Do you have another idea for a solution? @PawelGlowacz – develop Aug 15 '15 at 20:13
  • Please enter to http://chat.stackoverflow.com/rooms/87068/helpingwithgeo and we can talk – Paweł Głowacz Aug 15 '15 at 20:17
  • `` the change.Thank you for advice @PawelGlowacz – develop Aug 15 '15 at 22:43