I have a News entity that has the following fields:
@ManyToMany(mappedBy = "news")
@LazyCollection(LazyCollectionOption.FALSE)
private List<Category> categories;
@LazyCollection(LazyCollectionOption.FALSE)
@OneToMany(mappedBy = "news", cascade = CascadeType.REMOVE)
@JsonIgnore
private List<Comment> comments;
@ManyToMany(mappedBy = "newsVoted")
@JsonIgnore
private Collection<Session> sessionsVoted;
My Category entity has those fields:
@Id
@GeneratedValue
private long id;
private String name;
private int priority;
@LazyCollection(LazyCollectionOption.FALSE)
@ManyToMany
@JsonIgnore
private List<News> news;
private boolean visible;
I am trying to delete one News entry without removing connected Categories.
My code looks like this:
news.setComments(new ArrayList<Comment>());
news.setCategories(new ArrayList<Category>());
news.setSessionsVoted(new ArrayList<Session>());
news.getComments().forEach(comment -> comment.setNews(null));
news.getCategories().forEach(category -> category.setNews(null));
news.getSessionsVoted().forEach(session -> session.setNewsVoted(null));
newsFacade.save(news);
news.getComments().forEach(comment -> commentFacade.save(comment));
news.getCategories().forEach(category -> categoryFacade.save(category));
news.getSessionsVoted().forEach(session -> sessionFacade.save(session));
em.flush();
newsFacade.delete(news);
I tried to detach all Categories from News. However, when I execute this, I get the following Error:
Caused by: org.postgresql.util.PSQLException: ERROR: update or delete on table "news" violates foreign key constraint "fkhik32gpatwh82fuds49l7goo3" on table "category_news"
Detail: Key (id)=(5) is still referenced from table "category_news".
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2505)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2241)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:310)
at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:447)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:368)
at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:158)
at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:124)
at sun.reflect.GeneratedMethodAccessor82.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.postgresql.ds.PGPooledConnection$StatementHandler.invoke(PGPooledConnection.java:428)
at com.sun.proxy.$Proxy132.executeUpdate(Unknown Source)
at org.jboss.jca.adapters.jdbc.WrappedPreparedStatement.executeUpdate(WrappedPreparedStatement.java:537)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:175)
... 67 more
It seems like this category is still conected to the news. I wonder how
CodePudding user response:
After executing these statements
news.setComments(new ArrayList<Comment>());
news.setCategories(new ArrayList<Category>());
news.setSessionsVoted(new ArrayList<Session>());
aren't these statements just iterating over empty lists?
news.getComments().forEach(comment -> comment.setNews(null));
news.getCategories().forEach(category -> category.setNews(null));
news.getSessionsVoted().forEach(session -> session.setNewsVoted(null));