1

首先,这是代码

@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]

在我的简单示例操作中,问题集开头不包含任何问题,因此列表为空。

似乎hibernate首先尝试向OneToMany注释创建的连接表插入问题,但由于引用的实体尚未在数据库中,所以出现错误,但我认为这@OneToMany(cascade = CascadeType.ALL)会处理这种事情?

4

1 回答 1

1

我正在回答我自己的问题,因为我发现我有一些以前的注释的剩余部分,所以这个约束不应该存在于我当前的模型中。我清除了数据库,现在一切正常。

于 2013-04-28T14:54:39.233 回答