When I use Hibernate Validator (3.1.0 + 3.3.1 Core) with Spring (2.5.6) I can't catch the InvalidStateException directly. Spring throws a JpaSystemException, whose cause is a RollBackException, whose cause is finally the InvalidStateException. The same happens with PersistenceException and IllegalArgumentException at methods that I have implemented, like the following example:
DAO:Code:@PrePersist @PreUpdate protected void prePersist(StReservaVeiculo reserva) { if(reserva.getKmInicial() != null && reserva.getKmFinal() != null && reserva.getKmInicial() > reserva.getKmFinal()) { throw new PersistenceException("KM inicial maior que KM final"); } }
If add a call to EntityManager's flush() after merge(), I get the expected result, so I can catch the exceptions normally.Code:@Transactional(readOnly = false, propagation = Propagation.REQUIRED) public void persist(T t) { em.merge(t); }
Is this the best approach?
Thanks,
Felipe


Reply With Quote