Deleting product entity and keeping orders intact in java spring boot application

98 Views Asked by At

In a java spring boot application i have Product, OrderDetail and Order entities, so i have a many to many between Product and Order through OrderDetail. How can i delete products and still keep the OrderDetail intact? i was thinking about implementing a soft delete but i think that i should modify the deletion methods on the whole project( and i have many entities related to the Product entity), so i would like to keep the phisical deletion. If this is not really ok, then i would like to hear any suggestions.

These are the entities

@Table
@Builder
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Product{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    private Long id;

 <<Other relationships>>>
@Entity
@Data
@NoArgsConstructor
public class OrderDetail {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", nullable = false)
    private Long id;

    @ManyToOne
    @JoinColumn(name = "order_id")
    private Orders order;
    @ManyToOne
    @JoinColumn(name = "product_id")
    private Product product;
<<Other fields>>
}
@Entity
@Data
public class Orders{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", nullable = false)
    private Long id;

    @ManyToOne
    @JoinColumn(name = "user_id")
    private UserInfo user;

   @OneToMany(mappedBy = "order",cascade = CascadeType.ALL,orphanRemoval = true)
    private List<OrderDetail> orderDetails = new ArrayList<>();

<<other fields>>
}
0

There are 0 best solutions below