JPA 事务/回滚行为,对象通过级联持久化 [英] JPA transaction/rollback behaviour with objects persisted via cascade

查看:15
本文介绍了JPA 事务/回滚行为,对象通过级联持久化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two objects Antrag (application) and Anlage (facility). An application can be made for multiple facilities. The application is persisted directly in the DAO. The facilities are persisted via cascade.

@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);
  }
}

当插入设施时发生错误时,例如实体中的某些列名称拼写错误,会引发异常.堆栈跟踪指示已执行回滚.问题是,应用程序仍然存在.应用程序的插入不应该也回滚吗?我们正在使用 EclipseLink 2.4.1.EclipseLink 调试输出指出,所有插入都在一个事务中执行.数据库是Oracle 11g.我对交易行为的看法是错误的吗?我如何获得我想要的行为?

When an error occurs on inserting the facilities, e.g. some column name is misspelled in the entity, an exception is thrown. The stacktrace indicates, that a rollback was performed. The problem is, that the application is still persisted. Shouldn't the insertion of the application be rolled back as well? We are using EclipseLink 2.4.1. The EclipseLink debug output states, that all inserts are performed in one single transaction. The database is Oracle 11g. Is my ecpectation of the transactional behaviour wrong? How do I get the behaviour I want?

/* 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)

推荐答案

您的期望是正确的:一切都应该在一个事务中完成,并且 Antrag 的插入也应该回滚.

Your expectation is correct: everything should be made in a single transaction, and the insertion of Antrag should be rolled back as well.

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

I think your persistence-unit is simply not JTA: test in the persistence.xml file that you have something like:

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

这篇关于JPA 事务/回滚行为,对象通过级联持久化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆