zilcuanu
zilcuanu

Reputation: 3715

One to many associated entities are not getting deleted with Spring Data JPA

I have a one-to-many relationship between two entities Order and LineItem. I also have a parent to Order entity which is Customer and Customer to Order is one-to-many. When I try to delete the order entity, neither the order, nor the line_items associated with the order is getting deleted. I have tried the below options

Below is the Order and LineItem definitiona

Order

@Data
@NoArgsConstructor
@Builder
@AllArgsConstructor
@EqualsAndHashCode(exclude = {"customer", "lineItems"})
@ToString(exclude = {"customer", "lineItems"})
@Entity
@Table(name = "orders")
public class Order {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Min(value = 2000, message = "Min order value should be 2000 INR")
    @Max(value = 10_000, message = "Max order value can be 10000 INR")
    private double price;

    @PastOrPresent(message = "Order date cannot be in future")
    private LocalDate date;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="customer_id", nullable = false)
    @JsonBackReference
    private Customer customer;

    @OneToMany(mappedBy = "order",
               cascade = CascadeType.ALL,
               orphanRemoval = true,
               fetch = FetchType.EAGER)
    private Set<LineItem> lineItems;

@Data
@NoArgsConstructor
@EqualsAndHashCode(exclude = "order")
@ToString(exclude = "order")
@Builder
@AllArgsConstructor
@Entity
@Table(name="line_items")
public class LineItem {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    private int qty;
    private double price;
    private String name;
    @JsonIgnore
    @ManyToOne
    @JoinColumn(name="order_id", nullable = false)
    private Order order;
}

 this.orderRepository.deleteById(fetchedOrder.getId());

Output:

Hibernate: 
    select
        lineitems0_.order_id as order_id5_2_0_,
        lineitems0_.id as id1_2_0_,
        lineitems0_.id as id1_2_1_,
        lineitems0_.name as name2_2_1_,
        lineitems0_.order_id as order_id5_2_1_,
        lineitems0_.price as price3_2_1_,
        lineitems0_.qty as qty4_2_1_ 
    from
        line_items lineitems0_ 
    where
        lineitems0_.order_id=?

Where am I going wrong and how to troubleshoot this issue?

Upvotes: 1

Views: 470

Answers (2)

zilcuanu
zilcuanu

Reputation: 3715

I figured out the reason. There was one more entity reference with Customer and Order. I removed the reference of order from Customer and saved the customer to resolve the issue.

   customer.getOrders().removeIf(order -> order.getId() == orderId);
   this.customerRepository.save(customer);

Upvotes: 0

Yevgeniy
Yevgeniy

Reputation: 161

This might be a shot in the dark, but the only way I was able to reproduce Hibernate only executing a SELECT query and not going for DELETE was when I annotated the service method that calls orderRepository.deleteById(id); with @Transactional(readOnly=true), so this might be it, otherwise it might be some combination of configuration properties you've set.

It might help to add your dependencies versions to the question, since it might be some version-specific behavior of Hibernate.

While I'm at it, note that using lombok's @EqualsAndHashCode and @ToString (and, by extension, @Data) with jpa entities is not advised due to potential problems with related entities causing infinite loop and such.

Upvotes: 1

Related Questions