java - Why JPA OneToMany mapping findAll method returned recursive object? -


i have 2 domain classes, po class , product class, , there list inside po class. product manytoone po, , po onetomany product.

public class po {     ....      @onetomany(mappedby = "po", cascade= cascadetype.all, orphanremoval=true)     private list<product> products = new arraylist<>();     ..... }  public class product {     ....     @manytoone(optional = false)     @joincolumn(name = "po_id")     private po po;     .... } 

and when building po object. set po field in product reference of po.

po repo = new po(); .... (stockline item: source.getstocklines()) {     product prod = new product();     ....     prod.setpo(repo);     repo.getproducts().add(prod); } 

when call porepo.save(po); it's working, foreign key filled , data in both tables correct.

but problem when fetch po using porepo.findall(), debugged , found object recursively reference itself.

enter image description here

is normal? don't think it's normal, have done wrong?

btw, if don't add @jsonmanagedreference , @jsonbackreference generated json in recursive format too. json issue can fixed above annotations, how fix returned object issue? using spring boot data jpa 1.5.6.release

it's correct behavior lazy loading. load root object , during serializing getproducts() called. each of product tries lazyly load po, in turn loads list of products etc.

to break chain introduce dto (data transfer object) , convert entity tdo objects. serialize dto.

there alternative way "unproxy" entity. see here


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -