java - Spring Data one to many mapping -


i try configure onetomany , manytoone mapping in spring data project have issues.

so have 2 entities: employer , project. 1 employer have many projects.

entity classes:

employer.java

@entity @table (name="employer") public class employer {      @id     @sequencegenerator(name="my_seq", sequencename="global_sequence")     @generatedvalue(strategy = generationtype.sequence ,generator="my_seq")     @column (name="employer_id")     private int id;      @column (name="name")     private string name;      @joincolumn (name="employer_id")     @onetomany(mappedby = "employer", cascade = cascadetype.all, fetch = fetchtype.eager)     private set<project> project = new hashset<project>();      ...................... }    

project.java

@entity @table (name="project") public class project {      @id     @sequencegenerator(name="my_seq", sequencename="global_sequence")     @generatedvalue(strategy = generationtype.sequence ,generator="my_seq")     @column (name="project_id")     private int id;      @column (name="name")     private string name;      @manytoone     @joincolumn(name="employer_id", nullable = false)     private employer employer;     ...................... } 

repository classes:

public interface employerrepository extends jparepository<employer, integer> { }  public interface projectrepository extends jparepository<project, integer> { } 

services:

@service public class employerserviceimpl implements employerservice {      @autowired     private employerrepository employerrepository;      @override     public list<employer> getallemployers() {         return employerrepository.findall();     }    }  @service public class projectserviceimpl implements projectservice {      @autowired     private projectrepository projectrepository;      @override     public list<project> getallprojects() {         return projectrepository.findall();     } } 

controller:

@controller public class maincontroller {      private employerservice employerservice;      private projectservice projectservice;      @autowired(required = true)     @qualifier(value = "employerservice")     public void setemployerservice(employerservice employerservice) {         this.employerservice = employerservice;     }      @autowired(required = true)     @qualifier(value = "projectservice")     public void setprojectservice(projectservice projectservice) {         this.projectservice = projectservice;     }       @requestmapping(value="/employers", method=requestmethod.get)     public @responsebody list<employer> getemployers(@requestparam(value = "name", required = false) string name) {         return employerservice.getallemployers();             }     .............................. } 

employer table:

employer_id  .  name ...................... 1            .  google 2            .  oracle 3            .  facebook  

project table:

project_id .    name            . employer_id ....................................... 1          . create web site    .  1 2          . create reporting   .  2 3          . create web service .  3 4          . fixing web site    .  1         

i'm expecting this:

[{"id":1,"name":"google","project":[{"id":1,"name":"create web site"}, {"id":4,"name":"fixing web site"}},  {"id":2,"name":"oracle","project":{"id":2,"name":"create reporting"}},  {"id":3,"name":"facebook","project":{"id":3,"name":"create web service"}}] 

but getemployers method controller class return one:

[{"id":1,"name":"oracle","project":[{"id":4,"name":"fixing web site", "employer":{"id":1,"name":"oracle","project":[{"id":4,"name":"fixing web site", "employer":{"id":1,"name":"oracle","project":[{"id":4,"name":"fixing web site", "employer":{"id":1,"name":"oracle","project":[{"id":4,"name":"fixing web site", "employer":{"id":1,"name":"oracle","project":[{"id":4,"name":"fixing web site", "employer":{"id":1,"name":"oracle","project":[{"id":4,"name":"fixing web site","employer": ................... 

please sorry if question discussed many times didn't find suitable answer.

thanks in advance!

you have bi-directional association between employers , projects. additionally, have configured both sides of association loaded eagerly (@manytoone associations fetched eagerly default , have forced @onetomany side fetched eagerly well). due configuration, when serialization framework loads employers, finds valid projects have back-links employers , ends getting stuck in cyclic loop.

in order result want, have mark @manytoone side (on project entity) fetched lazyly @manytone(fetch = fetchtype.lazy).


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -