保存到json postgres:java.lang.UnsupportedOperationException

时间:2015-08-15 12:05:51

标签: json spring hibernate postgresql coordinates

我想将json数据保存到数据库,但首先我需要解析JSON数据。我做了矢量类型解析器但我无法协调。

示例:geoJson

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

我保存到矢量类型。 我的Geojson解析代码:

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;
}

在插入方法中的Mycontroller之后:

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;
}

我的SavegeojsonEntity课程:

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;}`

方法插入

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

因为

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

我想item.getData() value : -48.287108838558,-15.679686963558

这是我的直言不讳:

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

请。 提前谢谢。

1 个答案:

答案 0 :(得分:2)

<强>已更新

好吧,经过对远程桌面的一些调查,我们终于做到了。

结论是SavegeojsonEntity上有错误的注释。

  1. 注释适用于不在get方法
  2. 上的字段
  3. 方言错了:而不是:
  4. <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL82Dialect"/>

    最合适的是

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

    当然,还必须做出正确的Point

    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;
        }
    }
    

    这里放点pointRepresentation:

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

    并检查结果。