java - JSON Infinite string when using hibernate one to many and many to one mapping REST web service -
i facing weird issue when using hibernate 1 many , many 1 mapping , returning data using json swing client rest web service.
when web service returning salesorder object. have checked contains orderlines objects set. but, if open 1 of orderline object, again has sales order object.
this chaining causing issue client side infinite string of json being returned.
like below...
[ { "salesordernumber":"1", "customercode":"1", "totalprice":50.0, orderlines": [ { "salesordernumber":"1", "productcode":"2", "quantity":1, "salesorder":{"salesordernumber":"1","customercode":"1","totalprice":50.0,"orderlines":[{"salesordernumber":"1","productcode":"2","quantity":1,"salesorder":{"salesordernumber":"1","customercode":"1","totalprice":50.0,"orderlines": ............................................. ...............................
i have tried set @jsonignore don't want sent client, but, didn't help.
my 2 entities below:
@entity @table(name = "salesorder") //@jsonignoreproperties({ "hibernatelazyinitializer", "handler" }) public class salesorder implements serializable { private static final long serialversionuid = 1l; @id @column(name = "salesordernumber", unique = true, nullable = false) private string salesordernumber; @column(name = "customercode") private string customercode; @column(name = "totalprice") private double totalprice; @jsonignore @onetomany(fetch = fetchtype.eager, mappedby="salesorder",cascade=cascadetype.all) private set<orderlines> orderlines = new hashset<orderlines>(); public set<orderlines> getorderlines() { return orderlines; } public void setorderlines(set<orderlines> orderlines) { this.orderlines = orderlines; } public string getsalesordernumber() { return salesordernumber; } public void setsalesordernumber(string salesordernumber) { this.salesordernumber = salesordernumber; } public string getcustomercode() { return customercode; } public void setcustomercode(string customercode) { this.customercode = customercode; } public double gettotalprice() { return totalprice; } public void settotalprice(double totalprice) { this.totalprice = totalprice; } } @entity @table(name = "orderlines") //@jsonignoreproperties({ "hibernatelazyinitializer", "handler" }) public class orderlines implements serializable { private static final long serialversionuid = 1l; @id @column(name = "salesordernumber", unique = true, nullable = false) private string salesordernumber; @id @column(name = "productcode") private string productcode; @column(name = "quantity") private int quantity; @jsonignore @manytoone(fetch = fetchtype.lazy,cascade=cascadetype.all) @joincolumn(name="salesordernumber") private salesorder salesorder; public salesorder getsalesorder() { return salesorder; } public void setsalesorder(salesorder salesorder) { this.salesorder = salesorder; } public string getsalesordernumber() { return salesordernumber; } public void setsalesordernumber(string salesordernumber) { this.salesordernumber = salesordernumber; } public string getproductcode() { return productcode; } public void setproductcode(string productcode) { this.productcode = productcode; } public int getquantity() { return quantity; } public void setquantity(int quantity) { this.quantity = quantity; } }
it's really bad idea send entities json, couple clients internal representation of system... , bad hacks come this. if want regardless , suffer later (or let 1 of future colleagues suffer , curse you), keep reading.
the reason why doesn't work because hibernate creates proxies objects gets db, , annotations lost. there jackson extension take care of jackson-datatype-hibernate, please don't (unless app trivial , never change)
Comments
Post a Comment