对象的JPA事务/回滚行为通过级联持久化

时间:2013-10-29 10:36:51

标签: jpa transactions eclipselink rollback

我有两个对象Antrag(应用程序)和Anlage(设施)。可以为多个设施申请。该应用程序直接保存在DAO中。设施通过级联保持。

@Entity
@Table(name = "EEG_ANTRAG")
public class Antrag implements Serializable {
  private static final long serialVersionUID = -2440344011443487714L;

  @Id
  @Column(name = "ANT_ID", nullable = false)
  @SequenceGenerator(name = "sequenceGeneratorAntrag", sequenceName = "EEG_ANTRAG_SEQ", allocationSize = 1)
  @GeneratedValue(generator = "sequenceGeneratorAntrag")
  @Getter @Setter private Long id;

  @OneToMany(mappedBy = "antrag", cascade = { CascadeType.ALL }, orphanRemoval = true)
  @OrderBy("id ASC")
  @Getter private List<Anlage> anlageList = new ArrayList<Anlage>();

  public Anlage addAnlage(Anlage anlage) 
    anlageList.add(anlage);
    anlage.setApplication(this);
    return anlage;
  }

  /* some more simple attributes; just Strings, boolean, .. */
}

@Entity
@Table(name = "EEG_ANLAGE")
public class Anlage implements Serializable {    
  private static final long serialVersionUID = -3940344011443487741L;

  @Id
  @Column(name = "ANL_ID")
  @SequenceGenerator(name = "sequenceGeneratorAnlage", sequenceName = "EEG_ANLAGE_SEQ", allocationSize = 1)
  @GeneratedValue(generator = "sequenceGeneratorAnlage")    
  @Getter @Setter private Long id;

  @ManyToOne
  @JoinColumn(name = "ANL_ANT_ID")
  @Getter @Setter private Antrag antrag;

 /* some more simple attributes; just Strings, boolean, .. */
}

@Stateless
public class AntragDaoBean implements AntragDaoLocal {
  @PersistenceContext(unitName = "ejb-model")
  private EntityManager em;

  @Override
  public void persistAntrag(Antrag antrag) {
    em.persist(antrag);
  }
}

插入设施时发生错误,例如某些列名在实体中拼写错误,抛出异常。 stacktrace表示已执行回滚。问题是,应用程序仍然存在。是否应该回滚应用程序的插入? 我们正在使用EclipseLink 2.4.1。 EclipseLink调试输出指出,所有插入都在一个事务中执行。该数据库是Oracle 11g。 我对交易行为的怀疑是错误的吗?我如何得到我想要的行为?

/* shortened exemplary stacktrace for rollback */
EvaluationException:
  javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101)
EJBTransactionRolledbackException:
  org.jboss.as.ejb3.tx.CMTTxInterceptor.handleEndTransactionException(CMTTxInterceptor.java:115)
RollbackException:
  com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple.commitAndDisassociate(TransactionImple.java:1177)
DatabaseException:
  org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:324)
SQLSyntaxErrorException:
  oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:445)

1 个答案:

答案 0 :(得分:8)

您的期望是正确的:所有内容都应该在一个事务中完成,Antrag的插入也应该回滚。

我认为你的持久性单元根本不是JTA:在persistence.xml文件中测试你有类似的东西:

<persistence-unit name="ejb-model" transaction-type="JTA">
<jta-data-source>java:/someNameDB</jta-data-source>