无法从子项中获取父项数据
Unable to fetch Parent data from Child
我正在尝试查看是否将 Cab
分配给任何 Employee
,以便我可以获取该 Employee
Id 并将 cab 设置为 null
,但是 @OneToMany
映射,只是返回一个员工列表 & 通过这个我没有得到任何方法,比如 cab.getEmp()
来获取员工详细信息
Employee.java
@Data
@Entity
@Table(name = "employee")
public class Employee {
@Id
private Integer id;
private String username;
private String password;
private String role;
private String dropLocation;
@ManyToOne(
cascade = CascadeType.ALL,
fetch = FetchType.EAGER
)
@JoinColumn(
name = "empCab",
referencedColumnName = "cabId"
)
public Cab cab;
}
Cab.java
@Data
@Entity
@Table(name = "cab")
public class Cab {
@Id
private Integer cabId;
private Integer cabNumber;
private String cabShift;
@OneToMany(
cascade = CascadeType.ALL,
fetch = FetchType.EAGER,
mappedBy= "cab"
)
private List<Employee> emp = new ArrayList<>();
}
控制器
@GetMapping("deleteCab")
public ModelAndView deleteCab(@RequestParam("id") Integer id, ModelAndView mvc){
Cab cab = cabRepo.findById(id).orElse(null);
if(cab!=null){
List<Employee> emp = cab.getEmp();
if(!cab.getEmp().isEmpty()){
//e1.setCab(null);
//empRepo.save(e1);
mvc.addObject("msg", "Cab deleted & an employee cab detail also got changed");
mvc.setViewName(NOTHING_JSP);
} else {
cabRepo.deleteById(id);
mvc.addObject("msg", "Cab removed from the database");
mvc.setViewName(NOTHING_JSP);
}
}
}
在您的代码中,Cab
是永久的 class,Employee
是子 class,您的目标是移除子 class 对父的依赖class。为此,您必须从 Cab
中提取 Employee
并使用 employee.setCab(null)
.
删除与 Cab
的关系
@GetMapping("/deleteCab")
@ResponseBody
public ModelAndView deleteCab(@RequestParam("id") Integer id, ModelAndView mvc){
Cab cab = cabRepo.findById(id).orElse(null);
if(cab != null){
// Extract employee from cab
for(Employee emp: cab.getEmp())
{
// Remove the relationship with cab
emp.setCab(null);
}
cabRepo.save(cab);
mvc.addObject("msg", "Cab deleted & an employee cab detail also got changed");
mvc.setViewName(NOTHING_JSP);
} else {
cabRepo.deleteById(id);
mvc.addObject("msg", "Cab removed from the database");
mvc.setViewName(NOTHING_JSP);
}
return mvc;
}
我正在尝试查看是否将 Cab
分配给任何 Employee
,以便我可以获取该 Employee
Id 并将 cab 设置为 null
,但是 @OneToMany
映射,只是返回一个员工列表 & 通过这个我没有得到任何方法,比如 cab.getEmp()
来获取员工详细信息
Employee.java
@Data
@Entity
@Table(name = "employee")
public class Employee {
@Id
private Integer id;
private String username;
private String password;
private String role;
private String dropLocation;
@ManyToOne(
cascade = CascadeType.ALL,
fetch = FetchType.EAGER
)
@JoinColumn(
name = "empCab",
referencedColumnName = "cabId"
)
public Cab cab;
}
Cab.java
@Data
@Entity
@Table(name = "cab")
public class Cab {
@Id
private Integer cabId;
private Integer cabNumber;
private String cabShift;
@OneToMany(
cascade = CascadeType.ALL,
fetch = FetchType.EAGER,
mappedBy= "cab"
)
private List<Employee> emp = new ArrayList<>();
}
控制器
@GetMapping("deleteCab")
public ModelAndView deleteCab(@RequestParam("id") Integer id, ModelAndView mvc){
Cab cab = cabRepo.findById(id).orElse(null);
if(cab!=null){
List<Employee> emp = cab.getEmp();
if(!cab.getEmp().isEmpty()){
//e1.setCab(null);
//empRepo.save(e1);
mvc.addObject("msg", "Cab deleted & an employee cab detail also got changed");
mvc.setViewName(NOTHING_JSP);
} else {
cabRepo.deleteById(id);
mvc.addObject("msg", "Cab removed from the database");
mvc.setViewName(NOTHING_JSP);
}
}
}
在您的代码中,Cab
是永久的 class,Employee
是子 class,您的目标是移除子 class 对父的依赖class。为此,您必须从 Cab
中提取 Employee
并使用 employee.setCab(null)
.
Cab
的关系
@GetMapping("/deleteCab")
@ResponseBody
public ModelAndView deleteCab(@RequestParam("id") Integer id, ModelAndView mvc){
Cab cab = cabRepo.findById(id).orElse(null);
if(cab != null){
// Extract employee from cab
for(Employee emp: cab.getEmp())
{
// Remove the relationship with cab
emp.setCab(null);
}
cabRepo.save(cab);
mvc.addObject("msg", "Cab deleted & an employee cab detail also got changed");
mvc.setViewName(NOTHING_JSP);
} else {
cabRepo.deleteById(id);
mvc.addObject("msg", "Cab removed from the database");
mvc.setViewName(NOTHING_JSP);
}
return mvc;
}