我无法使用JPA中的单向@OneToMany
关系来获取记录。知道是否缺少了什么吗?记录正在被持久化;只有检索记录有问题。
class Employee{
int empId;
...
@OneToMany(cascade = ALL, mappedBy = "deptId",fetch = FetchType.EAGER)
List<Department> departments=new ArrayList<>();
//getters and setters
}
class Department{
String code;
....
int deptId;
//getters and setters
}
empRepository.findById(empId).getDepartments(); // is not returning rows even if records are available.
1条答案
按热度按时间xvw2m8pv1#
将**
@JoinColumn(name = "deptId")
与@OneToMany
**一起使用此处是在JPA中实现一对多单向或双向关系的最佳参考方法https://vladmihalcea.com/the-best-way-to-map-a-onetomany-association-with-jpa-and-hibernate/
https://medium.com/@rajibrath20/the-best-way-to-map-a-onetomany-relationship-with-jpa-and-hibernate-dbbf6dba00d3