Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
175 views
in Technique[技术] by (71.8m points)

java - How to display many to many jpa collection in jsf?

i made many to many join table between users and groups tables . so i have a collection in each entitie ( Users and groups )

@ManyToMany(mappedBy = "usersCollection")
    private Collection<Groups> groupsCollection;

and i want to display groups collection in Jsf thats what i did :

<p:dataTable var="user" value="#{usergestion.tableusers}">  
                           <p:column headerText="username">  
                               <h:outputText value="#{user.username}" />  
                           </p:column>  

                           <p:column headerText="nom">  
                               <h:outputText value="#{user.nom}" />  
                           </p:column>  

                           <p:column headerText="prenom">  
                               <h:outputText value="#{user.prenom}" />  
                           </p:column>

                            <p:column headerText="groupe"> 

                                <h:outputText value="#{user.groupsCollection.get(0)}"  />

                           </p:column>

and that what i get : enter image description here

how i can get just the nombre not com.database.Groups[ idGroups=2 ] ???

Solution :

i used : <h:outputText value="#{user.groupsCollection.get(0).groupname}" />

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You normally use an iterating component such as <ui:repeat> or <h:dataTable> to iterate over a collection. You can perfectly nest it inside another iterating component.

E.g.

<p:column headerText="groupe"> 
    <ui:repeat value="#{user.groupsCollection}" var="groups">
        <h:outputText value="#{groups.groupname}" /><br/>
    </ui:repeat>
</p:column>

Unrelated to the concrete problem, you've there some poor naming convention. One group should be represented by a class named Group, not Groups. I suggest to rename the one and other so that the code becomes so much more self documenting:

<p:column headerText="groups"> 
    <ui:repeat value="#{user.groups}" var="group">
        <h:outputText value="#{group.name}" /><br/>
    </ui:repeat>
</p:column>

The plural s in the field/variable name should already be indicative that it concerns a collection.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...