1 2 3 Previous Next 30 Replies Latest reply on Jan 9, 2011 3:25 PM by wadhah

    how to persist data from selectManyCheckbox

    wadhah

      hello every body how to persist data from selectManyCheckbox ?
      I have a list of roles i would like to persist them 



      package org.domain.seam.entity;
      import java.io.Serializable;
      import javax.persistence.Entity;
      import javax.persistence.Id;
      import javax.persistence.GeneratedValue;
      import javax.persistence.Table;
      import org.hibernate.validator.Length;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.security.management.RoleName;
      
      @Name("role")
      @Entity
      @Table(name = "Role")
      public class Role implements Serializable {
           /**
            * 
            */
           private static final long serialVersionUID = 2392158972471437066L;
      
           private Long id;
           private String rolename;
      
           @Id
           @GeneratedValue
           public Long getId() {
                return id;
           }
      
           public void setId(Long id) {
                this.id = id;
           }
      
           @RoleName
           @Length(max = 20)
           public String getName() {
                return rolename;
           }
      
           public void setName(String rolename) {
                this.rolename = rolename;
           }
      
      }
      



      package org.domain.seam.entity;
      import java.io.Serializable;
      import java.util.HashSet;
      import java.util.Set;
      import javax.persistence.Entity;
      import javax.persistence.Id;
      import javax.persistence.GeneratedValue;
      import javax.persistence.JoinColumn;
      import javax.persistence.JoinTable;
      import javax.persistence.ManyToMany;
      import javax.persistence.Table;
      import org.hibernate.validator.Length;
      import org.hibernate.validator.NotNull;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.security.management.UserRoles;
      
      @Entity
      @Name("user")
      @Table(name = "User")
      public class User implements Serializable {
      
           /**
            * 
            */
           private static final long serialVersionUID = -2133760147013842042L;
           private Long id;
           private String name;
           private String password;
           private String username;
           private Set<Role> roles = new HashSet<Role>();
      
           @Id
           @GeneratedValue
           public Long getId() {
                return id;
           }
      
           public void setId(Long id) {
                this.id = id;
           }
      
           @Length(max = 20)
           @NotNull
           public String getName() {
                return name;
           }
      
           public void setName(String name) {
                this.name = name;
           }
      
           @Length(max = 20)
           @NotNull
           public String getPassword() {
                return password;
           }
      
           public void setPassword(String password) {
                this.password = password;
           }
      
           @Length(max = 20)
           @NotNull
           public String getUsername() {
                return username;
           }
      
           public void setUsername(String username) {
                this.username = username;
           }
      
           @ManyToMany(targetEntity = Role.class)
           @UserRoles
           @JoinTable(name = "UsersRoles", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
           public Set<Role> getRoles() {
                return roles;
           }
      
           public void setRoles(Set<Role> roles) {
                this.roles = roles;
           }
      
      }
      






      Roles: <h:selectManyCheckbox>
                <f:selectItem itemLabel="Agent" itemValue="Agent" />
                <f:selectItem itemLabel="Admin" itemValue="Admin" />
           </h:selectManyCheckbox>


        • 1. Re: how to persist data from selectManyCheckbox
          wadhah

          what should i write into the tag



          <h:selectManyCheckbox>




          ?

          • 2. Re: how to persist data from selectManyCheckbox
            wadhah

            plz i need a help how to store selectmanycheckbox values into database?

            • 3. Re: how to persist data from selectManyCheckbox
              lvdberg

              Hi,


              You should write an bean which takes the values and store them in the database. You only have two entity beans which by themselves don't do much.


              So something like the following.



              @Name("wadhahBean")
              @Scope(CONVERSATION)
              public class WadHahBean implements  Serializable {
              
                @In protected EntityManager entityManager;
              
              
                private User user;  
                private List<Role> selectedRoles;  
              
                @Begin(join=true)
                public void init(){
                 ///.... Do something which inits your page vars and selects your user and get the roles
                  selectedRoles = new ArrayList<Role>(user.getRoles());
                }
              
                @Transactional
                public void save(){
                 user.setRoles(new HashSet(selectedRoles));
                 entityManager.persist(user.setRoles());
                }
              
              }






              Now you can save the UserRoles by doing:





              <h:selectManyCheckbox value="#{wadhahBean.selectedRoles}">
                   <f:selectItem itemLabel="Agent" itemValue="Agent" />
                   <f:selectItem itemLabel="Admin" itemValue="Admin" />
              </h:selectManyCheckbox>



              It's just a first draft, soyou should work on it.


              Leo



              • 4. Re: how to persist data from selectManyCheckbox
                wadhah

                Ok i wrote my class RegisterBean without writing roles this is my problem
                So i should add the roleList now in my RegisterBean
                thx


                package org.domain.seam.session;
                
                
                import javax.ejb.Stateless;
                import javax.persistence.EntityManager;
                import javax.persistence.PersistenceContext;
                import org.domain.seam.entity.User;
                import org.jboss.seam.annotations.Name;
                import org.jboss.seam.annotations.In;
                import org.jboss.seam.annotations.Logger;
                import org.jboss.seam.log.Log;
                import org.jboss.seam.international.StatusMessages;
                
                @Stateless
                @Name("Register")
                public class RegisterBean implements Register {
                     @Logger
                     private Log log;
                
                     @In
                     StatusMessages statusMessages;
                     @PersistenceContext
                     private EntityManager em;
                     @In
                     private User user;
                     public String register() {
                          // implement your business logic here
                          // log.info("Register.register() action called");
                          // statusMessages.add("register");
                          em.persist(user);
                          return "/registered.xhtml";
                     }
                
                     // add additional action methods
                
                }
                



                • 5. Re: how to persist data from selectManyCheckbox
                  wadhah

                  @Leo
                  in the xhtml page when i tape wadhahbean. that appear only the methods i didn't found the attributes . . .



                  <h:selectManyCheckbox value="#{wadhahBean.selectedRoles}">
                  



                  • 6. Re: how to persist data from selectManyCheckbox
                    lvdberg

                    Hi,


                    you need setter and getters for each property. I didn't add those for brevity, but you need them to work.


                    Leo

                    • 7. Re: how to persist data from selectManyCheckbox
                      wadhah

                      @Leo this is my bean


                      package org.domain.seam.session;
                      
                      import java.util.HashSet;
                      import java.util.Set;
                      
                      import javax.ejb.Stateless;
                      import javax.persistence.EntityManager;
                      import javax.persistence.PersistenceContext;
                      
                      import org.domain.seam.entity.Role;
                      import org.domain.seam.entity.User;
                      import org.jboss.seam.ScopeType;
                      import org.jboss.seam.annotations.Name;
                      import org.jboss.seam.annotations.In;
                      import org.jboss.seam.annotations.Logger;
                      import org.jboss.seam.annotations.Scope;
                      import org.jboss.seam.annotations.Transactional;
                      import org.jboss.seam.log.Log;
                      import org.jboss.seam.international.StatusMessages;
                      
                      @Stateless
                      @Name("Register")
                      @Scope(ScopeType.CONVERSATION)
                      public class RegisterBean implements Register {
                           @Logger
                           private Log log;
                      
                           @In
                           StatusMessages statusMessages;
                           @In
                           private User user;
                           @PersistenceContext
                           private EntityManager em;
                           private Set<Role> selectedRoles = new HashSet<Role>(user.getRoles());
                      
                           public User getUser() {
                                return user;
                           }
                      
                           public void setUser(User user) {
                                this.user = user;
                           }
                      
                           public Set<Role> getSelectedRoles() {
                                return selectedRoles;
                           }
                      
                           public void setSelectedRoles(Set<Role> selectedRoles) {
                                this.selectedRoles = selectedRoles;
                           }
                      
                           @Transactional
                           public String register() {
                                // implement your business logic here
                                //log.info("Register.register() action called");
                                //statusMessages.add("register");
                                user.setRoles(new HashSet<Role>(getSelectedRoles()));
                                em.persist(user);
                                return "/registered.xhtml";
                           }
                      
                           // add additional action methods
                      
                      }
                      


                      This is my xhtml page




                      <h:selectManyCheckbox value="#{Register.getSelectedRoles}">
                           <f:selectItem itemLabel="Agent" itemValue="Agent" />
                           <f:selectItem itemLabel="Admin" itemValue="Admin" />
                      </h:selectManyCheckbox>


                      this is the error





                      javax.el.PropertyNotFoundException: /register.xhtml @27,69 value="#{Register.selectedRoles}": Property 'selectedRoles' not found on type org.javassist.tmp.java.lang.Object_$$_javassist_seam_2




                      Please help me




                      • 8. Re: how to persist data from selectManyCheckbox
                        lvdberg

                        Hi,


                        you should use:




                        #{Register.selectedRoles}



                        You use directly the propertyname of the bean without set/get


                        Leo

                        • 9. Re: how to persist data from selectManyCheckbox
                          lvdberg

                          Hi,


                          I also think there is something wrong with your configuration, because ith should return an error property not found on the beanname and not on Object.


                          Check the previous first and see if it works. If not give some configuration data (components.xml)


                          Leo

                          • 10. Re: how to persist data from selectManyCheckbox
                            wadhah

                            The same error when i use direclty the propertyname  of the bean
                            This is my components.xml


                            <?xml version="1.0" encoding="UTF-8"?>
                            <components xmlns="http://jboss.com/products/seam/components"
                                        xmlns:core="http://jboss.com/products/seam/core"
                                        xmlns:persistence="http://jboss.com/products/seam/persistence"
                                        xmlns:drools="http://jboss.com/products/seam/drools"
                                        xmlns:bpm="http://jboss.com/products/seam/bpm"
                                        xmlns:security="http://jboss.com/products/seam/security"
                                        xmlns:mail="http://jboss.com/products/seam/mail"
                                        xmlns:web="http://jboss.com/products/seam/web"
                                        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                                        xsi:schemaLocation=
                                            "http://jboss.com/products/seam/core http://jboss.com/products/seam/core-2.2.xsd
                                             http://jboss.com/products/seam/persistence http://jboss.com/products/seam/persistence-2.2.xsd
                                             http://jboss.com/products/seam/drools http://jboss.com/products/seam/drools-2.2.xsd
                                             http://jboss.com/products/seam/bpm http://jboss.com/products/seam/bpm-2.2.xsd
                                             http://jboss.com/products/seam/security http://jboss.com/products/seam/security-2.2.xsd
                                             http://jboss.com/products/seam/mail http://jboss.com/products/seam/mail-2.2.xsd
                                             http://jboss.com/products/seam/web http://jboss.com/products/seam/web-2.2.xsd
                                             http://jboss.com/products/seam/components http://jboss.com/products/seam/components-2.2.xsd">
                            
                               <core:init debug="true" jndi-pattern="@jndiPattern@"/>
                            
                               <core:manager concurrent-request-timeout="500"
                                             conversation-timeout="120000"
                                             conversation-id-parameter="cid"
                                             parent-conversation-id-parameter="pid"/>
                            
                               <!-- Make sure this URL pattern is the same as that used by the Faces Servlet -->
                               <web:hot-deploy-filter url-pattern="*.seam"/>
                            
                               <persistence:managed-persistence-context name="entityManager" auto-create="true"
                                                  persistence-unit-jndi-name="java:/SeamEntityManagerFactory"/>
                            
                               <drools:rule-base name="securityRules">
                                  <drools:rule-files>
                                     <value>/security.drl</value>
                                  </drools:rule-files>
                               </drools:rule-base>
                            
                               <security:rule-based-permission-resolver security-rules="#{securityRules}"/>
                            
                               <security:identity authenticate-method="#{authenticator.authenticate}" remember-me="true"/>
                            
                               <event type="org.jboss.seam.security.notLoggedIn">
                                  <action execute="#{redirect.captureCurrentView}"/>
                               </event>
                               <event type="org.jboss.seam.security.loginSuccessful">
                                  <action execute="#{redirect.returnToCapturedView}"/>
                               </event>
                            
                               <mail:mail-session host="localhost" port="25"/>
                            
                               <!-- For use with jBPM pageflow or process management -->
                               <!--
                               <bpm:jbpm>
                                  <bpm:process-definitions></bpm:process-definitions>
                                  <bpm:pageflow-definitions></bpm:pageflow-definitions>
                               </bpm:jbpm>
                               -->
                            
                            </components>
                            



                            • 11. Re: how to persist data from selectManyCheckbox
                              lvdberg

                              Hi,


                              Are the Seam interceptor in place ? In other words: because you don't have such an annotation on the satles bean, there should be one defined in the file ejb-jar-xml That file should look like the following. I see that you're basically using a Seam-gen project so I assume that the rest of the settings in the different files is correct.


                              To make a quick check to see if the bean is available you could:


                              - See if the bean is registered in JNDI. If you use Jboss you can use the management tools of the server.
                              - To see if the bean exists and is intercepted correctly, create whatever init method, put a @Create annotation on that method and do some logging in the method.


                              The thing what really worries me is the fact that the error is on the JavaAssist on not on some component name.


                              Leo

                              • 12. Re: how to persist data from selectManyCheckbox
                                wadhah

                                Leo i would like to clarify that when i persist user,username and the password i found data stored in the database but when i put the list of selectedRoles i have this error


                                 Property 'selectedRoles' not found on type org.javassist.tmp.java.lang.Object_$$_javassist_seam_2



                                • 13. Re: how to persist data from selectManyCheckbox
                                  aareshchanka

                                  Use List not Set selectedRoles.

                                  • 14. Re: how to persist data from selectManyCheckbox
                                    wadhah

                                    Thank you for answer it's the same error when i changed Set to the List !!

                                    1 2 3 Previous Next