0 Replies Latest reply on May 3, 2006 8:57 AM by Ben Kyrlach

    Yet another newbie question...

    Ben Kyrlach Newbie

      Okay, I think I'm starting to figure things out. However, now I'm having problems with a one to many relationship. Here are some source snippets...

      (from User.java)

      @NotNull @ManyToOne @JoinColumn(name="Id", nullable=false)
      public Role getRole()
      {
      return role;
      }

      public void setRole(Role role)
      {
      this.role = role;
      }

      (from Role.java)

      @Entity
      @Name("role")
      @Scope(EVENT)
      @Table(name="roles")
      public class Role implements Serializable
      {
      private String name;
      private Integer id;

      public Role(String name, Integer id)
      {
      this.name = name;
      this.id = id;
      }

      public Role() {}

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

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

      @NotNull @Length(min=1, max=15)
      public String getName() {
      return name;
      }
      public void setName(String name) {
      this.name = name;
      }

      public String toString()
      {
      return "Role(" + name + ")";
      }
      }

      (from YaRBImpl.java)

      @Stateful
      @Name("yarb")
      @Scope(APPLICATION)
      @Interceptors(SeamInterceptor.class)
      public class YaRBImpl implements YaRB
      {
      List roles;
      Map<String,Role> roleMap;

      @PersistenceContext
      EntityManager em;

      @Create
      public void loadData()
      {
      roles = em.createQuery("from Role r").getResultList();

      Map<String,Role> results = new TreeMap<String,Role>();

      for(Role role: roles)
      {
      results.put(role.getName(), role);
      }
      roleMap = results;
      }

      public Map<String,Role> getRoles()
      {
      return roleMap;
      }

      public Converter getConverter()
      {
      return new RoleConverter(roles);
      }

      public Role getNullRole()
      {
      return new Role();
      }

      static public class RoleConverter implements Converter, Serializable
      {
      List roles;

      public RoleConverter(List roles)
      {
      this.roles = roles;
      }

      public String getAsString(FacesContext facesContext, UIComponent component, Object obj) throws ConverterException
      {
      if (obj == null) return null;

      return String.valueOf(((Role) obj).getId());
      }

      public Object getAsObject(FacesContext facesContext, UIComponent component, String str) throws ConverterException
      {
      if(str == null || str.length()==0) return null;

      Integer id = new Integer(str);
      for (Role role: roles)
      {
      if(role.getId().equals(id))
      {
      return role;
      }
      }

      return null;
      }
      }
      }

      (from SSBImpl.java)
      @Stateless
      @Name("ssb")
      @Interceptors(SeamInterceptor.class)
      public class SSBImpl implements SSB
      {
      @In(required=false) @Valid
      private User user;

      @In(required=false) @Valid
      private Role role;

      @PersistenceContext
      private EntityManager em;

      @IfInvalid(outcome="index")
      public String invoke()
      {
      List existing = em.createQuery("select username from User where username=:username").setParameter("username", user.getUsername()).getResultList();
      if(existing.size()==0)
      {
      em.persist(user);
      return "go";
      }
      else
      {
      return "oops";
      }
      }

      @IfInvalid(outcome="role")
      public String createRole()
      {
      List existing = em.createQuery("select name from Role where name=:name").setParameter("name", role.getName()).getResultList();
      if(existing.size()==0)
      {
      em.persist(role);
      return "bob";
      }
      else
      {
      return "role";
      }
      }
      }

      (from index.xhtml)

      <h:selectOneListbox value="#{user.role}" converter="#{yarb.converter}">
      <f:selectItems value="#{yarb.roles}"/>
      </h:selectOneListbox>
      <h:messages/>
      <h:commandButton type="submit" value="Submit" action="#{ssb.invoke}"/>
      <h:commandButton type="submit" value="Search" action="#{userlist.listUsers}"/>
      </h:form>

      It lists my roles correctly, and I can see that my converter is actually returning a role object when I try and submit the form. However, it's just returning me to index.xhtml with a rather generic validation error. If I comment out the snippet from index.xhtml that I provided above, it works, but I don't get any role (obviously). What am I doing wrong?