0

Evening!

I've got a DB structure which includes a join table (User-Groups) that relates the Users entity to Group entities for authorisation purposes. This is a many-to-many relationship (Users is the owner). Following hints & tips kindly provided by BalusC in other posts, I'm using the SelectItemsConverter custom converter provided by the Omnifaces framework which is allowing me to correctly populate the drop-down with Group entities but I can't figure out the correct syntax to then add these to the of Groups held by the Users entity. As things stand, the code will try to convert a Groups entity to a with terminal results! Having done a bit of digging, I'm wondering if I need to bind the reference to the view somehow and then pass the reference to the Groups add method when the form is submitted but this seems a little clumsy and I can't find anything that confirms my theory. Pointers anyone?

Users entity:

@Entity
@Table(name = "Users")
@XmlRootElement
...
public class Users implements Serializable {
...
    @JoinTable(name = "User_Groups", joinColumns = {
        @JoinColumn(name = "User_ID", referencedColumnName = "User_ID")}, inverseJoinColumns = {
        @JoinColumn(name = "Group_ID", referencedColumnName = "Group_ID")})
    @ManyToMany
    private Collection<Groups> groupsCollection;
 ...

Groups entity:

@Entity
@Table(name = "Groups")
...
public class Groups implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "Group_ID")
    private Integer groupID;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 20)
    @Column(name = "Group_Name")
    private String groupName;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 200)
    @Column(name = "Group_Desc")
    private String groupDesc;
    @ManyToMany(mappedBy = "groupsCollection")
    private Collection<Users> usersCollection;
...

Code fragment from Create Users JSF page:

...
<h:outputLabel value="#{bundle.CreateUsersLabel_groups}" for="groupsCollection" />
<h:selectOneMenu id="groupsCollection" value="#{usersController.selected.groupsCollection}" title="#{bundle.CreateUsersTitle_groups}" converter="omnifaces.SelectItemsConverter">
  <f:selectItems value="#{groupsController.itemsAvailableSelectOne}" />
</h:selectOneMenu>
...
rustproofFish
  • 931
  • 10
  • 32
  • You're not using `h:selectOneMenu` properly. That component returns a single selection, not a collection as you pretend (`#{usersController.selected.groupsCollection}`). – Aritz Oct 11 '13 at 17:45
  • Fair point. I am obviously doing something wrong but what do I use instead? I need to populate a Collection property in the Users field with one (or more) Groups entities. The SelectOneMenu (or alternative) needs to be populated with these entities, not the Collection. – rustproofFish Oct 11 '13 at 19:57

1 Answers1

0

I think what you need is a h:selectManyCheckbox, h:selectManyListbox or h:selectManyMenu, anything that allows you to select many groups at same time to associate them with your user. Try this:

<h:outputLabel value="#{bundle.CreateUsersLabel_groups}" for="groupsCollection" />
   <h:selectManyMenu id="groupsCollection" 
        value="#{usersController.selected.groupsCollection}" 
        title="#{bundle.CreateUsersTitle_groups}" converter="omnifaces.SelectItemsConverter">
   <f:selectItems value="#{groupsController.itemsAvailableSelectOne}" />
</h:selectManyMenu>
andremonteiro
  • 476
  • 2
  • 6
  • Bingo! It works. As with many of the problems I've come up against with JSF so far, the solution is annoyingly simple! However, is there a way of only allowing the user to select a single group? As it happens, this implementation is working fine for now but I'd like this flexibility for future development. Thanks again :-) My hair will have a chance to grow back now... – rustproofFish Oct 12 '13 at 10:45
  • If you want to allow the user to select a single group, then you should use the h:selectOneMenu. But, contrary to what you were doing, the "value" property of selectOneMenu must point a Entity, not a Collection. Something like – andremonteiro Oct 12 '13 at 14:56
  • Excellent. Thanks for the clarification - I shall give it a go – rustproofFish Oct 13 '13 at 10:53