Tho*_*hor 4 persistence ejb entitymanager jpa-2.0 jboss7.x
我正处于从JBoss AS 6到JBoss AS 7的迁移过程中,并且我的测试有问题.我们假设一个简单的实体EJB:
@Entity public class MyTest implements Serializable
{
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@NotNull
private String headline;
} //getter/setter
Run Code Online (Sandbox Code Playgroud)
在我@Stateless Bean做这样的事情(就像之前的JBoss5和JBoss6):
@Inject private EntityManager em;
public <T extends Object> T persist(T o) throws MyContraintViolationException
{
System.out.println("***************** persist:");
try
{
em.persist(o);
}
catch (Exception e)
{
System.out.println("*************** exception:");
// Further investigation of Exception e,
// then throw MyContraintViolationException
}
}
Run Code Online (Sandbox Code Playgroud)
如果我不违反@NotNull约束,这可以正常工作.如果headline==null,我得到例外,但不要输入我的catch块:
12:19:45 INFO ******************** persist:
12:19:45 WARN [com.arjuna.ats.arjuna] (management-handler-threads - 2)
ARJUNA012125: TwoPhaseCoordinator.beforeCompletion - failed for
SynchronizationImple< 0:ffffc0a801fb:4f969a6e:4f058744:9,
org.hibernate.engine.transaction.synchronization.internal.
RegisteredSynchronization@38eb9b73 >: javax.persistence.PersistenceException:
error during managed flush
...
Caused by: javax.validation.ConstraintViolationException: Validation failed for
classes [my.test.MyTest] during persist time for groups
[javax.validation.groups.Default, ] List of constraint violations:[
ConstraintViolationImpl{interpolatedMessage='kann nicht null sein',
propertyPath=headline, rootBeanClass=class my.test.MyTest,
messageTemplate='{javax.validation.constraints.NotNull.message}'}
Run Code Online (Sandbox Code Playgroud)
我很高兴看到错误消息比以前的JBoss版本更详细,但我如何能够抓住javax.validation.ConstraintViolationException并抛出自己的错误MyContraintViolationException?即使在调试消息***** exception中不打印.
JB *_*zet 11
如果您读取了异常的消息和堆栈跟踪,您将看到对persist的调用不会抛出此异常,而是通过flush:
托管刷新期间出错
persist不会发出任何查询,也不会向数据库保存任何内容.它只是要求实体管理器使瞬态实体持久化.在冲洗时间(即刚刚提交的交易,或Hibernate的执行可能需要这个实体是在数据库中返回正确的结果,或在查询之前之前flush(),则约束检查,并插入查询被明确地调用)被执行.
您可以明确地调用flush,但它不会让Hibernate批量处理多个查询并仅在必要时执行它们,从而影响应用程序的性能.我只会使用本机异常.你为什么需要这样的转换?
| 归档时间: |
|
| 查看次数: |
11661 次 |
| 最近记录: |