develop
develop

Reputation: 141

Save to json postgres : java.lang.UnsupportedOperationException

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.

Upvotes: 0

Views: 1132

Answers (1)

Paweł Głowacz
Paweł Głowacz

Reputation: 3046

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.

Upvotes: 2

Related Questions