I have a problem with the primefaces dataTable component. I dont know why it does not short the data in the table when i click on it.
<p:dataTable var="garbage" value="#{resultsController.allGarbage}" dynamic="true" paginator="true" paginatorPosition="bottom" rows="10"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="5,10,15">
<p:column sortBy="#{garbage[0].filename}">
<f:facet name="header">
<h:outputText value="Filename" />
</f:facet>
<h:outputText value="#{garbage[0]}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Description" />
</f:facet>
<h:outputText value="#{garbage[1]}" />
</p:column>
<p:column sortBy="#{garbage[2].uploadDate}">
<f:facet name="header">
<h:outputText value="Upload date" />
</f:facet>
<h:outputText value="#{garbage[2]}" />
</p:column>
</p:dataTable>
This is the managed bean
@ManagedBean
@RequestScoped
public class ResultsController {
@EJB
private ISearchEJB searchEJB;
private Garbage[] garbage;
public List<Garbage[]> getAllGarbage() {
return searchEJB.findAllGarbage();
}
public Garbage[] getGarbage() {
System.out.println("VALUES!!!!!!!!" + garbage[0].getFilename());
return garbage;
}
public void setGarbage(Garbage[] garbage) {
this.garbage = garbage;
}
}
This is the EJB that allows data access
@Stateless(name = "ejbs/SearchEJB")
public class SearchEJB implements ISearchEJB {
@PersistenceContext
private EntityManager em;
public List<Garbage[]> findAllGarbage() {
Query query = em.createNamedQuery("findAllGarbage");
return query.getResultList();
}
}
And this is the entity(Data representation)
@NamedQuery(name = "findAllGarbage", query = "SELECT g.filename, g.description, g.uploadDate FROM Garbage g;")
@Entity
public class Garbage {
@Id
@GeneratedValue
@Column(nullable = false)
private Long id;
@Column(nullable = false)
private String filename;
@Column(nullable = false)
private String fileType;
@Column(nullable = false)
private String uploadDate;
@Column(nullable = false)
private String destroyDate;
@Lob
@Column(nullable = false)
private byte[] file;
@Column(nullable = false)
private String description;
...//Getters and Setters
As shown in the image there is no changes when the sort buttons are clicked:
This is what the console says:
SEVERE: Error in sorting
UPDATE
public List<Garbage> findAllGarbage() {
Query query = em.createNamedQuery("findAllGarbage");
List<Garbage> gList = new ArrayList();
for (Object o: query.getResultList()) {
Garbage tmpG = new Garbage();
tmpG.setFilename(((Garbage) o).getFilename());
tmpG.setUploadDate(((Garbage) o).getUploadDate());
tmpG.setDescription(((Garbage) o).getDescription());
gList.add(tmpG);
}
return gList;
}
The modified managed bean
@ManagedBean
@RequestScoped
public class ResultsController {
@EJB
private ISearchEJB searchEJB;
private Garbage garbage;
public List<Garbage> getAllGarbage() {
return searchEJB.findAllGarbage();
}
public Garbage getGarbage() {
return garbage;
}
public void setGarbage(Garbage garbage) {
this.garbage = garbage;
}
}
The modified JSF
<p:dataTable var="garbage" value="#{resultsController.allGarbage}" dynamic="true" paginator="true" paginatorPosition="bottom" rows="10"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="5,10,15">
<p:column sortBy="#{garbage.filename}" parser="string">
<f:facet name="header">
<h:outputText value="Filename" />
</f:facet>
<h:outputText value="#{garbage.filename}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Description" />
</f:facet>
<h:outputText value="#{garbage.description}" />
</p:column>
<p:column sortBy="#{garbage.uploadDate}" parser="string">
<f:facet name="header">
<h:outputText value="Upload date" />
</f:facet>
<h:outputText value="#{garbage.uploadDate}" />
</p:column>
</p:dataTable>