如何编写休眠空间更新查询?

How to write hibernate spatial update query?

我的目标是更新地图上绘制的矢量。

我的更新方法:

public boolean update(int id,JSONObject json)
 {
   boolean success;
     try{
         EntityManager em = HibernateSpatialJPA.createEntityManager();
         em.getTransaction().begin();
         Query query = em.createQuery("update SavegeojsonEntity s set s=:json where id=:id");
         query.setParameter("id",id);
         query.setParameter("json",json);
         int result = query.executeUpdate();
         em.getTransaction().commit();
         if( result> 0){
                 crudProcess se = new crudProcess();
                 se.insert(json);
         }
         em.close();
         success=true;
     }catch (Exception ex){
         ex.printStackTrace();
         success=false;
     }return success;
 }

这是 crudProcess class。所以我根据添加的矢量类型制作。

//(Item) geojson in the parse(json) method returns
//item in data and type information 
 public boolean insert(JSONObject json) {
    GeoJSON item = new GeoJSON(json);
    return insert(item);
}
public boolean insert(GeoJSON item) {

    SavegeojsonEntity theEvent = new SavegeojsonEntity();
    EntityManager em = HibernateSpatialJPA.createEntityManager();
    em.getTransaction().begin();
    boolean success;
    try {
        String vectorType = item.getType();
        if (vectorType.equalsIgnoreCase("Point")) {
            Point point = new GeometryJSON().readPoint(item.getData());
            point.setSRID(3857);
            theEvent.setGeom(point);
            theEvent.setVectorType(vectorType);
        }
        else if (vectorType.equalsIgnoreCase("LineString")) {
            LineString lineString = new GeometryJSON().readLine(item.getData());
            lineString.setSRID(3857);
            theEvent.setGeom1(lineString);
            theEvent.setVectorType(vectorType);
        }
        else if (vectorType.equalsIgnoreCase("Polygon")) {
            Polygon polygon = new GeometryJSON().readPolygon(item.getData());
            polygon.setSRID(3857);
            theEvent.setGeom3(polygon);
            theEvent.setVectorType(vectorType);

        }
        else if (vectorType.equalsIgnoreCase("MultiPolygon")) {
            MultiPolygon multiPolygon = new GeometryJSON().readMultiPolygon(item.getData());
            multiPolygon.setSRID(3857);
            theEvent.setGeom2(multiPolygon);
            theEvent.setVectorType(vectorType);
        }
        em.persist(theEvent);
        em.getTransaction().commit();
        em.close();
        success = true;
        //HibernateSpatialJPA.close();
    } catch (Exception ex) {
        ex.printStackTrace();
        success = false;
    }
    return success;

}

保存geojsonEntity数据库信息:

    @Id
@GeneratedValue(generator="increment")
@GenericGenerator(name="increment", strategy = "increment")
@Column(name = "id", nullable = false, insertable = true, updatable = true)
private int id;
@Column(columnDefinition="Geometry", nullable = true)
@Type(type = "org.hibernate.spatial.GeometryType")
private Point geom;
bla bla ..

and getter setter method ..

public int getId() {return id;}
public void setId(int id) {this.id = id;}

public Point getGeom() {return geom;}
public void setGeom(Point geom) {this.geom = geom;}

etc.

GeoJSON class 是向量 getter 和 setter 特征(我发送一个向量类型和坐标):

 private int id;
private String type;
private String data;

public int getId() { return id; }
public void setId(int id) {  this.id = id; }

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) {

    this.type = json.getString("type");
    json.remove("type");
    this.data = json.toString();
    return this;
}

这是控制器:

 @RequestMapping(value = "/updateGeoJSON.html", method = RequestMethod.POST)
@ResponseBody
public GenericResponse updateGeoJson(final HttpServletRequest request,@RequestParam(value="id",required = false) final Integer id) {
    String geoJsonString = RequestUtil.getBody(request);
    int failedCount = 0, successCount = 0;
    JSONArray datas = new JSONObject(geoJsonString).getJSONArray("datas");
    try {
        for (int i = 0; i < datas.length(); i++) {
            JSONObject geoJson = datas.getJSONObject(i);
            System.out.println("geoJson output");
            System.out.println(geoJson);
            System.out.print("id:");  // id return null (why? )
            if (add.update(id,geoJson)) {
                successCount++;
            } else {
                failedCount++;
            }
        }
    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    }
    GenericResponse response = new GenericResponse("Failed : " + failedCount + ",Success : " + successCount);
    response.setMessage("successCount Bom Bom");
    response.setError("Failed : " + failedCount);
    return response;
}

我可以看到 geoJson 输出,但最终写入 null。所以我没有更新。 例如:

geoJson output:
{"coordinates":[[20.20690552540442,32.682341460350166],[19.536613076278492,9.54631569375309],[13.406050397745297,22.300641685140324]],"type":"LineString"}
null

有什么帮助吗? 谢谢。

您需要做的是从该请求中获取 ID,并且您似乎是从 RequestUtil.getBody()

进入的
public GenericResponse updateGeoJson(final HttpServletRequest request) {
    String geoJsonString = RequestUtil.getBody(request);
    int failedCount = 0, successCount = 0;
        int id = new JSONObject(geoJsonString).getInt("id");
        JSONArray datas = new JSONObject(geoJsonString).getJSONArray("datas");