crud - How to write hibernate spatial update query? -
my goal update drawn vector on map.
my update method :
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 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; }
and crudprocess class. made according type of vector added.
//(item) geojson in parse(json) method returns //item in data , 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; }
savegeojsonentity database information :
@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 .. , 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 vectors getter , setter features ( send vectortype , coordinates ) :
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; }
and controller :
@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 { (int = 0; < 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; }
i can see geojson output writes null
. did not update. example :
geojson output: {"coordinates":[[20.20690552540442,32.682341460350166],[19.536613076278492,9.54631569375309],[13.406050397745297,22.300641685140324]],"type":"linestring"} null
any ? thank you.
what need id request , seems getting in 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");
Comments
Post a Comment