1 2 3 Previous Next 31 Replies Latest reply on Jan 24, 2013 5:42 PM by strenkor Go to original post
      • 15. Re: Entity Converter - "value is not valid"

        Hi Vladimir,
        my situation is exactly the same. I am using only one Seam-managed persistence context, and I overrided equals (comparing Long ids) and hashcode (returning hashcode of Long id).


        it's really strange because for some of the items the validation error value is not valid ocurrs, but not for others (in the same select!!)


        I know that with


        inmediate=true



        I can bypass the validation process, so the validation error doesn't occur (I tested it). But sometimes I can't use this trick.


        Did you find some information about this problem?



        Thanks in advance!

        • 16. Re: Entity Converter - "value is not valid"
          vladimir.kovalyuk

          Leo, The cause of the problem was relatively simple - equals() method was called for entity instances that was supposed to be lazily initialized but actually hadn't been yet. Since I compared entities by id in the manner like


          this.id.equals(that.id)



          the result of the comparison depended on the JPA lazy initialization.
          I changed equals() to compare properties which are intercepted by JPA instead of fields (it actually depends on what field or property are annotated @Id):


          this.getId().equals(that.getId())



          and it get worked fine. That's all.
          Good luck!

          • 17. Re: Entity Converter - "value is not valid"

            Thanks Vladimir.


            I didn'know this about JPA lazy initialization. It's really interesting. But this is still not enough for me.


            I am using the combo to filter a searchAction (just an extended EntityQuery).


            The component that retrieves combo elements is session scoped, and I am injecting this component in searchAction in this way:



            @In(create=true) ComboItems comboItems;



            I realized that changing create=true with required=false the value not valid error doesn't appear. But I don't know why.


            I was reading this faqEntry
            . Could be related with my problem?


            Thanks again

            • 18. Re: Entity Converter - "value is not valid"

              Unfortunatelly, I am still getting this error.


              I post here my code for the action that retrieves the elements for selectItems. Maybe I am doing something wrong.



              @Name("tipoMatriculaSelect")
              
              @Scope(ScopeType.SESSION)
              
              public class SelectList {
              
              
                   @In(create=true)
              
                   EntityManager entityManager;
              
                   
              
                   private List<TipoMatricula> aux;
              
              
                   public List <TipoMatricula> getResultList() {
              
                        return aux;
              
                   }
              
                   @Create     
              
                   public void inicializar(){
              
                        Query q;
              
                        String ejbql;
              
                        ejbql="select t from TipoMatricula t ";
              
                        q = entityManager.createQuery(ejbql);
              
                        aux= q.getResultList();
              
                   }
              
                   
              
                   public TipoMatricula getTipoMatriculaById(Long id){
              
                        return map.get(id);
              
                   }
              
              }



              As you can see, this component is session scope. I am doing this because these entities change few times. Form facelets I call
              So I am trying to 'cache' them in for each session. Is correct this approach?


              If I use ScopeType.CONVERSATION the JSF value not valid doesn't occur.

              • 19. Re: Entity Converter - "value is not valid"
                nickarls

                Put debug code in your equals() to see what you are comparing against what and if there are any matches...

                • 20. Re: Entity Converter - "value is not valid"
                  clarkchrisp

                  Hello,


                  For anyone reading this forum I had exactly the same issue, I'm using two entity managers with seam managed one for the gui (it's great in solving the lazy loading issue). Anyway I can confirm that overriding the equals and hashcode methods on the entity did fix the issue, so give this a try first.


                  Thanks


                  Chris


                  • 21. Re: Entity Converter - "value is not valid"
                    smilind

                    Hello everyone,


                    I am using seam managed persistence using EntityQuery. I am getting Value is not valid error when I select value from h:selectOneMenu.
                    I tried by overriding equals() and hashCode() method but it did not help.
                    I observed that if I use the resultList of EntityQuery directly, I am not getting that error. However when I get resultList again and sort it in sortedResultList, I get that error.


                    How to make sure that the same persistence context through the whole form (for loading the list, for loading the value and for entity converter) when using EntityQuery for getting list of records from database?


                    Any ideas?

                    • 22. Re: Entity Converter - "value is not valid"

                      Hello all,


                      I have the same error as Chris has. I want to compare object (Person) that is in CONVERSATION scope with on in ArrayList (PersonList) that is in APPLICATION scope. When debugging equals method of Person ( equals(Object other) ) other value is null. If i change PersonList scope to CONVERSATION it works just fine.


                      Thanks,
                      Simon Himmelreich

                      • 23. Re: Entity Converter - "value is not valid"

                        I have found the solution of my problem.


                        Just add another managed persistence context in component.xml file and add jpa-entity-loader.   Now you will have one SMPC just for Lists that are in APPLICATION or SESSION context and you will avoid jsf validation errors on converter.
                        Remember to use @Observer and @RaiseEvent annotations for data consistency.


                        new components.xml file:


                            <persistence:managed-persistence-context auto-create="true" name="entityManager"
                        persistence-unit-jndi-name="java:/ucrmEntityManagerFactory" />
                        
                            <persistence:managed-persistence-context auto-create="true" name="entityManagerConvertEntity" 
                        persistence-unit-jndi-name="java:/ucrmEntityManagerFactory" />
                            
                            <ui:jpa-entity-loader entity-manager="#{entityManagerConvertEntity}" />




                        • 24. Re: Entity Converter - "value is not valid"
                          subaochen

                          I have met the same problem this morning.....and find that, the equals method eclipse generated is not quite correct, for example:



                               @Override
                               public boolean equals(Object obj) {
                                    if (this == obj)
                                         return true;
                                    if (obj == null)
                                         return false;
                                    if (getClass() != obj.getClass())
                                         return false;
                                    PaymentCfg other = (PaymentCfg) obj;
                                    if (id != other.id)
                                         return false;
                                    return true;
                               }





                          should be:




                               @Override
                               public boolean equals(Object obj) {
                                    if (this == obj)
                                         return true;
                                    if (obj == null)
                                         return false;
                                    PaymentCfg other = (PaymentCfg) obj;
                                    if (id != other.id)
                                         return false;
                                    return true;
                               }





                          work done.


                          Su Baochen

                          • 25. Re: Entity Converter - "value is not valid"
                            kragoth

                            Su Baochen wrote on Sep 08, 2010 02:02:


                            I have met the same problem this morning.....and find that, the equals method eclipse generated is not quite correct, for example:


                                 @Override
                                 public boolean equals(Object obj) {
                                      if (this == obj)
                                           return true;
                                      if (obj == null)
                                           return false;
                                      if (getClass() != obj.getClass())
                                           return false;
                                      PaymentCfg other = (PaymentCfg) obj;
                                      if (id != other.id)
                                           return false;
                                      return true;
                                 }





                            should be:



                                 @Override
                                 public boolean equals(Object obj) {
                                      if (this == obj)
                                           return true;
                                      if (obj == null)
                                           return false;
                                      PaymentCfg other = (PaymentCfg) obj;
                                      if (id != other.id)
                                           return false;
                                      return true;
                                 }





                            work done.

                            Su Baochen


                            Leaving out the line


                            if (getClass() != obj.getClass()) return false;
                            



                            I would have thought it is probably not a good idea. Doing so just means that the very next line of code will result in a ClassCastException if two classes of different types are being compared.


                            I know with inheritance or whatever this may not be desirable but maybe using the instanceof comparator would work.


                            Either way you are just opening up your application for exceptions by not doing some sort of check before casting.


                            I don't know all the technicalities of implementing equals and hashcode because too many times it comes back and bites ya. But, I'm pretty sure eclipse's little check is there for a good reason.

                            • 26. Re: Entity Converter - "value is not valid"
                              sandeepkul88

                              Hi All,


                              I came across a similar problem. The value selected in h:selectOneMenu was not accepted by jsf and it throw validation error that value is not valid. I am using a single persistence context in whole form.


                              After overriding equals() and hashcode() and checking only id columns in it the problem is resolved. (Note : equals() and hashcode() implementation should be alike)


                              Thanks for help.
                              Regards

                              • 27. Re: Entity Converter - "value is not valid"
                                davidmiller

                                I hit the same problem, but solved by actually passing the EntityManager into the Bean that I was using to return a List of Entity beans:


                                So with my EntityHome implementation I have this:



                                   public void refreshDownloads() {
                                        masterDocuments = downloadService.getMasterDocuments(getCmsCompanyIdCom(), getEntityManager());
                                        liveDocuments = downloadService.getLiveDocuments(getCmsCompanyIdCom(), getEntityManager());
                                        log.debug("Number of masterDocuments {0}", masterDocuments.size());
                                        log.debug("Number of liveDocuments {0}", liveDocuments.size());
                                    }



                                See how I'm passing the 'getEntityManager()' to:




                                @Stateless
                                @Scope(ScopeType.CONVERSATION)
                                @Name("downloadService")
                                public class DownloadService implements DownloadServiceI{
                                
                                    @SuppressWarnings("unchecked")
                                    @Override
                                    @Begin(join=true)
                                    public List<CmsDownloads> getLiveDocuments(Integer idCom, EntityManager em) {
                                        Query query = em.createNamedQuery("Document.findLive");
                                        query.setParameter("status", StatusCode.Live.toString());
                                        query.setParameter("idCom", idCom);
                                        return query.getResultList();
                                    }
                                
                                    @SuppressWarnings("unchecked")
                                    @Override
                                    @Begin(join=true)
                                    public List<CmsDownloads> getMasterDocuments(Integer idCom, EntityManager em) {
                                        Query query = em.createNamedQuery("Document.findMaster");
                                        query.setParameter("live", StatusCode.Live.toString());
                                        query.setParameter("suppress", StatusCode.Suppress.toString());
                                        query.setParameter("idCom", idCom);
                                        return query.getResultList();
                                    }
                                }



                                I was originally injecting the EntityManager by using @PersistenceContext but this never worked.


                                So I'm kind of frustrated the code I have now is not modular and is coupled to the EntityManager, but it works.



                                • 28. Re: Entity Converter - "value is not valid"
                                  jimwang

                                  Pete Muir 编写:

                                   

                                  Override equals() and hashCode() on the entity.


                                  Or make sure you are using the same persistence context through the whole form (for loading the list, for loading the value and for  entity converter).

                                  on the entity.

                                   

                                   

                                   

                                  thanks  Pete Muir it's helpful .

                                   

                                  My case is :  my  page has a  selectOneMenu , when I   do nothing ,  after 10 minutes ,    change the value of selection element.  i get the Value is not valid error.

                                   

                                  after I check my entity and overwrite equals() and hasCode().   It works.

                                  • 29. Re: Entity Converter - "value is not valid"
                                    strenkor

                                    Sorry for reviving this old post, but the replies of the post helped me to solve my problem, but in order to solve it I had to mix different replies. So I thought I can share my experience on this particular problem..

                                     

                                    For clarity, all the entities I'm displaying in h:selectOneMenu and s:selectItems are being kept in a List inside of a SessionScoped bean. And I'm using them in different pages and different conversations in my application. The List gets initialized at the creation of the SessionScoped bean and gets used throughout the whole session.

                                     

                                    First of all, overriding equals() and hashCode() methods of the responding entities solves the problem but there are some points to be aware of.

                                     

                                     

                                    @Override
                                    public int hashCode() {
                                              final int prime = 31;
                                              int result = 1;
                                              result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
                                              return result;
                                    }
                                    
                                    @Override
                                    public boolean equals(Object obj) {
                                              if (this == obj)
                                                        return true;
                                              if (obj == null)
                                                        return false;
                                              /*if (getClass() != obj.getClass())
                                                        return false;*/
                                              MyEntity other = (MyEntity) obj;
                                              if (getId() == null) {
                                                        if (other.getId() != null)
                                                                  return false;
                                              } else if (!getId().equals(other.getId()))
                                                        return false;
                                              return true;
                                    }
                                    

                                     

                                    I've used the overrides, that have been generated by eclipse using Id field of MyEntity, but they did not solved my problem.

                                     

                                    In order to get it working:

                                    • I commented out the Class comparison in equals() method. Clearly, the object convertEntity passes to equals() method is not an instance of MyEntity class (maybe we can use class.isAssignableFrom() method instead).
                                    • I used the getter function of Id field in my override methods instead of directly using it. It could be a problem raised by lazy initialization or something else, I honestly don't know.

                                     

                                    My theory on the "why" of this problem is that the entities we used at the h:selectOneMenu getting detached from the entityManager throughout the conversations of the application. Maybe we use entityManager.clear at some point, or we use different persistence contexts, or a connection problem with database leads to a recreation of entitymanager, or something else.

                                     

                                    Anyway, I hope my amateur explanation on the matter somehow helps people, and thanks to the previous repliers for their help..

                                     

                                    Message was edited by "strenkor" in order to fix style problem on the code.