English 中文(简体)
Hibernate Validator Exceptions
原标题:

I m using Hibernate Validator in an application with EJB and Spring MVC. I m using JBoss 5, Hibernate 3 and Spring MVC version 3.

I d like to try and catch validation exceptions at Spring Exception Resolver level and generate an error message based on the InvalidStateException message. I don t want to put exception handling logic in the data access layer; I want this to be driven by annotations and just handle the validation errors in one centralized place.

The domain model is being correctly verified and an exception of type InvalidStateException is thrown as expected when I try to create an invalid entity (e.g. violate a length constraint on a field). At the point of trying to catch the exception in my instance of a Spring Exception resolver, I find my original exception has disappeared and a javax.ejb.EJBTransactionRolledbackException has taken its place. The original exception is not in the caused by list.

My first guess was that org.hibernate.validator.InvalidStateException wasn t annotated with ApplicationException, so I installed a Hibernate event listener, caught the original exception and rethrew it as a new exception annotated with ApplicationException. This has no effect.

One further complexity is that the web tier is calling EJBs via a Remote interface. I annotated my exception class with WebFault, but to no avail.

What should I do to ensure the exception bubbles all the way up?

最佳回答

Try this?

protected InvalidStateException extractValidationException(Throwable ex) { Throwable e = ex; while (e != null) { if (e instanceof InvalidStateException) { return (ValidationException) e; } else if (e instanceof EJBException) { e = ((EJBException) e).getCausedByException(); } else { e = e.getCause(); } } return null; }

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签