【问题标题】:How to add multiple entity in a single entity in jpa spring boot application如何在 jpa spring boot 应用程序中的单个实体中添加多个实体
【发布时间】:2021-03-21 02:18:55
【问题描述】:

我正在尝试在一个实体中添加多个实体,我不知道这种方式是否可行请参考我下面的代码和帮助

下面的代码是实体表

    @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";   
}

我认为代码没有任何问题,但我认为我在保存数据时遇到了问题。请任何机构参考正确的方法来分析这个问题。

【问题讨论】:

    标签: mysql spring-boot hibernate jpa spring-data-jpa


    【解决方案1】:

    您的映射似乎不正确。还要验证您有一个 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
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-05
      • 2023-03-17
      • 2020-07-04
      • 2018-06-21
      • 1970-01-01
      • 1970-01-01
      • 2019-08-17
      • 2023-03-08
      • 1970-01-01
      相关资源
      最近更新 更多