简体   繁体   中英

How to delete entity with many to many relation? JPA

i don't know how to delete this. Only one thing i noticed that the OrderToProvider will be deleted after deleting all entities in a table named "OrderToProvider-Goods".

Code:

OrderToProvider

public class OrderToProvider {
@GeneratedValue (strategy = GenerationType.IDENTITY)
@Id
private int id;
@Basic
private int price;
@Basic
private Date dataOfOrder;
@Basic
private Date dateOfProcessing;
@ManyToOne(optional = false)
private main.data.Provider provider;
@ManyToMany (cascade = CascadeType.ALL)
private Collection<Good> goods;

Good

public class Good implements Comparable<Good>{
    @GeneratedValue (strategy = GenerationType.IDENTITY)
    @Id
    private int id;
    @Basic
    private String name;
    @Basic
    private String model;
    @Basic
    private int price;
    @Basic
    private String type;
    @Basic
    private int amount;
    @ManyToOne(optional = false)
    private Provider provider;

To delete the OrderToProvider from your DB, first you retrieve it from your DB, then setGoods(null) and setProvider(null) , then delete it afterwards. This should work with conditions that you have a properly implemented setters .

I hope this is helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM