[英]How to structure an Entity in Spring boot/ JPA having multiple foreign keys?
[英]How to add multiple entity in a single entity in jpa spring boot application
我正在尝试在一个实体中添加多个实体,我不知道这种方式是否可行请参考我下面的代码和帮助
下面的代码是实体表
@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;
}
}
和就业class是
@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 + "]";
}
}
和办公室 class 是
@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 引导存储库用于所有相应的实体
@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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.