简体   繁体   中英

SQL Query error in JSF, JPA application. javax.ejb.EJBException

I am trying to get a count of Users from my Manager, passing the value from the jsf side.

// important part of the Jsf

h:outputText value="#{bookProviderBean.getCoursesAddedByTP(101)}

// important part of the Bean

public IBOOKProviderManager getBookProviderrManager() {
    return bookProviderManager;
}

public long getCoursesAddedByTP(Integer bookProviderId){
    return courseManager.getCoursesAddedByTP(bookProviderId);
}

// important part of the Manager

public long getCoursesAddedByTP(Integer bookProviderId){

    Query query = this.em.createQuery(

    " SELECT COUNT(*) AS total FROM Courses c WHERE c.book_provider_id = " +bookProviderId);

    long coursesByTP = 0;

    try {
         coursesByTP = (Long) query.getSingleResult();
    } catch (Exception e) {
        e.getStackTrace();
    }

    return coursesByTP;
}
  • I also tried passing the parameter like so:

" SELECT COUNT(*) AS total FROM Courses WHERE book_provider_id = :tpID "); query.setParameter("tpID", bookProviderId);

This is the error generated:

Caused by: java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: Exception Description: Syntax error parsing [ SELECT COUNT(*) AS total FROM Courses c WHERE c.book_provider_id = 101]. [13, 13] The left expression is missing from the arithmetic expression. [14, 14] The right expression is missing from the arithmetic expression.

Thanks a lot

The EJB container encountered problems in the syntax of the JPQL query. To count the number of courses according to a book provider, you can use the usual method size() of the collection:

public long getCoursesAddedByTP(Integer bookProviderId){
    Query query = this.em.createQuery(    
        "SELECT c FROM Courses c WHERE c.book_provider_id = :tpID");   
    query.setParameter("tpID", bookProviderId);
    long coursesByTP = 0;
    try {
         coursesByTP = query.getResultList().size();  // getResultList() and not getSingleResult()
    } catch (Exception e) {
        e.getStackTrace();
    }
    return coursesByTP;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM