1

父实体:

@Entity
class Employee {
    @Id
    @GeneratedValue
    private Long id;
    private String firstName ="";
    private String lastName;
    private String role;

    @OneToMany(mappedBy = "employee", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
    private List<Order> orders;

    public void addOrder(Order order) {
        orders.add(order);
        order.setEmployee(this);
    }

    public void removeOrder(Order order) {
        orders.remove(order);
        order.setEmployee(null);
    }

    @Override
    public boolean equals(Object o) {

        if (this == o)
            return true;
        if (!(o instanceof Employee))
            return false;
        Employee employee = (Employee) o;
        return Objects.equals(this.id, employee.id)
                && Objects.equals(this.firstName, employee.firstName)
                && Objects.equals(this.lastName, employee.lastName)
                && Objects.equals(this.role, employee.role);
    }

    @Override
    public int hashCode() {
        return Objects.hash(this.id, this.firstName, this.lastName, this.role);
    }

    // Constructor, getters, setters
}

子实体:

@Entity
@Table(name = "CUSTOMER_ORDER")
class Order {
    @Id
    @GeneratedValue
    private Long id;

    private String description;
    private Status status;

    @ManyToOne
    private Employee employee;

    @Override
    public boolean equals(Object o) {

        if (this == o)
            return true;
        if (!(o instanceof Order))
            return false;
        Order order = (Order) o;
        return Objects.equals(this.id, order.id) && Objects.equals(this.description, order.description)
                && this.status == order.status;
    }

    @Override
    public int hashCode() {
        return Objects.hash(this.id, this.description, this.status);
    }

    // Constructor, getters, setters
}

应用代码:

public static app() {
    ...
    Employee employee1 = new Employee("Bilbo", "Baggins", "burglar", new ArrayList<>());
    employeeRepository.save(employee1);
    
    Order order1 = new Order("Xiaomi", Status.IN_PROGRESS);
    orderRepository.save(order1);

    employee1.addOrder(order1);
    employeeRepository.save(employee1);
    employeeRepository.flush();
    orderRepository.flush();
    employee1.removeOrder(order1);
    employeeRepository.save(employee1);         
}

我面临的是:

对于employee表:

| id | first_name | last_name | role    |
| -- | ---------- | --------- | ------- |
| 1  | Bilbo      | Baggins   | burglar |

对于customer_order表:

| id | description | status    | employee_id |
| -- | ----------- | --------- | ----------- |
| 1  | Xiaomi      | 0         | 1           |

所以,我希望在这些字符串执行之后

employee1.removeOrder(order1);
employeeRepository.save(employee1);

order1行将从数据库中删除。我想我需要某种方式来触发order1实体保存。我玩过,即使order1保存后,它仍然保留在 dbemployee_idnull。我希望删除孩子,因为我将orders字段配置为orphanRemoval = true.

我的配置:

  • 爪哇:11
  • 休眠:5.4.32
  • 弹簧启动:2.5.3
  • mysql: 8.0.26
  • 弹簧数据-jpa:2.5.3
  • 休眠-jpamodelgen:5.4.32
4

1 回答 1

1

执行employee1后将被分离employeeRepository.flush()。您应该将其状态更改为持久。为什么employeeRepository.flush()在最后一行代码之后不执行?

于 2021-12-26T18:38:04.107 回答