7 Replies Latest reply on Mar 30, 2007 9:30 AM by pmuir

    selectManyMenu validation error

    jonathan.shin

      I created EJB3 ManyToMany entities. User has a many to many relationship with Role. A user can have zero or more roles, and a role can have zero or more users. But, I?m getting validation error (?roles: Value is not a valid option?) when I clicked the ?Save? button.

      User Entity:

      @Entity
      @Table(name = "User")
      public class User implements java.io.Serializable {
      ...
      @ManyToMany
      @JoinTable(name="User_Role",
       joinColumns=@JoinColumn(name="user_id", referencedColumnName="user_id"),
       inverseJoinColumns=@JoinColumn(name="role_id", referencedColumnName="role_id")
       )
      public List<Role> getRoles() {
       return this.roles;
      }
      
      public void setRoles(List<Role> roles) {
       this.roles = roles;
      }
      }
      
      


      UserHome Object:
      @Name("userHome")
      public class UserHome extends EntityHome<User> {
      
       public void setUserUserId(Integer id) {
       setId(id);
       }
      
       public Integer getUserUserId() {
       return (Integer) getId();
       }
      
       @Override
       protected User createInstance() {
       User user = new User();
       return user;
       }
      ...
       public List<Role> getRoles() {
       return getInstance() == null ? null : new ArrayList<Role>(
       getInstance().getRoles());
       }
      
      }
      


      Role Entity:
      @Entity
      @Name("role")
      @Scope(SESSION)
      @Table(name = "Role")
      public class Role implements java.io.Serializable
      {
      
       private int roleId;
      
       private String name;
      
       private List<User> users;
      
       public Role()
       {
       System.out.println("Default Constructor");
       }
      ...
       @Column(name = "NAME", length = 50)
       @Length(max = 50)
       @NotNull
       public String getName()
       {
       return this.name;
       }
      
       public void setName(String name)
       {
       this.name = name;
       }
      
       @ManyToMany(mappedBy = "roles")
       public List<User> getUsers()
       {
       return this.users;
       }
      
       public void setUsers(List<User> users)
       {
       this.users = users;
       }
      }
      


      Converter:
      @Name("converters")
      public class Converters
      {
      
       @Transactional
       public Converter getRoleConverter() {
       return new Converter() {
      
       @Transactional
       public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) throws ConverterException
       {
       if (arg2 == null) {
       return null;
       }
       try {
       return ((EntityManager) Component.getInstance("entityManager")).find(Role.class, Integer.valueOf(arg2));
       } catch (NumberFormatException e) {
       throw new ConverterException("Cannot find selected Role", e);
       }
       }
      
       @Transactional
       public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) throws ConverterException
       {
       if (arg2 instanceof Role)
       {
       Role role = (Role) arg2;
       Integer tempId = (Integer) role.getRoleId();
       return tempId.toString();
       }
       else
       {
       return null;
       }
       }
      
       };
       }
      }
      


      UserEdit.xhtml view:
      ...
       <h:outputLabel for="role">
       Roles
       </h:outputLabel>
       <s:decorate id="roleDecoration">
       <h:selectManyMenu id="roles" value="#{userHome.instance.roles}" size="5" converter="#{converters.roleConverter}">
       <s:selectItems value="#{roleList.resultList}" var="role" label="#{role.name}" noSelectionLabel="Please Select..." hideNoSelectionLabel="true"/>
       </h:selectManyMenu>
       </s:decorate>
      ...
       <h:commandButton id="save"
       value="Save"
       action="#{userHome.persist}"
       disabled="#{!userHome.wired}"
       rendered="#{!userHome.managed}"/>
      ...