5 Replies Latest reply on Jun 28, 2008 4:34 PM by nimo stephan

    selectManyCheckbox (ManyToMany, s:convertEntity)

    nimo stephan Master

      Hello,


      I have a view:



      <s:label value="Select Permissions for Person"/>
      <h:selectManyCheckbox value="#{persons.permissions}">
      <s:selectItems value="#{permissionList}" var="p" label="#{p.description}" itemValue="#{p.idPermission}"/>
      </h:selectManyCheckbox>
      
      <h:commandButton value="Save permissions to User" action="#{sessionBean.save()}"/>
      



      The view shows all the permissions a user can choose.
      The 'itemValue'-Attribute in s:selectItems returns the actual permission-ID.


      I have two entity-beans :



      Permission and Person have a (bidirectional) ManyToMany-Relation.


      So what I wanna to do is to save the choosen permissions in the associated join-table:


      ..
      save() {
      ..
      Person person = new Person();
      person.setAge(person.getAge());
      person.setPermissions(person.getPermissions());
      entityManager.persist(person);
      ..}
      



      But I cannot save this. When I push the button save, no stacktrace or message occurs - no save is made! My View have other components, such as an h:inputText for my age-attribute ('person.setAge(person.getAge())').


      When I delete the 'selectManyCheckbox' in the view and the
      'person.setPermissions(person.getPermissions())' from the session-bean, the other components (i.e. setAge) works well and the person will be saved. When I insert the s:convertEntity in my 'selectManyCheckbox', the message EntityManager closed appears in the stacktrace.


      Have I forgotten anything?

        • 1. Re: selectManyCheckbox (ManyToMany, s:convertEntity)
          nimo stephan Master

          I have red the thread http://www.seamframework.org/Community/HowToUseSselectItemsWithHselectManyCheckbox
          It does not help me out..


          For my Entity-Beans I used Sets..however, the permissionList is a List:



            @Factory("permissionList")
            public List <SelectItem> getPermissionList() {
          
          return  entityManager.createQuery("from Permission m").getResultList();
          }


          • 2. Re: selectManyCheckbox (ManyToMany, s:convertEntity)
            nimo stephan Master

            Okay I come closer to my problem.


            I substitute the this:



            <s:label value="Select Permissions for Person"/>
            <h:selectManyCheckbox value="#{persons.permissions}">
            <s:selectItems value="#{permissionList}" var="p" label="#{p.description}" itemValue="#{p.idPermission}"/>
            </h:selectManyCheckbox>
            



            with that (h:selectItems instead of s:selectItems):


            <s:label value="Select Permissions for Person"/>
            <h:selectManyCheckbox value="#{persons.permissions}">
            <h:selectItems value="#{permissionList}" var="p" label="#{p.description}" itemValue="#{p.idPermission}"/>
            </h:selectManyCheckbox>
            



            The following error:



            Argument Error: An option for component selectManyX was not an instance of javax.faces.model.SelectItem. Type found: java.util.ArrayList.
            



            But my Factory has SelectItem:


            @Factory("permissionList")
              public List <SelectItem> getPermissionList() {
            
            return  entityManager.createQuery("from Permission m").getResultList();
            }
            






            • 3. Re: selectManyCheckbox (ManyToMany, s:convertEntity)
              nimo stephan Master

              I come closer to the problem:



              Caused by: org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of com.entity.Permission.idPermission
                   at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:171)
                   at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:183)
                   at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:3591)
                   at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:3307)
                   at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:181)
                   at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:218)
                   at org.hibernate.type.EntityType.getIdentifier(EntityType.java:397)
                   at org.hibernate.type.EntityType.replace(EntityType.java:253)
                   at org.hibernate.type.CollectionType.replaceElements(CollectionType.java:451)
                   at org.hibernate.type.CollectionType.replace(CollectionType.java:518)
                   at org.hibernate.type.TypeFactory.replace(TypeFactory.java:482)
                   at org.hibernate.event.def.DefaultMergeEventListener.copyValues(DefaultMergeEventListener.java:340)
                   at org.hibernate.event.def.DefaultMergeEventListener.entityIsPersistent(DefaultMergeEventListener.java:153)
                   at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:126)
                   at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:53)
                   at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:677)
                   at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:661)
                   at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:665)
                   at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:227)
                   ... 139 more
              Caused by: java.lang.IllegalArgumentException: java.lang.ClassCastException@7b41d6
                   at sun.reflect.GeneratedMethodAccessor196.invoke(Unknown Source)
                   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
                   at java.lang.reflect.Method.invoke(Unknown Source)
                   at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:145)
                   ... 157 more
              



              so this is not a seam specific problem..the failure occurs when I do not use s:convertEntity. By using 's:convertEntity', the message 'EntityManager closed' appear.


              java.lang.IllegalStateException: EntityManager is closed





              • 4. Re: selectManyCheckbox (ManyToMany, s:convertEntity)
                Daniel Hinojosa Master

                did you put all your view tags in an <h:form>?


                also, you may want to consider removing the () from save...


                <h:commandButton value="Save permissions to User" action="#{sessionBean.save}"/>
                
                

                • 5. Re: selectManyCheckbox (ManyToMany, s:convertEntity)
                  nimo stephan Master

                  I put all my view tags in an h:form and considered removing () from save (though, it does not matter if with () or without () ).


                  However, I guess it s a hibernate issue..I have a manytomany-relation between two entities and when I save a new Person-Record in the database all works well, but only the relation-table PERSON-PERMISSION does not persist the new record with the generated PersonID and its associated PermissionID coming from the form selectManyCheckbox. The PermissionID exists, the generated PersonID comes from the actual instance..


                  I cannot use s:convertentity, by using it, this error comes:


                  java.lang.IllegalStateException: EntityManager is closed



                  I do not use the s:convertentity, and now this error comes, when I want to persist a new Person-Object with its Permissions. (ManyToMany)


                  Caused by: org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of entity.Permission.idPermission
                          at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:171)
                          at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:183)
                          at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:3591)
                          at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:3307)
                          at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:181)
                          at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:218)
                          at org.hibernate.type.EntityType.getIdentifier(EntityType.java:397)
                          at org.hibernate.type.EntityType.replace(EntityType.java:253)
                          at org.hibernate.type.CollectionType.replaceElements(CollectionType.java:451)
                          at org.hibernate.type.CollectionType.replace(CollectionType.java:518)
                          at org.hibernate.type.TypeFactory.replace(TypeFactory.java:482)
                          at org.hibernate.event.def.DefaultMergeEventListener.copyValues(DefaultMergeEventListener.java:340)
                          at org.hibernate.event.def.DefaultMergeEventListener.entityIsPersistent(DefaultMergeEventListener.java:153)
                          at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:126)
                          at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:53)
                          at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:677)
                          at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:661)
                          at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:665)
                          at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:227)
                          ... 140 more
                  Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
                          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                          at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
                          at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
                          at java.lang.reflect.Method.invoke(Unknown Source)
                          at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:145)
                          ... 158 more
                  



                  So I assume, that hibernate wants to find the PermissionIDs from the Permission-Entity with the getter (which still exists). Well, I guess it s an hibernate-issue..