1 2 3 Previous Next 36 Replies Latest reply on Sep 18, 2006 3:56 AM by trouby Go to original post
      • 30. Re: Entities with one/many_to_many relationships and SEAM
        adam.stortz

        trouby,

        Are you saying that the entity converter does not work properly with JSF 1.2?

        Pete, is that the feeling you are getting as well?

        Adam

        • 31. Re: Entities with one/many_to_many relationships and SEAM
          adam.stortz

          Alright guys, thought you might like to know.

          I figured out how to correct the problem with my application and the validation error.

          It was a really simple fix, grab the latest version of JSF... version 1.2_01.

          Pete, keep up the good work, this is a really slick way to handle this, and it eliminates one of my biggest pet peeves with JSF.

          Converters are a fantastic idea, but it is really nice to finally have a way to automate the standard cases.

          Adam

          • 32. Re: Entities with one/many_to_many relationships and SEAM
            trouby

            Hey Adam,

            Glad to hear it works for you,

            Say, you use Jboss with JSF 1.2 RI right?

            If so, it would be very interesting to know if the multiple SelectItems annotation with the entity converter by the facelets tag works for you,

            You have the example in Petemuir's blog site,


            I'll be glad to know if it works for you because for me it doesnt ;-q

            I guess You'll need one/many-to-many relationships GUI management someday anyway :)


            Cheers

            • 33. Re: Entities with one/many_to_many relationships and SEAM
              adam.stortz

              Trouby,

              Yes, I am using JBoss with JSF 1.2_01 RI and facelets

              I am using JBoss 4.0.4 with the EJB 3.0 RC8 update

              I am not using the multiple SelectItems to populate a ManyToMany or OneToMany relationship, I am injecting the selected items into a stateful session bean, but translating it to manage an entity relationship would be fairly easy. Here are the important pieces of code, maybe another example would help you. The most interesting parts will be in bold.

              editBlogEntry.xhtml

              <ui:composition template="/template/template.xhtml"
               xmlns="http://www.w3.org/1999/xhtml"
               xmlns:ui="http://java.sun.com/jsf/facelets"
               xmlns:f="http://java.sun.com/jsf/core"
               xmlns:h="http://java.sun.com/jsf/html"
               xmlns:selectitems="http://jboss.com/products/seam/selectitems/taglib">
              
               <ui:define name="body">
               <h:messages globalOnly="true" layout="list" />
              
               <h:form>
               <h:inputHidden value="${blogEntry.id}" />
               <h:panelGrid columns="3">
              
               <h:outputText value="${msgs.title}:" />
               <h:inputText id="title" value="${blogEntry.title}" required="true"/>
               <h:message for="title" styleClass="error"/>
              
               <h:outputText value="${msgs.body}:" />
               <h:inputTextarea id="body" value="${blogEntry.body}" rows="50" cols="30"/>
               <h:message for="body" styleClass="error"/>
              
               <h:outputText value="${msgs.userGroups}:"/>
               <h:outputText value="${msgs.userGroupsEmpty}" rendered="${empty userGroupsSelectItems}" />
               <h:selectManyListbox id="userGroups" size="5" value="${blogEntryService.selectedUserGroups}" rendered="${not empty userGroupsSelectItems}">
               <f:selectItems value="${userGroupsSelectItems}"/>
               <selectitems:convertEntity entityClass="org.unleashed.ejb.entity.UserGroup" />
               </h:selectManyListbox>
               <h:message for="userGroups" styleClass="error" rendered="${not empty userGroupsSelectItems}"/>
              
               </h:panelGrid>
              
               <h:commandButton action="${blogEntryService.save}" value="${msgs.save}" />
               </h:form>
              
               </ui:define>
              </ui:composition>
              


              BlogEntryServiceBean.java - This is where the multiple select items get injected from the page
              @Stateful
              @Name("blogEntryService")
              public class BlogEntryServiceBean implements BlogEntryService{
              
              
               @Logger private Log log;
              
               @PersistenceContext
               private EntityManager em;
              
               @In (create=true)
               @Out
               @Valid
               private BlogEntry blogEntry;
              
               
               @SelectItems(label="name")
               private List<UserGroup> userGroupsSelectItems;
              
               private List<UserGroup> selectedUserGroups;
              
               @Factory("userGroupsSelectItems")
               public List<UserGroup> findAllUserGroupsSelectItems()
               {
               userGroupsSelectItems = em.createQuery("from UserGroup userGroup").getResultList();
               return userGroupsSelectItems;
               }
              
               @IfInvalid(outcome=Outcome.REDISPLAY)
               public String save()
               {
               log.debug("Selected User Groups: '" + selectedUserGroups + "'");
               blogEntry.setPostDate(new Date());
               log.debug("Saving Blog Entry: " + blogEntry);
               em.merge(blogEntry);
               log.debug("Blog Entry: " + blogEntry + " saved.");
               return "viewBlogEntries";
               }
              
               public List<UserGroup> getSelectedUserGroups()
               {
               return selectedUserGroups;
               }
               public void setSelectedUserGroups(List<UserGroup> selectedUserGroups)
               {
               this.selectedUserGroups = selectedUserGroups;
               }
              ...
              
              }
              
              


              In the save method, the selected items are logged, right now I am performing no actions based on the selection, but the proper output shows up in the log

              components.xml - Deployed in WEB-INF
              FYI unleashed is the name of my application
              <components>
              
               <component class="org.jboss.seam.selectitems.SelectItemsPersistenceConfig">
               <property name="persistenceUnitJndiName">java:/selectItemsEntityManagerFactory</property>
               </component>
              
               <component name="em" class="org.jboss.seam.core.ManagedPersistenceContext">
               <property name="persistenceUnitJndiName">java:/unleashedEntityManagerFactory</property>
               </component>
              
               <component class="org.jboss.seam.selectitems.SelectItemsPersistenceConfig">
               <property name="persistenceUnitJndiName">java:/unleashedEntityManagerFactory</property>
               </component>
              </components>
              


              I also had to override the equals method in my UserGroup entity:
               @Override
               public boolean equals(Object obj) {
               if (obj instanceof UserGroup) {
               UserGroup userGroup = (UserGroup) obj;
               return this.id == userGroup.getId();
               } else {
               return false;
               }
               }
              


              I think that covers all of the code that has any impact on that. If you have any questions about the code, please let me know.

              Regards,
              Adam

              • 34. Re: Entities with one/many_to_many relationships and SEAM
                trouby

                Hey again,

                Thanks for the last big post Adam, not sure why, but it doesnt work for me :-/

                The method 'getEntityIdAsString' in the EntityConverter class is called, and then I get a 'convertion error' with the following message:

                Conversion Error setting value '2 5' for '#{entity.items}'.

                (While both items 2/5 ids were selected)

                And thats it, I'm not sure what throws the message, as it seems, it's a JSF message and not the converter error, so this is very hard to determine,

                more to say, I have globalmessages and everything, nothing shows up, except this error,


                As always guys, thanks a lot !

                Asaf.

                • 35. Re: Entities with one/many_to_many relationships and SEAM
                  pmuir

                  trouby, if you can convert the example I wrote to use 1.2 in jboss (alter the ant build so it will just deploy correctly to a clean install of jboss) I'll have a go at sorting it out. Sorry, I just don't have time to work out installing jsf 1.2.

                  • 36. Re: Entities with one/many_to_many relationships and SEAM
                    trouby

                    Eh, thanks for the efford and time, I solved this,

                    Eh, silly, after digging into internal code (since JSF didnt throw ANYTHING except displaying a 'convertion error' message!), as it seems the problem was that the property that keeps the selected items was defined as a 'Collection' instead of 'List', thats all.. :)


                    thanks !

                    1 2 3 Previous Next