I try to configure OneToMany and ManyToOne mapping in Spring Data project but have some issues.
So I have two entities: Employer and Project. One Employer could 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 something like 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 from controller
class return this 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 this question discussed many times but I didn't find any suitable answer.
Thanks in advance!
@ManyToOne(fetch = FetchType.LAZY)
toProject
entity but it doesn't help, the result is the same like the first one. – Aoudad