postgresql - Parse string as json array from Postgre -
i have table in postgresql 2 columns - id , coord. column "coord" - geo coordinates stored string in json format.
example:
[{"lat":49.09693425316379,"lng":33.61747393628419},{"lat":49.11835977646441,"lng":33.638456496907},{"lat":49.12103137811804,"lng":33.63866144845382},{"lat":49.09694682809236,"lng":33.61746879914138},{"lat":49.08920750204137,"lng":33.61734796797724},{"lat":49.07643862058337,"lng":33.61246117651179}]
how send such string json array of objects(post request).
entity without getters , setters
public class lepcoord implements serializable { private static final long serialversionuid = 1l; @id @basic(optional = false) @notnull @size(min = 1, max = 30) @column(name = "tplnr") private string tplnr; @size(max = 2147483647) @column(name = "coord") private string coord;
controller
@post @requestmapping(value= "/lep/{voltage}", method = requestmethod.post, headers = "accept=application/json") @responsebody public responseentity<list<lepcoord>> lep (@pathvariable string voltage) { return new responseentity<>(gisdaoservice.lep(voltage), httpstatus.ok); }
and service
@transactional(readonly = true) public list <lepcoord> lep (string voltage) { query query = this.em.createquery( " lepcoord "); list <lepcoord> rez = null; try { rez = (list<lepcoord>) query.getresultlist(); } catch (persistenceexception r) { return null; } return rez; }
hibernate cant handle json type if storeing coord json in postgre. may knows easier way. not write own classes work postgres json type
you using hibernate use custom usertype knows how handle json.
create hibernate usertype
public class geojsontype implements usertype { protected static final int[] sql_types = { java.sql.types.varchar }; @override public int[] sqltypes() { return sql_types; } @override public class returnedclass() { return geoentity.class; } @override public boolean equals(object x, object y) throws hibernateexception { if (x == y) { return true; } else if (x == null || y == null) { return false; } else { return x.equals(y); } } @override public int hashcode(object x) throws hibernateexception { return x.hashcode(); } @override public object nullsafeget(resultset rs, string[] names, object owner) throws hibernateexception, sqlexception { // if (rs.wasnull()) // { // return null; // } //this json stored in db string rsarr = rs.getstring(names[0]); if (rsarr == null) return null; geoentity detailattr = json.toobject(rsarr, geoentity.class, null); return detailattr; } @override public void nullsafeset(preparedstatement st, object value, int index) throws hibernateexception, sqlexception { if (value == null) { st.setnull(index, sql_types[0]); } else { //when stroing object db convert json geoentity castobject = (geoentity) value; string json = json.tojson(castobject); st.setstring(index, json); } } @override public object deepcopy(object value) throws hibernateexception { return value; } @override public boolean ismutable() { return true; } @override public serializable disassemble(object value) throws hibernateexception { return null; } @override public object assemble(serializable cached, object owner) throws hibernateexception { return null; } @override public object replace(object original, object target, object owner) throws hibernateexception { return original; } }
your entity.java
@type(type = "fqn geojsontype")
@column(name = "geo")
public geoentity getgeo()
{
return geo;
}
Comments
Post a Comment