我尝试在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>();
......................
}
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
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":
...................
如果这个问题经过多次讨论,我很抱歉,但我没有找到合适的答案。
提前致谢!
答案 0 :(得分:5)
Employer
和Project
之间存在双向关联。此外,您已配置要加载的关联的两侧EAGER
默认情况下@ManyToOne
关联被提取EAGER
并且您已强制提取@OneToMany
方EAGER
也是如此。由于这种配置,当序列化框架加载Employer
时,它会找到有Project
的反向链接的有效Employer
,并最终陷入循环循环。
为了获得您想要的结果,您必须将@ManyToOne
侧(在Project
实体上)标记为LAZY
,标记为@ManyTone(fetch = FetchType.LAZY)
。
答案 1 :(得分:1)
添加@JsonManagedReference在关系的前面部分(即User.java类):
@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>();
...
添加@JsonBackReference在关系的后半部分(即Role.java类):
@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>();
...
这对我很有用。 : - )