通过级联保留对象的JPA事务/回滚行为 [英] JPA transaction/rollback behaviour with objects persisted via cascade

查看:98
本文介绍了通过级联保留对象的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);
  }
}

在插入设施时发生错误时,例如实体中某些列名称拼写错误,将引发异常. stacktrace指示已执行回滚.问题是,该应用程序仍然存在.是否也应回滚插入应用程序? 我们正在使用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天全站免登陆