什么会导致Hibernate InvalidStateException

时间:2022-10-10 19:59:06

I get this error, even though there are no validation constraints in my model class (all of the member variables are set properly but I stil get this exception on object creation). How do I debug this error?

我得到了这个错误,即使我的模型类中没有验证约束(所有成员变量都设置正确,但我仍然在创建对象时获得此异常)。如何调试此错误?

javax.el.ELException: org.hibernate.validator.InvalidStateException: valida
tion failed for: com.mycompany.model.User
        at org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.jav
a:339)
        at org.jboss.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.jav
a:280)
        at org.jboss.el.parser.AstMethodSuffix.getValue(AstMethodSuffix.jav
a:59)
        at org.jboss.el.parser.AstMethodSuffix.invoke(AstMethodSuffix.java:
65)
        at org.jboss.el.parser.AstValue.invoke(AstValue.java:96)
        at org.jboss.el.MethodExpressionImpl.invoke(MethodExpressionImpl.ja
va:276)
        at org.jboss.seam.core.Expressions$2.invoke(Expressions.java:175)
        at org.jboss.seam.navigation.Pages.callAction(Pages.java:703)
        at org.jboss.seam.navigation.Pages.preRender(Pages.java:331)
        at org.jboss.seam.jsf.SeamPhaseListener.preRenderPage(SeamPhaseList
ener.java:561)
        at org.jboss.seam.jsf.SeamPhaseListener.beforeRenderResponse(SeamPh
aseListener.java:472)
        at org.jboss.seam.jsf.SeamPhaseListener.beforeServletPhase(SeamPhas
eListener.java:148)
        at org.jboss.seam.jsf.SeamPhaseListener.beforePhase(SeamPhaseListen
er.java:118)
        at com.sun.faces.lifecycle.Phase.handleBeforePhase(Phase.java:214)
        at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:96)
        at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:
139)
        at javax.faces.webapp.FacesServlet.service(FacesServlet.java:266)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter
(ApplicationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(Applica
tionFilterChain.java:206)
        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamF
ilter.java:83)
        at org.jboss.seam.web.RewriteFilter.doFilter(RewriteFilter.java:63)

        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamF
ilter.java:69)
        at org.jboss.seam.web.IdentityFilter.doFilter(IdentityFilter.java:4
0)
        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamF
ilter.java:69)
        at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java
:90)
        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamF
ilter.java:69)
        at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java
:64)
        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamF
ilter.java:69)
        at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:4
5)
        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamF
ilter.java:69)
        at org.jboss.seam.web.CharacterEncodingFilter.doFilter(CharacterEnc
odingFilter.java:42)
        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamF
ilter.java:69)
        at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java
:206)
        at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290
)
        at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(Ba
seFilter.java:388)
        at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:515)
        at org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:5
6)
        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamF
ilter.java:69)
        at org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:60)

        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamF
ilter.java:69)
        at org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter
(ApplicationFilterChain.java:235)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(Applica
tionFilterChain.java:206)
        at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHea
derFilter.java:96)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter
(ApplicationFilterChain.java:235)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(Applica
tionFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWra
pperValve.java:235)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardCon
textValve.java:191)
        at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(Se
curityAssociationValve.java:190)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(Authe
nticatorBase.java:433)
        at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContex
tValve.java:92)
        at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.
process(SecurityContextEstablishmentValve.java:126)
        at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.
invoke(SecurityContextEstablishmentValve.java:70)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostVa
lve.java:127)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportVa
lve.java:102)
        at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(Ca
chedConnectionValve.java:158)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngi
neValve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapte
r.java:330)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor
.java:829)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.
process(Http11Protocol.java:598)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.ja
va:447)
        at java.lang.Thread.run(Thread.java:619)
Caused by: org.hibernate.validator.InvalidStateException: validation failed
 for: com.mycompany.model.User            at org.hibernate.validator.event.ValidateEventListener.validate(Val
idateEventListener.java:148)
        at org.hibernate.validator.event.ValidateEventListener.onPreInsert(
ValidateEventListener.java:172)
        at org.hibernate.action.EntityIdentityInsertAction.preInsert(Entity
IdentityInsertAction.java:142)
        at org.hibernate.action.EntityIdentityInsertAction.execute(EntityId
entityInsertAction.java:65)
        at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
        at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrR
eplicate(AbstractSaveEventListener.java:321)
        at org.hibernate.event.def.AbstractSaveEventListener.performSave(Ab
stractSaveEventListener.java:204)
        at org.hibernate.event.def.AbstractSaveEventListener.saveWithGenera
tedId(AbstractSaveEventListener.java:130)
        at org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGenerat
edId(EJB3PersistEventListener.java:49)
        at org.hibernate.event.def.DefaultPersistEventListener.entityIsTran
sient(DefaultPersistEventListener.java:154)
        at org.hibernate.event.def.DefaultPersistEventListener.onPersist(De
faultPersistEventListener.java:110)
        at org.hibernate.event.def.DefaultPersistEventListener.onPersist(De
faultPersistEventListener.java:61)
        at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:645)

        at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:619)
        at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:623)
        at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEnti
tyManagerImpl.java:220)
        at sun.reflect.GeneratedMethodAccessor454.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethod
AccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.jboss.seam.persistence.EntityManagerInvocationHandler.invoke
(EntityManagerInvocationHandler.java:46)
        at $Proxy550.persist(Unknown Source)
        at org.jboss.seam.framework.EntityHome.persist(EntityHome.java:84)
        at com.mycompany.action.UserHome.persist(

3 个解决方案

#1


22  

Each InvalidStateException has an array of InvalidValue. Each InvalidValue shows which property of the bean you want to persist is violated. As @Arthur pointed out correctly, violations of Java persistence annotations end up in InvalidStateExceptions, too.

每个InvalidStateException都有一个InvalidValue数组。每个InvalidValue都会显示您要保留的bean的哪个属性被违反。正如@Arthur正确指出的那样,违反Java持久性注释也会在InvalidStateExceptions中出现。

To debug your problem, I would temporarily catch the InvalidStateException in com.mycompany.action.UserHome.persist and print out each InvalidValue as follows:

要调试您的问题,我会暂时捕获com.mycompany.action.UserHome.persist中的InvalidStateException并打印出每个InvalidValue,如下所示:

} catch (InvalidStateException e) {
    for (InvalidValue invalidValue : e.getInvalidValues()) {
        log.info("Instance of bean class: " + invalidValue.getBeanClass().getSimpleName() +
                 " has an invalid property: " + invalidValue.getPropertyName() +
                 " with message: " + invalidValue.getMessage()));
    }
}

#2


3  

I am pretty sure it is caused by a listener registered by default

我很确定它是由默认注册的监听器引起的

at org.hibernate...ValidateEventListener.onPreInsert(ValidateEventListener.java)
at org.hibernate...EntityIdentityInsertAction.preInsert(EntityIdentityInsertAction.java)

Just set up the hibernate.validator.autoregister_listeners property as false

只需将hibernate.validator.autoregister_listeners属性设置为false即可

hibernate.validator.autoregister_listeners=false

#3


1  

If you want to easily debug this, you can generate/implement a toString() method, and print the object before you are flushing.

如果要轻松调试它,可以生成/实现toString()方法,并在刷新之前打印对象。

This way you can see if some of your properties are missing, or have some validation errors.

这样,您可以查看是否缺少某些属性,或者是否存在某些验证错误。

#1


22  

Each InvalidStateException has an array of InvalidValue. Each InvalidValue shows which property of the bean you want to persist is violated. As @Arthur pointed out correctly, violations of Java persistence annotations end up in InvalidStateExceptions, too.

每个InvalidStateException都有一个InvalidValue数组。每个InvalidValue都会显示您要保留的bean的哪个属性被违反。正如@Arthur正确指出的那样,违反Java持久性注释也会在InvalidStateExceptions中出现。

To debug your problem, I would temporarily catch the InvalidStateException in com.mycompany.action.UserHome.persist and print out each InvalidValue as follows:

要调试您的问题,我会暂时捕获com.mycompany.action.UserHome.persist中的InvalidStateException并打印出每个InvalidValue,如下所示:

} catch (InvalidStateException e) {
    for (InvalidValue invalidValue : e.getInvalidValues()) {
        log.info("Instance of bean class: " + invalidValue.getBeanClass().getSimpleName() +
                 " has an invalid property: " + invalidValue.getPropertyName() +
                 " with message: " + invalidValue.getMessage()));
    }
}

#2


3  

I am pretty sure it is caused by a listener registered by default

我很确定它是由默认注册的监听器引起的

at org.hibernate...ValidateEventListener.onPreInsert(ValidateEventListener.java)
at org.hibernate...EntityIdentityInsertAction.preInsert(EntityIdentityInsertAction.java)

Just set up the hibernate.validator.autoregister_listeners property as false

只需将hibernate.validator.autoregister_listeners属性设置为false即可

hibernate.validator.autoregister_listeners=false

#3


1  

If you want to easily debug this, you can generate/implement a toString() method, and print the object before you are flushing.

如果要轻松调试它,可以生成/实现toString()方法,并在刷新之前打印对象。

This way you can see if some of your properties are missing, or have some validation errors.

这样,您可以查看是否缺少某些属性,或者是否存在某些验证错误。