8 Replies Latest reply on Apr 29, 2011 7:32 PM by GIOVANNI ROBERTO MACHADO

    Hibernate IllegalStateException

    Edem Morny Novice

      Hi.
      I'm trying to persist some changes to an already persistent entity as follows


      
       User user =(User) entityManager.createNamedQuery("User.findById").setParameter("id",loggedUser.getId()).getSingleResult();
              user.setEmail(email);
              entityManager.merge(user);       
              facesMessages.add("Email address changed");
      



      Here all the fields of the User entity are already populated. I'm just changing the email field and merging it again. But I get the following


      IllegalStateException: validation failed for com.aeminvestements.entity.User
      



      I've tried doing this in a conversation (maybe because there wasn't one previously, but doesn't make a difference).


      I'm using Seam 2.0.GA on Tomcat 6 and JDK 6. Any help will be welcome.



        • 1. Re: Hibernate IllegalStateException
          hasan Newbie

          Looks like a validation problem. Is your email valid ? What annotaions are u using in ur entity User to validate ur email?


          • 2. Re: Hibernate IllegalStateException
            Edem Morny Novice

            I used these two annotations


            @NotNull @Email



            I realised they could be validation annotation problems so i decided to comment them out. Unfortunately i'm still getting the same error.

            • 3. Re: Hibernate IllegalStateException
              Christian Douven Newbie

              Catch the IllegalStateException and drop its InvalidValues[] into FacesMessages and you will probably see whats going wrong

              • 4. Re: Hibernate IllegalStateException
                Edem Morny Novice

                I tried catching the exception as follows


                  User user =(User) entityManager.createNamedQuery("User.findById").setParameter("id",loggedUser.getId()).getSingleResult();
                       
                        user.setEmail(email);
                        try {
                            entityManager.merge(user);    
                        } catch (InvalidStateException e) {
                            e.printStackTrace();
                        }catch(Exception ex){
                            System.out.println("Exception thrown");
                        }
                      facesMessages.add("Email address changed");    
                



                The problem is that the exception is thrown AFTER this method call, not during it.


                Here is the stack trace. And sorry, the exception is InvalidStateException not IllegalStateException.


                SEVERE: uncaught exception
                java.lang.IllegalStateException: Could not commit transaction
                        at org.jboss.seam.jsf.SeamPhaseListener.commitOrRollback(SeamPhaseListener.java:592)
                        at org.jboss.seam.jsf.SeamPhaseListener.handleTransactionsAfterPhase(SeamPhaseListener.java:325)
                        at org.jboss.seam.jsf.SeamPhaseListener.afterServletPhase(SeamPhaseListener.java:226)
                        at org.jboss.seam.jsf.SeamPhaseListener.afterPhase(SeamPhaseListener.java:184)
                        at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:280)
                        at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
                        at javax.faces.webapp.FacesServlet.service(FacesServlet.java:244)
                        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
                        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                        at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:503)
                        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
                        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
                        at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:85)
                        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                        at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)
                        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                        at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:44)
                        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                        at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:147)
                        at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:256)
                        at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:362)
                        at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:488)
                        at org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:60)
                        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
                        at org.jboss.seam.debug.hot.HotDeployFilter.doFilter(HotDeployFilter.java:68)
                        at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.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(ApplicationFilterChain.java:206)
                        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
                        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
                        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
                        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
                        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
                        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
                        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
                        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
                        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
                        at java.lang.Thread.run(Thread.java:619)
                Caused by: javax.persistence.RollbackException: Error while commiting the transaction
                        at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:71)
                        at org.jboss.seam.transaction.EntityTransaction.commit(EntityTransaction.java:109)
                        at org.jboss.seam.jsf.SeamPhaseListener.commitOrRollback(SeamPhaseListener.java:582)
                        ... 39 more
                Caused by: org.hibernate.validator.InvalidStateException: validation failed for: com.aeminvestments.entity.User
                        at org.hibernate.validator.event.ValidateEventListener.validate(ValidateEventListener.java:148)
                        at org.hibernate.validator.event.ValidateEventListener.onPreUpdate(ValidateEventListener.java:177)
                        at org.hibernate.action.EntityUpdateAction.preUpdate(EntityUpdateAction.java:217)
                        at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:65)
                        at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
                        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:234)
                        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:142)
                        at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
                        at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
                        at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
                        at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
                        at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
                        at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:54)
                        ... 41 more
                



                • 5. Re: Hibernate IllegalStateException
                  Damian Harvey Apprentice

                  The entityManager is flushing at the method boundary. Try putting in an entityManager.flush() after the merge.


                  try {
                          entityManager.merge(user);
                          entityManager.flush();
                          
                  } catch (org.hibernate.validator.InvalidStateException e) {
                          InvalidValue[] invalidValues = e.getInvalidValues();
                          for(InvalidValue invalidValue : invalidValues) {
                              log.info("Invalid Value: #0", invalidValue.getPropertyName());
                          }
                  }
                  


                  Cheers,


                  Damian.

                  • 6. Re: Hibernate IllegalStateException
                    Edem Morny Novice

                    Thanks Damian!! Using the trick above helped me realise the problem.


                    It  was an issue of validation failing on some other fields of my User entity, not on the email field on which my whole focus was on.


                    Nice day.

                    • 7. Re: Hibernate IllegalStateException
                      Dean Hiller Expert

                      THANKS for the help!!!!   That worked great


                      Now if only they would get a bit smarter and put the list of invalid properties in the Exception message itself so we don't have to add code to figure stuff like this out!!!!!!!


                      thanks though for the hint.

                      • 8. Re: Hibernate IllegalStateException
                        GIOVANNI ROBERTO MACHADO Newbie

                        Great, Damian !!!


                        This code helped me a lot.


                        10x, man...


                        Btw, Dean, I think you're 100% right.