实体经理异常 [英] Exception not cuaght with Entity Manager

查看:136
本文介绍了实体经理异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的EJB中有一个实体管理器

  @PersistenceContext(unitName =cnsbEntities)
private EntityManager EM;

我填充一个对象,然后我将其提交到我的数据库,但如果我有一个例外,重复的ID,我无法抓住它,我不知道为什么。

  try {
em.merge (boelLog);
} catch(Exception e){
System.out.println(Generic Exception);
}


解决方案

JPA使用事务发送实体修改数据库。您可以通过Bean管理事务(BMT)手动指定这些事务,或让应用程序服务器为您(Container Managed Transactions; Container Managed Transactions;默认值)。



所以,您需要在事务结束时捕获异常,而不是调用 merge() persist() EntityManager 类。在您的情况下,当您从最后一个EJB对象返回时,事务可能会结束。



容器管理事务示例(默认值):

  @Stateless 
public class OneEjbClass {
@Inject
private MyPersistenceEJB persistenceEJB;

public void someMethod(){
try {
persistenceEJB.persistAnEntity();
} catch(PersistenceException e){
//这里你可以捕获持久异常!
}
}
}

...

@Stateless
public class MyPersistenceEJB {
//此注释强制应用程序服务器在调用此方法时创建一个新的
//事务,并在其末尾提交所有
//修改!
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void persistAnEntity(){
//与EntityManager合并
}
}

可以指定方法调用(或EJB对象的任何方法调用)是否必须可以创建新事务。这是通过 @TransactionAttribute 注释完成的。默认情况下,EJB的每个方法都配置为 REQUIRED (与指定 @TransactionAttribute(TransactionAttributeType.REQUIRED))相同) ,它告诉应用程序在调用该方法时重用(继续)活动的事务,并在需要时创建一个新的事务。



有关此处的事务的更多信息: a href =http://docs.oracle.com/javaee/7/tutorial/doc/transactions.htm#BNCIH =nofollow> http://docs.oracle.com/javaee/7/tutorial/ doc / transactions.htm#BNCIH



有关JPA和JTA的更多信息,请访问: http://en.wikibooks.org/wiki/Java_Persistence/Transactions


I have an Entity Manager in my EJB

@PersistenceContext(unitName = "cnsbEntities")
private EntityManager em;

I populate an object and then I commit it in my DB, but if I have an exception, for duplicate ID, I can't catch it and I don't know why.

    try{
      em.merge(boelLog);
    } catch (Exception e){
        System.out.println("Generic Exception");
    }

解决方案

JPA uses transactions to send entity modifications to the database. You can specify those transactions manually through Bean Managed Transactions (BMT) or let the application server do it for you (Container Managed Transactions; the default).

So, you need to catch the exception at the end of the transaction, and not after calling merge() or persist() methods of EntityManager class. In your case, probably the transaction will end when you return from the last EJB object.

Example for Container Managed Transactions (the default):

 @Stateless
 public class OneEjbClass {
      @Inject
      private MyPersistenceEJB persistenceEJB;

      public void someMethod() {
           try {
                persistenceEJB.persistAnEntity();
           } catch(PersistenceException e) {
                // here you can catch persistence exceptions!
           }
      }
 }

 ...

 @Stateless
 public class MyPersistenceEJB {
      // this annotation forces application server to create a new  
      // transaction when calling this method, and to commit all 
      // modifications at the end of it!
      @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) 
      public void persistAnEntity() {
            // merge stuff with EntityManager
      }
 }

It's possible to specify when a method call (or any method call of an object of an EJB) must, can or must not create a new transaction. This is done through the @TransactionAttribute annotation. By default, every method of an EJB is configured as REQUIRED (same as specifying @TransactionAttribute(TransactionAttributeType.REQUIRED)), that tells the application to reuse (continue) the transaction that is active when that method was called, and create a new transaction if needed.

More about transactions here: http://docs.oracle.com/javaee/7/tutorial/doc/transactions.htm#BNCIH

More about JPA and JTA here: http://en.wikibooks.org/wiki/Java_Persistence/Transactions

这篇关于实体经理异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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