سؤال

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>
...
هل كانت مفيدة؟

المحلول

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>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top