我正试图在一个实体中添加多个实体,我不知道这种方式是否可行,请参阅我下面的代码并帮助
下面的代码是实体表
@Entity
@Table(name = "agent_employee")
public class AgentEmployee extends Agent implements Serializable{
private static final long serialVersionUID = 1L;
@OneToMany // unidirectional
@JoinColumn(name = "employment_id", referencedColumnName = "id")
List<Employment> employmnet = new ArrayList<Employment>();
@OneToMany(
mappedBy = "agent",
cascade = CascadeType.ALL,
orphanRemoval = true
)
private Set<Officess> officess = new HashSet<>();
public List<Employment> getEmploymnet() {
return employmnet;
}
public void setEmploymnet(List<Employment> employmnet) {
this.employmnet = employmnet;
}
public Set<Officess> getOfficess() {
return officess;
}
public void setOfficess(Set<Officess> officess) {
this.officess = officess;
}
}
就业等级为
@Data
@Entity
public class Employment {
@Id
@Column(nullable = false, unique = true)
private Long id;
private String empName;
private String location;
@Override
public String toString() {
return "Employment [id=" + id + ", empName=" + empName + ", location=" + location + "]";
}
}
办公室等级为
@Data
@Entity
@Table(name = "officess")
public class Officess implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String officeName;
@ManyToOne
@JoinColumn(name = "agent_emp")
private AgentEmployee agent;
}
我已经将spring-boot存储库用于所有相应的实体
@GetMapping(path = "/add")
public @ResponseBody String addAgentEmployee() {
try {
AgentEmployee agemp = new AgentEmployee();
agemp.setFirstName("harish");
agemp.setLastName("kadsuru");
agemp.setEmail("hari**********is.net");
Employment emp1 = new Employment();
Employment emp2 = new Employment();
Employment emp3 = new Employment();
emp1.setId(501l);
emp2.setId(502l);
emp3.setId(503l);
emp1.setEmpName("junior engineer");
emp2.setEmpName("senior engineer");
emp3.setEmpName("team leader");
emp1.setLocation("bengaluru");
emp2.setLocation("mumbai");
emp3.setLocation("UAE");
List<Employment> emps = Arrays.asList(emp1, emp2, emp3);
employmentRepository.saveAll(emps);
agemp.setEmploymnet(emps);
agentEmployeeRepository.save(agemp);
return "saved";
} catch (Exception e) {
return "unable to save data due to exception";
}
}
@GetMapping("addOffice")
public @ResponseBody String addAgentEmployeeOffice() {
AgentEmployee emp;
Optional<AgentEmployee> agemp = agentEmployeeRepository.findById(27l);
if (agemp.isPresent()) {
emp = agemp.get();
}
else {
emp =new AgentEmployee();
emp.setFirstName("garish");
emp.setLastName("tumkur");
emp.setEmail("garish.kr@cyclotis.net");
}
log.info("###### {}",agemp);
Officess off1 = new Officess();
Officess off2 = new Officess();
Officess off3 = new Officess();
off1.setOfficeName("Google");
off2.setOfficeName("facebook");
off3.setOfficeName("Instagram");
Set<Officess> offices = emp.getOfficess();
offices.add(off1);
offices.add(off2);
offices.add(off3);
agentEmployeeRepository.save(emp);
log.info("######## {}", offices);
return "saved";
}
我认为代码没有任何问题,但我认为我在保存数据时遇到了问题。请任何机构参考分析这个问题的正确方法。
看起来映射不正确。还要验证您是否有EMPID列。您不需要在您的案例中使用@JoinTable
注释。
在保存数据时,应使用@PostMapping
StatusReport-删除private BigInteger EMPID;
,因为它用于加入
@Entity
@Table(name="statusreport")
public class StatusReport {
private BigInteger COMPLIANCEID;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private BigInteger STATUSRPTID;
private String COMMENTS;
private Date CREATEDDATE;
private BigInteger DEPARTMENT_ID;
@OneToOne
@JoinColumn(name = "EMPID")
private Employees employee;
//others methods
}
员工-删除private BigInteger DEPARTMENT_ID;
,因为它用于加入
@Entity
public class Employees {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private BigInteger EMPID;
private String FIRSTNAME;
private String LASTNAME;
private Date DOB;
private String EMAIL;
@OneToOne
@JoinColumn(name = "DEPARTMENT_ID")
private Department department;
//others methods
}