我有一個包含以下欄位的新聞物體:
@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;
我的類別物體具有以下欄位:
@Id
@GeneratedValue
private long id;
private String name;
private int priority;
@LazyCollection(LazyCollectionOption.FALSE)
@ManyToMany
@JsonIgnore
private List<News> news;
private boolean visible;
我正在嘗試洗掉一個新聞條目而不洗掉連接的類別。
我的代碼如下所示:
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);
我試圖從新聞中分離所有類別。但是,當我執行此操作時,出現以下錯誤:
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
似乎這個類別仍然與新聞有關。我想知道如何
uj5u.com熱心網友回復:
執行這些陳述句后
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));
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/506416.html
標籤:爪哇 春天 PostgreSQL jpa 雅加达
下一篇:java.lang.StackOverflowError:com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFiel
