我正在從JBoss AS 6遷移到JBoss AS 7,並且遇到了我的測試問題。讓我們假設一個簡單的實體EJB:JBoss AS 7中的Catch PersistenceException或ConstraintViolationException
@Entity public class MyTest implements Serializable
{
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@NotNull
private String headline;
} //getter/setter
在我@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
}
}
如果我沒有違反這工作得很好@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.
[email protected] >: 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}'}
我很高興地看到,該錯誤信息是更詳細的比在JBoss中的早期版本,但我怎麼能趕上javax.validation.ConstraintViolationException
並拋出我自己的MyContraintViolationException
?即使調試消息***** exception
是也不是已打印。