我尝试在Spring Data项目中配置 OneToMany ManyToOne 映射,但是有一些问题。

所以我有两个实体:雇主和项目。一位雇主可能有许多项目。

实体类:

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>();

    ......................
}

项目.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;
    ......................
}

存储库类:
public interface EmployerRepository extends JpaRepository<Employer, Integer> {
}

public interface ProjectRepository extends JpaRepository<Project, Integer> {
}

服务:
@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_ID  .  NAME
......................
1            .  Google
2            .  Oracle
3            .  Facebook

项目表:
PROJECT_ID .    NAME            . EMPLOYER_ID
.......................................
1          . Create web site    .  1
2          . Create reporting   .  2
3          . Create web service .  3
4          . Fixing web site    .  1

我期待这样的事情:
[{"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"}}]

但是getEmployers类中的controller方法返回此代码:
[{"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":
...................

如果这个问题讨论了很多次,但是我找不到合适的答案,请抱歉。

提前致谢!

最佳答案

您在EmployerProject之间存在双向关联。此外,您还配置了关联的两面以EAGER ly加载(默认情况下@ManyToOne关联是EAGER ly提取的,并且您也已强制@OneToMany面也被EAGER提取了)。由于这种配置,当序列化框架加载Employer时,它会找到具有Project反向链接的有效Employer,最终陷入循环循环。

为了获得所需的结果,您必须将@ManyToOne一侧(在Project实体上)标记为LAZY,以便将@ManyTone(fetch = FetchType.LAZY)提取。

09-28 01:51