简体   繁体   中英

Hibernate 4 remove entity with oneToMany collection

I'm trying to delete User entity from DB

//whatever
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "user", orphanRemoval = true)
    private Set<Filter> filters = new HashSet<Filter>();
//whatever

//whatever
@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.DETACH})
@JoinColumn(name = "user_id", nullable = false)
private User user;
//whatever

but getting: ERROR SqlExceptionHelper:146 - Cannot delete or update a parent row: a foreign key constraint fails ( web_app_db . filters , CONSTRAINT FK_8oeay5yddrcgd4i71m1yda1hb FOREIGN KEY ( user_id ) REFERENCES users ( user_id ))

orphanRemoval = true not working, what should I do?

Try this:

@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
@JoinColumn(name = "user_id", nullable = false)
private User user;

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