0

I'm writing a JSF application which lets the user select a list of documents from a list of folders where the user clicks the specific folder link to see the list of documents related to the selected folder. The folder list is displayed from the following XHTML page:

<p:dataTable value="#{projectDocsFolderBean.listModel}" var="docs">
                    <p:column headerText="Folders:">
                        <h:commandLink action="#{projectDocBean.selectFolderDocs}" 
                                       value="#{docs.classification}" /> 
                    </p:column>

                </p:dataTable>

Where the classification field is supposed to display a list of folder names but gives this stack:

WARNING:   StandardWrapperValve[Faces Servlet]: Servlet.service() for servlet Faces Servlet threw exception
java.lang.NumberFormatException: For input string: "classification"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at javax.el.ArrayELResolver.toInteger(ArrayELResolver.java:378)
at javax.el.ArrayELResolver.getValue(ArrayELResolver.java:198)
at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)....

I'm almost sure that this is caused by the use of explicit Select statements in my DAO class (where I'm selecting specific entity fields instead of the whole entiy), so that the results are returned as an array. If I try to call docs[0] in the JSF data table, the list displays fine, but I want to be able to explicitly call docs.classification instead of using array indices.

The below question does not adequately answer MY question because I do not want to use array indices but rather explicit EL properties (ie: docs.classification NOT docs[0]). In this case, I would like to know 1) What the appropriate result class should look like (see my below attempt)? 2) Do I need to include a custom conversion class in addition or instead of the defined result class?

Showing Hibernate/JPA results in JSF datatable causes: java.lang.NumberFormatException: For input string: "[propertyname]"

The above is helpful but I already understand that the structure of my query returns a tuple and my question as above is if there is a way to get around that without having to use array indices.

After trying conventional ORM methods, I tried to define the following result class:

 @SuppressWarnings("unchecked")
public List<ProjectDocs> findAllFolders(int projectid) {
    final String SQLCOMMAND
            = "SELECT DISTINCT d.classification, d.fk_projectdoc_id " + "FROM "
            + ProjectDocs.class.getSimpleName() + " d "
            + " WHERE d.fk_projectdoc_id = ?1";

    Query query = em.createQuery(SQLCOMMAND);
    query.setParameter(1, projectid);
    try {
      //  ListDataModel<ProjectDocs> lresults = new ListDataModel<ProjectDocs>(query.getResultList());
  List<ProjectDocs> lresults = query.getResultList();
        return lresults;
    } catch (Exception ex) {
        throw new RuntimeException("FAILED", ex);
    }
}

And the JSF Managed bean class is as follows (I also tried using a CDI bean but this product a NPE):

@ManagedProperty(value = "#{projBB}")
// @Model

private ProjectsBB projectBean;

 public String selectFolders() {

    projects = projectBean.getProjectList().getRowData();
    projectid = projects.getProjectid();
    projects.setProjectid(projectid);
    listModel = (ListDataModel<ProjectDocs>) getFolderBaseModel();
    return "ProDocsFolderList";

}


   public DataModel<ProjectDocs> getFolderBaseModel() {

    /* Check cache */
    if (this.folderBaseModel == null) {
        this.folderBaseModel= buildFolderListModel(this.projectid);
    }
    return this.folderBaseModel;
}

public DataModel<ProjectDocs> buildFolderListModel(int projectid) {
    List<ProjectDocs> folderList = proDocs.findFolderList(projectid);
    DataModel<ProjectDocs> model = new ListDataModel<ProjectDocs>(folderList);
    return model;
}

Thank you!

Community
  • 1
  • 1
jay tai
  • 419
  • 1
  • 17
  • 35
  • 2
    The query returns a tuple (which is not a fully qualified entity) in the form of a `List` of `Object` array (`javax.el.ArrayELResolver.toInteger(ArrayELResolver.java:378)`) which is not associated with any property of the entity. Thus, there is no question of referring to them using expressions like `#{docs.classification}` (see the exception stack-trace) You answered the question on your own, "*If I try to call `docs[0]` in the JSF data table, the list displays fine*". Specify a result class as you mentioned in the last part of the question, if you want to have property-like expressions in EL – Tiny Apr 17 '15 at 21:23
  • I found a similar question [here](http://stackoverflow.com/q/4412171/1391249). – Tiny Apr 17 '15 at 21:42
  • Thanks a lot for the helpful comments Tiny! Yes I've seen that question and I guess answered a large part of this question myself. I think where I am at is in searching for information about what a result class should look like and how it should fit into my ORM. Maybe I will update my question with an example of the type of class I am researching. Otherwise I found your comments very helpful but I don't appear to be able to upvote your comment? – jay tai Apr 18 '15 at 10:07
  • As per your edit: that's not a JSF problem. Just ask a new question specifically to JPA users how to obtain the result of a SELECT DISTINCT into a list of entities instead of a tulpes. That's all. You already know how to present one or other kind of model in JSF, so you don't have a JSF problem at all. Putting JSF code in question anyway would only end up in a very distracting question to JPA users. – BalusC Apr 20 '15 at 10:17
  • You need constructor expressions to map a result class to the list of tuples supposed to be returned. If you need an example, [Here](http://stackoverflow.com/q/12271305/1391249) you may go. Create a new class with appropriate properties corresponding to the list of fields/columns in the `SELECT` clause of the query (JPQL), respective getters & setters and appropriate constructor(s) matching the result list of the query. Both the list of fields specified in the query statement and corresponding constructor arguments in the result class must precisely match in number, order and type (datatype). – Tiny Apr 20 '15 at 14:42

0 Answers0