Hibernate 验证器异常
我在带有 EJB 和 Spring MVC 的应用程序中使用 Hibernate Validator。我正在使用 JBoss 5、Hibernate 3 和 Spring MVC 版本 3。
我想尝试在 Spring 异常解析器级别捕获验证异常,并根据 InvalidStateException
消息生成错误消息。我不想将异常处理逻辑放在数据访问层;我希望这是由注释驱动的,并且只在一个集中的地方处理验证错误。
当我尝试创建无效实体(例如,违反字段的长度约束)时,域模型正在正确验证,并且会按预期抛出 InvalidStateException
类型的异常。在尝试捕获 Spring 异常解析器实例中的异常时,我发现原来的异常已经消失,并且 javax.ejb.EJBTransactionRolledbackException 已取代它。原始异常不在引起列表中。
我的第一个猜测是 org.hibernate.validator.InvalidStateException
没有用 ApplicationException
注释,因此我安装了一个 Hibernate 事件监听器,捕获了原始异常并将其重新抛出为用 ApplicationException
注释的新异常。这没有效果。
另一个复杂性是 Web 层通过远程接口调用 EJB。我用 WebFault
注释了我的异常类,但没有效果。
我应该怎么做才能确保异常一直向上冒泡?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个?
<代码>
Try this?