Issue rendering JPQL custom query results into a JSF page

此生再无相见时 提交于 2019-12-11 10:56:29

问题


I have an entity with the following named query:

@NamedQuery(name = "findAllGarbage", query = "SELECT g.filename, g.description, g.uploadDate FROM Garbage g;")

The problem is that i want to pass that result to a dataTable to render, and i recieve NumberFormatException. I dont understand why because there are no numbers anywhere.

This is how the rest of the program looks like:

-The EJB that executes the query

@Stateless(name = "ejbs/SearchEJB")
public class SearchEJB implements ISearchEJB {

@PersistenceContext
private EntityManager em;


public List<Garbage> findAllGarbage() {
    Query query = em.createNamedQuery("findAllGarbage");    
    List<Garbage> tmpGarbage = query.getResultList();
    return tmpGarbage;
}

-The part of the JSF that displays the tableData:

<p:dataTable var="garbage" value="#{resultsController.allGarbage}" paginator="true" rows="10"  
             paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"  
             rowsPerPageTemplate="5,10,15">         

            <p:column>  
            <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>  
            <f:facet name="header">  
            <h:outputText value="Upload date" />  
            </f:facet>  
            <h:outputText value="#{garbage.uploadDate}" /> 
             </p:column>                
    </p:dataTable> 

-The managed bean that interacts with the JSF page:

@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 error says:

WARNING: StandardWrapperValve[Faces Servlet]: PWC1406: Servlet.service() for servlet Faces Servlet threw exception java.lang.NumberFormatException: For input string: "filename"

I dont understand what is wrong with the file name.

------------------------------------UPDATE------------------------------------

I changed the JSF to this, and now i dont see the error. I see tha table data but empty:

<p:dataTable var="garbage" value="#{resultsController.allGarbage}" paginator="true" rows="10"  
         paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"  
         rowsPerPageTemplate="5,10,15">         

        <p:column>  
        <f:facet name="header">  
        <h:outputText value="Filename" />  
        </f:facet>  
        <h:outputText value="#{garbage[4]}" />
         </p:column> 

        <p:column>  
        <f:facet name="header">  
        <h:outputText value="Description" />  
        </f:facet>  
        <h:outputText value="#{garbage[3]}" />  
         </p:column> 

        <p:column>  
        <f:facet name="header">  
        <h:outputText value="Upload date" />  
        </f:facet>  
        <h:outputText value="#{garbage[6]}" /> 
         </p:column>                
</p:dataTable> 

回答1:


The problem is caused by the fact that a query such as

SELECT g.filename, g.description, g.uploadDate FROM Garbage

returns an Object[] with filename, description and uploadDate as its elements.

If you want to access them as object properties (as you do in JSF), you need to query for the full object instead:

SELECT g FROM Garbage g


来源:https://stackoverflow.com/questions/5460476/issue-rendering-jpql-custom-query-results-into-a-jsf-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!