1 2 3 Previous Next 38 Replies Latest reply on Jul 9, 2008 6:00 PM by tikus

    how to use s:selectItems with h:selectManyCheckbox

    anatorian

      I use s:selectItems inside the h:selectManyCheckbox. The items are some entities and I want the user select some item and set them to the backing beans property which is a Set type. But I get a error msg:
      Conversion Error setting value 'Role@16308ac Role@ce72a9' for '{userHome.instance.roles}'.


      Do I have to write a converter? Is there any good simple solution?

        • 1. Re: how to use s:selectItems with h:selectManyCheckbox
          nickarls

          Sounds like a job for <s:convertEntity>

          • 2. Re: how to use s:selectItems with h:selectManyCheckbox
            anatorian

            I tried the s:convertEntity, but I got another error Conversion Error setting value '0 1' for '{userHome.instance.roles}'.

            • 3. Re: how to use s:selectItems with h:selectManyCheckbox
              nickarls

              Have you implemented equals() and hashCode() for your Role class?

              • 4. Re: how to use s:selectItems with h:selectManyCheckbox
                anatorian

                No, let me try.


                By the way, how do you paste less-than charater here? I got an instant error message when I type it here.

                • 5. Re: how to use s:selectItems with h:selectManyCheckbox
                  nickarls

                  The Help-button said:




                  But if you really want to type a special character such as * or +, you need to escape it with a \.
                  • 6. Re: how to use s:selectItems with h:selectManyCheckbox
                    anatorian

                    No, it doesn't work. I implemented equals and hashCode, but it said:
                    Conversion Error setting value '0 1' for '#{userHome.instance.roles}'

                    • 7. Re: how to use s:selectItems with h:selectManyCheckbox
                      nickarls

                      Paste all relevant code (xhtml, entity, backing bean)

                      • 8. Re: how to use s:selectItems with h:selectManyCheckbox
                        anatorian

                        The xhtml:
                        <h:selectManyCheckbox value=#{userHome.instance.roles}>
                            <s:selectItems value=#{roleList.resultList} var=role label=#{role.name} />
                            <s:convertEntity />
                        </h:selectManyCheckbox>


                        The entity:
                        @Entity
                        public class User {
                             private Long id;


                             private Set<Role> roles = new HashSet<Role>(0);


                             @ManyToMany(targetEntity = Role.class)
                             @JoinTable()
                             public Set<Role> getRoles() {
                                  return roles;
                             }


                             public void setRoles(Set<Role> roles) {
                                  this.roles = roles;
                             }


                             @Id
                             @GeneratedValue
                             public Long getId() {
                                  return id;
                             }


                             public void setId(Long id) {
                                  this.id = id;
                             }


                        }


                        @Entity
                        @Table
                        public class Role implements java.io.Serializable {
                             private Long id;
                             private Set<User> users = new HashSet<User>(0);


                             @Id
                             @GeneratedValue
                             @Column(name = id, unique = true, nullable = false)
                             public Long getId() {
                                  return this.id;
                             }


                             public void setId(Long id) {
                                  this.id = id;
                             }


                             @ManyToMany(mappedBy=roles)
                             public Set<User> getUsers() {
                                  return this.users;
                             }


                             public void setUsers(Set<User> users) {
                                  this.users = users;
                             }
                             
                             public int hashCode() {
                                  final int prime = 31;
                                  int result = 1;
                                  result = prime * result + ((id == null) ? 0 : id.hashCode());
                                  return result;
                             }


                             public boolean equals(Object obj) {
                                  if (this == obj)
                                       return true;
                                  if (obj == null)
                                       return false;
                                  if (getClass() != obj.getClass())
                                       return false;
                                  final Role other = (Role) obj;
                                  if (id == null) {
                                       if (other.id != null)
                                            return false;
                                  } else if (!id.equals(other.id))
                                       return false;
                                  return true;
                             }
                        }



                        The user home and role list is generated by the seam:
                        @Name(userHome)
                        public class UserHome extends EntityHome<User> {


                             public void setUserId(Long id) {
                                  setId(id);
                             }


                             public Long getUserId() {
                                  return (Long) getId();
                             }


                             @Override
                             protected User createInstance() {
                                  User user = new User();
                                  return user;
                             }


                             public void wire() {
                             }


                             public boolean isWired() {
                                  return true;
                             }


                             public User getDefinedInstance() {
                                  return isIdDefined() ? getInstance() : null;
                             }


                        }


                        @Name(roleList)
                        public class RoleList extends EntityQuery {


                             private static final String[] RESTRICTIONS = {
                                       lower(role.code) like concat(lower(#{roleList.role.code}),'%'),
                                       lower(role.description) like concat(lower(#{roleList.role.description}),'%'),
                                       lower(role.name) like concat(lower(#{roleList.role.name}),'%'),};


                             private Role role = new Role();


                             @Override
                             public String getEjbql() {
                                  return select role from Role role;
                             }


                             @Override
                             public Integer getMaxResults() {
                                  return 25;
                             }


                             public Role getRole() {
                                  return role;
                             }


                             @Override
                             public List<String> getRestrictions() {
                                  return Arrays.asList(RESTRICTIONS);
                             }


                        }


                        • 9. Re: how to use s:selectItems with h:selectManyCheckbox
                          nickarls

                          Please use code tags

                          • 10. Re: how to use s:selectItems with h:selectManyCheckbox
                            anatorian

                            The xhtml:
                            &lt;code&gt;
                            <h:selectManyCheckbox value=#{userHome.instance.roles}> 
                                 <s:selectItems value=#{roleList.resultList} var=role label=#{role.name} /> 
                                 <s:convertEntity />
                            </h:selectManyCheckbox>
                            &lt;/code&gt;


                            The entity:
                            &lt;code&gt;
                            @Entity
                            public class User {
                                 private Long id;


                                 private Set<Role> roles = new HashSet<Role>(0);


                                 @ManyToMany(targetEntity = Role.class)
                                 @JoinTable()
                                 public Set<Role> getRoles() {
                                      return roles;
                                 }


                                 public void setRoles(Set<Role> roles) {
                                      this.roles = roles;
                                 }


                                 @Id
                                 @GeneratedValue
                                 public Long getId() {
                                      return id;
                                 }


                                 public void setId(Long id) {
                                      this.id = id;
                                 }
                            }


                            @Entity
                            public class Role{
                                 private Long id;
                                 private Set<User> users = new HashSet<User>(0);


                                 @Id
                                 @GeneratedValue
                                 @Column(name = id, unique = true, nullable = false)
                                 public Long getId() {
                                      return this.id;
                                 }


                                 public void setId(Long id) {
                                      this.id = id;
                                 }


                                 @ManyToMany(mappedBy=roles)
                                 public Set<User> getUsers() {
                                      return this.users;
                                 }


                                 public void setUsers(Set<User> users) {
                                      this.users = users;
                                 }
                                 
                                 public int hashCode() {
                                      final int prime = 31;
                                      int result = 1;
                                      result = prime * result + ((id == null) ? 0 : id.hashCode());
                                      return result;
                                 }


                                 public boolean equals(Object obj) {
                                      if (this == obj)
                                           return true;
                                      if (obj == null)
                                           return false;
                                      if (getClass() != obj.getClass())
                                           return false;
                                      final Role other = (Role) obj;
                                      if (id == null) {
                                           if (other.id != null)
                                                return false;
                                      } else if (!id.equals(other.id))
                                           return false;
                                      return true;
                                 }
                            }
                            @Name(roleList)
                            public class RoleList extends EntityQuery {


                                 private static final String[] RESTRICTIONS = {
                                           lower(role.code) like concat(lower(#{roleList.role.code}),'%'),
                                           lower(role.description) like concat(lower(#{roleList.role.description}),'%'),
                                           lower(role.name) like concat(lower(#{roleList.role.name}),'%'),};


                                 private Role role = new Role();


                                 @Override
                                 public String getEjbql() {
                                      return select role from Role role;
                                 }


                                 @Override
                                 public Integer getMaxResults() {
                                      return 25;
                                 }


                                 public Role getRole() {
                                      return role;
                                 }


                                 @Override
                                 public List<String> getRestrictions() {
                                      return Arrays.asList(RESTRICTIONS);
                                 }


                            }


                            @Name(userHome)
                            public class UserHome extends EntityHome<User> {


                                 public void setUserId(Long id) {
                                      setId(id);
                                 }


                                 public Long getUserId() {
                                      return (Long) getId();
                                 }


                                 @Override
                                 protected User createInstance() {
                                      User user = new User();
                                      return user;
                                 }


                                 public void wire() {
                                 }


                                 public boolean isWired() {
                                      return true;
                                 }


                                 public User getDefinedInstance() {
                                      return isIdDefined() ? getInstance() : null;
                                 }
                            }
                            &lt;/code&gt;

                            • 11. Re: how to use s:selectItems with h:selectManyCheckbox
                              nickarls

                              Please use code tags. And check in the preview that they are correct ;-)

                              • 12. Re: how to use s:selectItems with h:selectManyCheckbox
                                anatorian
                                The xhtml:
                                <h:selectManyCheckbox value=#{userHome.instance.roles}>  
                                     <s:selectItems value=#{roleList.resultList} var=role label=#{role.name} />  
                                     <s:convertEntity /> 
                                </h:selectManyCheckbox>
                                
                                The entities:
                                @Entity
                                public class User {
                                     private Long id;
                                
                                     private Set<Role> roles = new HashSet<Role>(0);
                                
                                     @ManyToMany(targetEntity = Role.class)
                                     @JoinTable()
                                     public Set<Role> getRoles() {
                                          return roles;
                                     }
                                
                                     public void setRoles(Set<Role> roles) {
                                          this.roles = roles;
                                     }
                                
                                     @Id
                                     @GeneratedValue
                                     public Long getId() {
                                          return id;
                                     }
                                
                                     public void setId(Long id) {
                                          this.id = id;
                                     }
                                }
                                
                                @Entity
                                public class Role{
                                     private Long id;
                                     private Set<User> users = new HashSet<User>(0);
                                
                                     @Id
                                     @GeneratedValue
                                     @Column(name = "id", unique = true, nullable = false)
                                     public Long getId() {
                                          return this.id;
                                     }
                                
                                     public void setId(Long id) {
                                          this.id = id;
                                     }
                                
                                     @ManyToMany(mappedBy="roles")
                                     public Set<User> getUsers() {
                                          return this.users;
                                     }
                                
                                     public void setUsers(Set<User> users) {
                                          this.users = users;
                                     }
                                     
                                     public int hashCode() {
                                          final int prime = 31;
                                          int result = 1;
                                          result = prime * result + ((id == null) ? 0 : id.hashCode());
                                          return result;
                                     }
                                
                                     public boolean equals(Object obj) {
                                          if (this == obj)
                                               return true;
                                          if (obj == null)
                                               return false;
                                          if (getClass() != obj.getClass())
                                               return false;
                                          final Role other = (Role) obj;
                                          if (id == null) {
                                               if (other.id != null)
                                                    return false;
                                          } else if (!id.equals(other.id))
                                               return false;
                                          return true;
                                     }
                                }
                                @Name("roleList")
                                public class RoleList extends EntityQuery {
                                
                                     private static final String[] RESTRICTIONS = {
                                               "lower(role.code) like concat(lower(#{roleList.role.code}),'%')",
                                               "lower(role.description) like concat(lower(#{roleList.role.description}),'%')",
                                               "lower(role.name) like concat(lower(#{roleList.role.name}),'%')",};
                                
                                     private Role role = new Role();
                                
                                     @Override
                                     public String getEjbql() {
                                          return "select role from Role role";
                                     }
                                
                                     @Override
                                     public Integer getMaxResults() {
                                          return 25;
                                     }
                                
                                     public Role getRole() {
                                          return role;
                                     }
                                
                                     @Override
                                     public List<String> getRestrictions() {
                                          return Arrays.asList(RESTRICTIONS);
                                     }
                                
                                }
                                
                                @Name("userHome")
                                public class UserHome extends EntityHome<User> {
                                
                                     public void setUserId(Long id) {
                                          setId(id);
                                     }
                                
                                     public Long getUserId() {
                                          return (Long) getId();
                                     }
                                
                                     @Override
                                     protected User createInstance() {
                                          User user = new User();
                                          return user;
                                     }
                                
                                     public void wire() {
                                     }
                                
                                     public boolean isWired() {
                                          return true;
                                     }
                                
                                     public User getDefinedInstance() {
                                          return isIdDefined() ? getInstance() : null;
                                     }
                                }
                                


                                • 13. Re: how to use s:selectItems with h:selectManyCheckbox

                                  Hint: Paste the code, mark it, select Code block in the message dropdown. or just put one


                                  `  before and one ` after your code, which will show


                                  before and one



                                  in the preview. 


                                  (I did the same mistake the first time too)

                                  • 14. Re: how to use s:selectItems with h:selectManyCheckbox
                                    nickarls

                                    Daniel Roth wrote on Mar 11, 2008 09:34 AM:


                                    Hint: Paste the code, mark it, select Code block in the message dropdown.


                                    Offer not valid on IE7 currently.

                                    1 2 3 Previous Next