Spring Data one to many mapping

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!


You have a bi-directional association between Employer s and Project s. Additionally, you have configured both sides of the association to be loaded EAGER ly ( @ManyToOne associations are fetched EAGER ly by default and you have forced the @OneToMany side to be fetched EAGER ly as well). Due to this configuration, when the serialization framework loads the Employer s, it finds valid Project s that have back-links to the Employer s and ends up getting stuck in a cyclic loop.

In order to get the result you want, you will have to mark the @ManyToOne side (on the Project entity) to be fetched LAZY ly as @ManyTone(fetch = FetchType.LAZY) .


Add the @JsonManagedReference In the forward part of the relationship (ie User.java class):

@Entity
public class User implements Serializable {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private long id;

    @Column(name="name")
    private String name;

    @ManyToMany
    @JoinTable(name="users_roles",joinColumns=@JoinColumn(name = "user_fk"), inverseJoinColumns=@JoinColumn(name = "role_fk"))
    @JsonManagedReference
    private Set<Role> roles = new HashSet<Role>();

    ...

Add the @JsonBackReference In the back part of the relationship (ie Role.java class):

@Entity
public class Role implements Serializable {

    @Id 
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

    @ManyToMany(mappedBy="roles")
    @JsonBackReference
    private Set<User> users = new HashSet<User>();

    ...

This works very well for me. :-)

链接地址: http://www.djcxy.com/p/86240.html

上一篇: 列属性未正确对齐

下一篇: Spring Data一对多映射