P: dataList cannot iterate java.util.Set

I have a data sheet of primfaces within the boundaries of a dataGrid, but I have a problem mapping with a property of the nested collection dataList (java.util.Set). When I refer to any attribute on a nested Set (dream.tag), I get an exception:

javax.servlet.ServletException: /registered/modify.xhtml @ 42,48 value = "# {tag.id}": The 'id' property was not found in type org.hibernate.collection.PersistentSet.

This attribute is there, but the dream.tag attribute maps to the private Set tag. Is it possible to use the dataList component using Set. I copied the schema of my data model below. Thanks for the help!

<p:dataGrid var="dream" value="#{dreamModifyBean.dreams}" columns="5" rows="10" paginator="true" effect="true" paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}" rowsPerPageTemplate="10,15,20" paginatorPosition="bottom"> <p:column> <h:panelGrid columns="1" style="width:100%"> <h:outputText value="#{dream.order}. #{dream.title}"/><br/> <p:graphicImage value="#{dream.imageThumb}" width="125" height="100"/><br/> <h:outputText value="#{dream.notes}"/><br/> <p:dataList value="#{dream.tag}" var="tag"> <h:outputText value="#{tag.id}"/> </p:dataList> <h:outputText value="#{bundle['dreamModify.cost.TEXT']} #{dream.cost}"/><br/> </h:panelGrid> </p:column> </p:dataGrid> 

Dream (dreamModifyBean.dreams) - Parent:

 public class Dream implements Serializable{ @OneToMany(fetch=FetchType.EAGER,cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}) @JoinColumn(name="DREAM_ID") private Set<Tag> tag; public Set<Tag> getTag() { return tag; } public void setTag(Set<Tag> tag) { this.tag = tag; } } 

Tag (dream.tag) - Child

 public class Tag { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Basic(optional=false) @Column(name="ID") private Long id; @Basic(optional=false) @Column(name="DESCRIPTION") private String description; @ManyToOne(fetch=FetchType.EAGER) @JoinColumn(name="DREAM_ID") private Dream dream; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public Dream getDream() { return dream; } public void setDream(Dream dream) { this.dream = dream; } } 
+4
source share
3 answers

Try using List . The value attribute must be one of the following, as described here :

  • beans list
  • beans array
  • one bean
  • javax.faces.model.DataModel object
  • java.sql.ResultSet object
  • javax.servlet.jsp.jstl.sql.Result object
  • javax.sql.RowSet object
+9
source

You can use the toArray () method for Set:

 <p:dataList value="#{dream.tag.toArray}" var="tag"> <h:outputText value="#{tag.id}" /> </p:dataList> 
+2
source

I have exactly the same problem, and I suppose a mistake in implementing the correct concepts (she clearly states that she iterates through collections). BTW seems to fail while it displays the end tag, not the list itself.

A (not completely satisfying) workaround is to add a helper method:

 public List<Tag> getTagsAsList() { return new ArrayList<Tag>(tags); } 

@Matt Handy: it can definitely be ordered, some implementations (e.g. TreeSet), others not (e.g. HashSet). The main difference between Set and List is the handling of duplicates.

Edit : I'm too quick to blame for this. It seems that the root of the problem lies in the JSF implementation itself, which does not provide a DataModel for collections. See here for more details.

+1
source

Source: https://habr.com/ru/post/1343269/


All Articles