まず、ここにコードがあります
@Entity
@SequenceGenerator(name = "wordlist_seq", sequenceName = "wordlist_seq")
public class QuestionSet {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "wordlist_seq")
private Long id;
@Constraints.Required
private String name;
@Constraints.Required
private String questionType;
@Constraints.Required
@ManyToOne
@PrimaryKeyJoinColumn
private User owner;
public String getQuestionType() {
return questionType;
}
public void setQuestionType(String questionType) {
this.questionType = questionType;
}
@OneToMany(cascade = CascadeType.ALL)
private List<Question> questions;
....
ここにいくつかのサンプル操作があります
@SecureSocial.SecuredAction
@play.db.jpa.Transactional
public static Result test(){
QuestionSet set = questionSetDAO.findByID(QuestionSet.class, 100L);
List<Question> questions = new ArrayList<Question>();
OpenQuestion question = new OpenQuestion();
question.setQuestion("a");
question.setAnswer("b");
questions.add(question);
set.setQuestions(questions);
questionSetDAO.merge(set);
return ok();
}
QuestionSet
ここで、データベースからフェッチしているクラスのインスタンスを更新するときに、質問の新しいリストを作成します(IDが設定されていない新しい質問が含まれている場合があり、既存のIDを持つ質問が含まれている場合があります-それらは更新用です)。
ここで、エンティティ マネージャーのメソッドを呼び出すと、merge
次の例外が発生します。
play.api.Application$$anon$1: Execution exception[[RollbackException: Error while committing the transaction]]
at play.api.Application$class.handleError(Application.scala:289) ~[play_2.10.jar:2.1.1]
at play.api.DefaultApplication.handleError(Application.scala:383) [play_2.10.jar:2.1.1]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anon$2$$anonfun$handle$1.apply(PlayDefaultUpstreamHandler.scala:144) [play_2.10.jar:2.1.1]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anon$2$$anonfun$handle$1.apply(PlayDefaultUpstreamHandler.scala:140) [play_2.10.jar:2.1.1]
at play.api.libs.concurrent.PlayPromise$$anonfun$extend1$1.apply(Promise.scala:113) [play_2.10.jar:2.1.1]
at play.api.libs.concurrent.PlayPromise$$anonfun$extend1$1.apply(Promise.scala:113) [play_2.10.jar:2.1.1]
javax.persistence.RollbackException: Error while committing the transaction
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:93) ~[hibernate-entitymanager-3.6.9.Final.jar:3.6.9.Final]
at play.db.jpa.JPA.withTransaction(JPA.java:107) ~[play-java-jpa_2.10.jar:2.1.1]
at play.db.jpa.TransactionalAction.call(TransactionalAction.java:14) ~[play-java-jpa_2.10.jar:2.1.1]
at securesocial.core.java.SecureSocial$Secured.call(SecureSocial.java:218) ~[securesocial_2.10-master-SNAPSHOT.jar:master-SNAPSHOT]
at play.core.j.JavaAction$$anon$2.apply(JavaAction.scala:74) ~[play_2.10.jar:2.1.1]
at play.core.j.JavaAction$$anon$2.apply(JavaAction.scala:73) ~[play_2.10.jar:2.1.1]
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1387) ~[hibernate-entitymanager-3.6.9.Final.jar:3.6.9.Final]
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1315) ~[hibernate-entitymanager-3.6.9.Final.jar:3.6.9.Final]
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:81) ~[hibernate-entitymanager-3.6.9.Final.jar:3.6.9.Final]
at play.db.jpa.JPA.withTransaction(JPA.java:107) ~[play-java-jpa_2.10.jar:2.1.1]
at play.db.jpa.TransactionalAction.call(TransactionalAction.java:14) ~[play-java-jpa_2.10.jar:2.1.1]
at securesocial.core.java.SecureSocial$Secured.call(SecureSocial.java:218) ~[securesocial_2.10-master-SNAPSHOT.jar:master-SNAPSHOT]
Caused by: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:188) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
Caused by: org.h2.jdbc.JdbcBatchUpdateException: Referential integrity constraint violation: "FK415AFE694BC8D057: PUBLIC.QUESTIONSET_QUESTION FOREIGN KEY(QUESTIONS_ID) REFERENCES PUBLIC.QUESTION(ID) (200)"; SQL statement:
insert into QuestionSet_Question (QuestionSet_id, questions_id) values (?, ?) [23506-168]
at org.h2.jdbc.JdbcPreparedStatement.executeBatch(JdbcPreparedStatement.java:1121) ~[h2.jar:1.3.168]
at com.jolbox.bonecp.StatementHandle.executeBatch(StatementHandle.java:469) ~[bonecp.jar:0.7.1.RELEASE]
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:188) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
私の簡単な操作例では、質問セットには最初に質問が含まれていないため、リストは空です。
休止状態が最初に注釈によって作成された結合テーブルに質問を挿入しようとしているように見えOneToMany
ますが、参照されたエンティティがまだデータベースにないため、エラーが発生していますが、@OneToMany(cascade = CascadeType.ALL)
このようなことを処理すると思いましたか?