エンティティを更新しようとして、以下のエラーが発生しました。この問題の解決策は、所有者を永続化する前に「所有された」エンティティを永続化する必要があると言いますが、私はそうしました。または、両方のエンティティをカスケードでマークします。
エラー(完全なスタックトレースは必要ないと思います):
2015-08-10T20:45:11.481+0200|Warning: A system exception occurred during an invocation on EJB ThreadLookUpService, method: public void main.java.services.ThreadLookUpService.setLastPost(long,main.java.entities.Post)
Caused by: java.lang.IllegalStateException: During synchronization a new object was found through a relationship that was not marked cascade PERSIST: main.java.entities.Post@6724f919.
(追加するcascade = CascadeType.ALL
と、外部キーが存在しないという別のエラーが表示されますが、それを永続化しただけです。理解する限り、エンティティを正しい順序で永続化する場合、カスケードを使用する必要さえありません。
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`forumcs`.`thethreads`, CONSTRAINT `fk_thethreads_posts1` FOREIGN KEY (`last_post`) REFERENCES `posts` (`idpost`) ON DELETE NO ACTION ON UPDATE NO ACTION)
)
ここでは、投稿 (所有) を永続化し、次にスレッド (所有者) を永続化します。
post = postLookup.createPostAndGiveItBack(post);
threadLookup.setLastPost(thread.getThread().getIdthread(), post); //this gives error.
投稿を永続化する方法は次のとおりです。投稿 ID が生成されていないか、smtg ではないと考えたため、フラッシュを使用しました。
@Override
public Post createPostAndGiveItBack(Post post) {
em.persist(post);
em.flush();
return post;
}
Thethread の lastPost を更新する方法は次のとおりです。以前に引き出したものと直接マージすることもできますが、そうすると、2人の異なるユーザーが同じページを見てスレッドを更新すると、賛成票と反対票のフィールドが台無しになります。
@Override
public void setLastPost(long id, Post post) {
Query query = em.createQuery(FIND_THREAD_BY_ID);
query.setParameter("id", id);
Thethread thread = (Thethread) query.getSingleResult();
thread.setLastPost(post);
}
投稿エンティティ:
@Entity
@Table(name = "posts")
@NamedQuery(name = "Post.findAll", query = "SELECT p FROM Post p")
public class Post implements Serializable, Comparable<Post> {
private static final long serialVersionUID = 1L;
@Id
private int idpost;
private String content;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "date_post")
private Date datePost;
private int downvotes;
private int upvotes;
// bi-directional many-to-one association to PostVote
@OneToMany(mappedBy = "post")
private List<PostVote> postVotes;
// bi-directional many-to-one association to Post
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "reply_to_post")
private Post replyTo;
// bi-directional many-to-one association to Post
@OneToMany(mappedBy = "replyTo")
private List<Post> replies;
// bi-directional many-to-one association to Thethread
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "threads_idthread")
private Thethread thethread;
// bi-directional many-to-one association to User
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "users_username")
private User user;
@Transient
private double confidenceScore;
public Post() {
}
スレッド エンティティ:
@Entity
@Table(name = "thethreads")
@NamedQuery(name = "Thethread.findAll", query = "SELECT t FROM Thethread t")
public class Thethread implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private int idthread;
private String content;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "date_posted")
private Date datePosted;
// I could get this by scanning the Threadvotes and count the number of downvotes
// But I thought it was better like this for ease and performance.
private int downvotes;
@Column(name = "hot_score")
private int hotScore;
@Column(name = "is_pol")
private String isPol;
private String title;
private String type;
private int upvotes;
// bi-directional many-to-one association to Post
@OneToMany(mappedBy = "thethread")
private List<Post> posts;
// bi-directional many-to-one association to Category
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "categories_idcategory")
private Category category;
// UNI-directional many-to-one association to Post
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "last_post")
private Post lastPost;
// bi-directional many-to-one association to User
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "posted_by")
private User user;
// bi-directional many-to-one association to ThreadVote
@OneToMany(mappedBy = "thethread")
private List<ThreadVote> threadVotes;