从现有的一个更改ID字段中保存新实体(带有嵌套实体)

时间:2019-05-10 14:29:17

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

使用Spring Data JPA,我从DB获得了一个实体(带有嵌套实体),我只想从中创建一个新实体,仅更改ID字段(在父亲和孩子身上)。但是保存后,在数据库上,我看到了具有新ID的新实体父亲,但没有任何子实体。

这是我的服务班级:

  @Transactional
  public void createEmployeeFromExisting(Integer employeeNo) {
    Employee employeeOld = employeeRepository.findById(employeeNo).get();
    Integer newNo = 10003;
//    Employee employeeNew = new Employee();
//    BeanUtils.copyProperties(employeeOld, employeeNew);
//    employeeNew.setEmpNo(newNo);
    //Employee newEm = (Employee) SerializationUtils.clone(employeeOld);
    entityManager.detach(employeeOld);
    employeeOld.setEmpNo(newNo);
    employeeRepository.save(employeeOld);

  }

这是我的实体结构:

enter image description here

@Entity
@Data
@NoArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
@Table(name = "employees")
public class Employee implements Serializable {

    @Id
    @Column(name = "emp_no")
    Integer empNo;
    @Column(name = "birth_date")
    LocalDate birthDate;
    @Column(name = "first_name")
    String firstName;
    @Column(name = "last_name")
    String lastName;
    @Column(name = "gender")
    String gender;
    @Column(name = "hire_date")
    LocalDate hireDate;
    @OneToMany(mappedBy = "employeeByEmpNo")
    Collection<DeptEmp> deptEmpsByEmpNo;
    @OneToMany(mappedBy = "employeeByEmpNo")
    Collection<Salary> salaryByEmpNo;
}

@Entity
@Data
@NoArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
@IdClass(SalaryPK.class)
@Table(name = "salaries")
public class Salary implements Serializable {

    @Id
    @Column(name = "emp_no")
    Integer empNo;
    @Id
    @Column(name = "from_date")
    LocalDate fromDate;
    @Column(name = "salary")
    Integer salary;
    @Column(name = "to_date")
    LocalDate toDate;
    @ManyToOne
    @JoinColumn(name = "emp_no", referencedColumnName = "emp_no", nullable = false, insertable = false, updatable = false)
    Employee employeeByEmpNo;
}

@Data
@NoArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
@Entity
@Table(name = "dept_emp")
@IdClass(DeptEmpPK.class)
public class DeptEmp implements Serializable {

    @Id
    @Column(name = "emp_no")
    Integer empNo;
    @Id
    @Column(name = "dept_no")
    String deptNo;
    @Column(name = "from_date")
    LocalDate fromDate;
    @Column(name = "to_date")
    LocalDate toDate;
    @ManyToOne
    @JoinColumn(name = "emp_no", referencedColumnName = "emp_no", nullable = false, insertable = false, updatable = false)
    Employee employeeByEmpNo;
    @ManyToOne
    @JoinColumn(name = "dept_no", referencedColumnName = "dept_no", nullable = false, insertable = false, updatable = false)
    Department departmentsByDeptNo;
}

@Entity
@Data
@NoArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
@Table(name = "departments")
public class Department implements Serializable {

    @Id
    @Column(name = "dept_no")
    String deptNo;
    @Column(name = "dept_name")
    String deptName;
    @OneToMany(mappedBy = "departmentsByDeptNo")
    Collection<DeptEmp> deptEmpByDeptNo;
}

0 个答案:

没有答案