2010-11-01 186 views
13

即使在我的模型類中沒有驗證約束(所有成員變量都設置正確,但我仍然在創建對象時得到此異常),我得到此錯誤。我如何調試這個錯誤?什麼會導致Hibernate InvalidStateException

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(
+1

顯示用戶的實體可能會有幫助。 – 2010-11-01 10:46:59

回答

22

每個InvalidStateException一個數組InvalidValue。每個InvalidValue都顯示您想要堅持的bean的哪個屬性被違反。正如@Arthur指出的那樣,對Java持久性註釋的違反最終也會在InvalidStateException之間。

要調試你的問題,我會暫時保持在com.mycompany.action.UserHome.persistInvalidStateException並打印出每個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())); 
    } 
} 
+0

將給這個嘗試 – Sam 2010-11-02 03:38:21

+0

謝謝這幫助我找到錯誤(一個簡短的變量被設置爲一個錯誤條件的字符串值)。非常感謝 – Sam 2010-11-02 06:46:58

1

如果你想輕鬆地調試這一點,您可以生成/實行toString()方法,並打印你在沖洗前的對象。

這樣你就可以看到你的一些屬性是否丟失或者有一些驗證錯誤。

+0

我確實打印了對象,對於所有成員正確設置,我看起來都很好。 – Sam 2010-11-02 03:37:41

+0

然後請向我們展示用戶實體,以及對象的打印輸出 – 2010-11-02 10:22:51

3

我敢肯定它是由默認註冊的偵聽

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

剛剛成立的hibernate.validator.autoregister_listeners造成屬性爲假

hibernate.validator.autoregister_listeners=false 
+0

是的,(hibernate)驗證程序已註冊爲偵聽程序。但爲什麼它會拒絕沒有任何驗證約束的實體? – 2010-11-01 15:25:24

+0

@Pascal Thivent你好帕斯卡,即使你不使用普通的HibernateValidator註解,它可以驗證你的實體,如果你有像* @列(長度= 10)*,例如。你可以通過使用* session.persist(yourEntity)* – 2010-11-01 16:13:34

+0

呃,非常好點! – 2010-11-01 16:27:23

相關問題