title: Spring Data one to many mapping
body: <p>I try to configure <strong>OneToMany</strong> and <strong>ManyToOne</strong> mapping in Spring Data project but have some issues.</p>
  
  <p>So I have two entities: <em>Employer</em> and <em>Project</em>. One Employer could have many projects.</p>
  
  <p><em>Entity classes:</em></p>
  
  <p>Employer.java</p>
  
  <pre><code>@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&lt;Project&gt; project = new HashSet&lt;Project&gt;();
  
      ......................
  }   
  </code></pre>
  
  <p>Project.java</p>
  
  <pre><code>@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;
      ......................
  }
  </code></pre>
  
  <p><em>Repository classes:</em></p>
  
  <pre><code>public interface EmployerRepository extends JpaRepository&lt;Employer, Integer&gt; {
  }
  
  public interface ProjectRepository extends JpaRepository&lt;Project, Integer&gt; {
  }
  </code></pre>
  
  <p><em>Services:</em></p>
  
  <pre><code>@Service
  public class EmployerServiceImpl implements EmployerService {
  
      @Autowired
      private EmployerRepository employerRepository;
  
      @Override
      public List&lt;Employer&gt; getAllEmployers() {
          return employerRepository.findAll();
      }   
  }
  
  @Service
  public class ProjectServiceImpl implements ProjectService {
  
      @Autowired
      private ProjectRepository projectRepository;
  
      @Override
      public List&lt;Project&gt; getAllProjects() {
          return projectRepository.findAll();
      }
  }
  </code></pre>
  
  <p><em>Controller:</em></p>
  
  <pre><code>@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&lt;Employer&gt; getEmployers(@RequestParam(value = "name", required = false) String name) {
          return employerService.getAllEmployers();        
      }
      ..............................
  }
  </code></pre>
  
  <p><em>Employer table:</em></p>
  
  <pre><code>EMPLOYER_ID  .  NAME
  ......................
  1            .  Google
  2            .  Oracle
  3            .  Facebook 
  </code></pre>
  
  <p><em>Project table:</em></p>
  
  <pre><code>PROJECT_ID .    NAME            . EMPLOYER_ID
  .......................................
  1          . Create web site    .  1
  2          . Create reporting   .  2
  3          . Create web service .  3
  4          . Fixing web site    .  1        
  </code></pre>
  
  <p>I'm expecting something like this:</p>
  
  <pre><code>[{"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"}}]
  </code></pre>
  
  <p>But <code>getEmployers</code> method from <code>controller</code> class return this one:</p>
  
  <pre><code>[{"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":
  ...................
  </code></pre>
  
  <p>Please sorry if this question discussed many times but I didn't find any suitable answer.</p>
  
  <p>Thanks in advance!</p>