实体持久寄存器验证侦听器 [英] Entity persist register validation listener

查看:110
本文介绍了实体持久寄存器验证侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Seam中使用JBoss 6,并且我有一个我试图持久化的实体, User 。我已经扩展了 org.jboss.seam.framework.EntityHome 来提供一个 UserHome 类,并且已经覆盖了 persist()方法。我用javax.validation注释彻底注释了我的实体类。当我为我的实体类中的所有字段提供有效值时,该对象将保持没有问题。当我违反其中一个验证约束时,持久性按预期失败。



我遇到的问题是在异步持久期间违反约束:hibernate不告诉我哪个约束有已被违反!

  javax.validation.ConstraintViolationException:验证类失败[com.example.model.User] [javax.validation.groups.Default,] 
at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:132)
at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreInsert( BeanValidationEventListener.java:71)

如果我通过添加 EntityManager强制同步持久性。 () UserHome.persist(),然后我可以捕获 ConstraintViolationException 和输出包含违反约束的日志语句。



我的问题:是否有可能向hibernate / jboss注册一个侦听器来处理约束违规?我认为它有点跛脚,hibernate不打印违规,但希望我可以插入一些代码,将捕获并记录他们的更多信息。否则我不知道违反了什么限制。



谢谢!

解决方案

请检查这些链接,你会得到很好的信息:

validator-checkconstraints



additionalmodules


I'm using JBoss 6 with Seam, and I have an entity I am trying to persist, User. I have extended org.jboss.seam.framework.EntityHome to provide a UserHome class and have overridden the persist() method. I have thoroughly annotated my entity class with javax.validation annotations. When I provide valid values for all the fields in my entity class, the object is persisted with no problems. When I violate one of the validation constraints, persistence fails as expected.

The issue I am having is with constraint violations during asynchronous persistence: hibernate does not tell me which constraint has been violated!

javax.validation.ConstraintViolationException: validation failed for classes [com.example.model.User] duringpersist time for groups [javax.validation.groups.Default, ]
at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:132)
at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreInsert(BeanValidationEventListener.java:71)

If I force synchronous persistence by adding EntityManager.flush() to UserHome.persist(), then I can catch the ConstraintViolationException and output log statements containing the violated constraints.

My question: is it possible to register a listener with hibernate/jboss for the constraint violations? I think its a little lame that hibernate doesnt print the violations, but hopefully I can insert some code that will catch and log them for more info. Otherwise I have no idea what constraint was violated.

Thanks!

解决方案

Please check these links, you will get good information:

validator-checkconstraints

additionalmodules

这篇关于实体持久寄存器验证侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆