Class JRJpaQueryExecuter

  • All Implemented Interfaces:
    JRQueryExecuter

    public class JRJpaQueryExecuter
    extends JRAbstractQueryExecuter
    EJBQL query executer that uses the Java Persistence API.

    To use EJBQL in queries, an jakarta.persistence.EntityManager is needed. When running or filling reports the em need to be supplied with the named parameter EjbqlConstants.PARAMETER_JPA_ENTITY_MANAGER.

    Example:

     Map parameters = new HashMap();
     EntityManager em = emf.createEntityManager();
     parameters.put(EjbqlConstants.PARAMETER_JPA_ENTITY_MANAGER, em);
     JasperRunManager.runReportToPdfFile(fileName, parameters);
     

    When dealing with large result sets, pagination can be used by setting the PROPERTY_JPA_QUERY_PAGE_SIZE property in the report template.

    Example:

     <property name="net.sf.jasperreports.ejbql.query.page.size" value="100"/>
     

    Implementation-specific query hints can be set either using report properties in the report template, or by supplying the named parameter EjbqlConstants.PARAMETER_JPA_QUERY_HINTS_MAP containing a java.util.Map with named/value query hints.

    Example using report property:

     <property name="net.sf.jasperreports.ejbql.query.hint.fetchSize" value="100"/>
     
    The name of the query hint need to be prefixed with net.sf.jasperreports.ejbql.query.hint.. Above example will set a query hint with the name fetchSize and the String value 100.

    Example using map:

     Map hints = new HashMap();
     hints.put("fetchSize", 100);
     hints.put("anyName", anyObject());
     Map parameters = new HashMap();
     EntityManager em = emf.createEntityManager();
     parameters.put(EjbqlConstants.PARAMETER_JPA_ENTITY_MANAGER, em);
     parameters.put(EjbqlConstants.PARAMETER_JPA_QUERY_HINTS_MAP, hints);
     JasperRunManager.runReportToPdfFile(fileName, parameters);
     
    Note that when using report properties only String values can be set as query hint. When using a query hints map, any Object can be set as value.
    Author:
    Marcel Overdijk (marceloverdijk@hotmail.com)
    See Also:
    JRJpaQueryExecuterFactory
    • Field Detail

      • PROPERTY_JPA_QUERY_PAGE_SIZE

        public static final String PROPERTY_JPA_QUERY_PAGE_SIZE
        Property specifying the number of result rows to be retrieved at once.

        Result pagination is implemented by jakarta.persistence.Query.setFirstResult() and javax.persistence.Query.setMaxResults().

        By default, all the rows are retrieved (no result pagination is performed).

        See Also:
        Constant Field Values
      • PROPERTY_JPA_QUERY_HINT_PREFIX

        public static final String PROPERTY_JPA_QUERY_HINT_PREFIX
        Property specifying the prefix for EJBQL query hints.
        See Also:
        Constant Field Values
    • Method Detail

      • getCanonicalQueryLanguage

        protected String getCanonicalQueryLanguage()
        Description copied from class: JRAbstractQueryExecuter
        Returns a canonical query language for this query executer implementation.

        The canonical language is used to retrieve extensions for the query executer.

        The default implementation returns the runtime query language used in the dataset, but query executer implementations should override this method and return a fixed language.

        Overrides:
        getCanonicalQueryLanguage in class JRAbstractQueryExecuter
        Returns:
        a canonical query language
      • createQuery

        protected void createQuery​(String queryString)
        Creates the EJBQL query object.
        Parameters:
        queryString - the query string
      • createResultDatasource

        protected JRDataSource createResultDatasource()
        Creates a data source out of the query result.
        Returns:
        the data source
      • close

        public void close()
        Description copied from interface: JRQueryExecuter
        Closes resources kept open during the data source iteration.

        This method is called after the report is filled or the dataset is iterated. If a resource is not needed after the data source has been created, it should be released at the end of createDatasource.

      • cancelQuery

        public boolean cancelQuery()
                            throws JRException
        Description copied from interface: JRQueryExecuter
        Cancels the query if it's currently running.

        This method will be called from a different thread if the client decides to cancel the filling process.

        Returns:
        true if and only if the query was running and it has been canceled
        Throws:
        JRException
      • getEjbqlParameterName

        protected String getEjbqlParameterName​(String parameterName)
      • getResultList

        public List<?> getResultList()
        Runs the query by calling jakarta.persistence.Query.getResultList.

        All the result rows are returned.

        Returns:
        the result of the query as a list
      • getResultList

        public List<?> getResultList​(int firstIndex,
                                     int resultCount)
        Returns a page of the query results by calling jakarta.persistence.Query.getResultList.
        Parameters:
        firstIndex - the index of the first row to return
        resultCount - the number of rows to return
        Returns:
        result row list