Package com.axelor.db

Class Query<T extends Model>

    • Constructor Detail

      • Query

        public Query​(Class<T> beanClass)
        Create a new instance of Query with given bean class.
        Parameters:
        beanClass - model bean class
    • Method Detail

      • em

        protected javax.persistence.EntityManager em()
      • getFilter

        protected String getFilter()
      • setFilter

        protected void setFilter​(String filter)
      • getParams

        protected Object[] getParams()
      • setParams

        protected void setParams​(Object[] params)
      • setNamedParams

        protected void setNamedParams​(Map<String,​Object> namedParams)
      • filter

        public Query<T> filter​(String filter,
                               Object... params)
        A convenient method to filter the query using JPQL's where clause.

        The filter string should refer the field names with self. prefix and values should not be embedded into the filter string but should be passed by parameters and ? placeholder should be used to mark parameter substitutions.

        Here is an example:

         Query<Person> query = Query.of(Person);
         query = query.filter("self.name = ? AND self.age >= ?", "some", 20);
        
         List<Person> matched = query.fetch();
         

        This is equivalent to:

         SELECT self from Person self WHERE (self.name = ?1) AND (self.age >= ?2)
         

        The params passed will be added as positional parameters to the JPA query object before performing fetch().

        Parameters:
        filter - the filter string
        params - the parameters
        Returns:
        the same instance
      • fixPlaceholders

        protected String fixPlaceholders​(String filter)
      • order

        public Query<T> order​(String spec)
        Set order by clause for the query. This method can be chained to provide multiple fields.

        The spec is just a field name for ASC or should be prefixed with - for DESC clause.

        For example:

         Query<Person> query = Query.of(Person);
         query = query.filter("name =", "some").filter("age >=", 20)
                .filter("lang in", "en", "hi");
        
         query = query.order("name").order("-age");
         

        This is equivalent to:

         SELECT p from Person p WHERE (p.name = ?1) AND (p.age >= ?2) AND (lang IN (?3, ?4)) ORDER BY p.name, p.age DESC
         
        Parameters:
        spec - order spec
        Returns:
        the same query instance
      • cacheable

        public Query<T> cacheable()
        Set the query result cacheable.
        Returns:
        the same query instance
      • readOnly

        public Query<T> readOnly()
        Set the query readonly.
        Returns:
        the same query instance.
      • translate

        public Query<T> translate​(boolean translate)
        Set whether to use translation join.
        Parameters:
        translate -
        Returns:
      • translate

        public Query<T> translate()
        Use translation join.
        Returns:
        the same query instance.
      • autoFlush

        public Query<T> autoFlush​(boolean auto)
      • fetchStream

        public Stream<T> fetchStream()
        Fetch all the matched records as Stream.

        Recommended only when dealing with large data, for example, batch processing. For normal use cases, the fetch() is more appropriate.

        Also configure hibernate.jdbc.fetch_size (default is 20) to fine tune the fetch size.

        Returns:
        stream of matched records.
        See Also:
        fetchStream(int), fetchStream(int, int)
      • fetchStream

        public Stream<T> fetchStream​(int limit)
        Fetch the matched records as Stream with the given limit.

        Recommended only when dealing with large data, for example, batch processing. For normal use cases, the fetch() is more appropriate.

        Also configure hibernate.jdbc.fetch_size (default is 20) to fine tune the fetch size.

        Parameters:
        limit - the limit
        Returns:
        stream of matched records within the limit
        See Also:
        fetchStream(int, int)
      • fetchStream

        public Stream<T> fetchStream​(int limit,
                                     int offset)
        Fetch the matched records as Stream within the given range.

        Recommended only when dealing with large data, for example, batch processing. For normal use cases, the fetch() is more appropriate.

        Also configure hibernate.jdbc.fetch_size (default is 20) to fine tune the fetch size.

        Parameters:
        limit - the limit
        offset - the offset
        Returns:
        stream of matched records within the range
      • fetch

        public List<T> fetch()
        Fetch all the matched records.
        Returns:
        list of all the matched records.
      • fetch

        public List<T> fetch​(int limit)
        Fetch the matched records with the given limit.
        Parameters:
        limit - the limit
        Returns:
        matched records within the limit
      • fetch

        public List<T> fetch​(int limit,
                             int offset)
        Fetch the matched records within the given range.
        Parameters:
        limit - the limit
        offset - the offset
        Returns:
        list of matched records within the range
      • fetchOne

        public T fetchOne()
        Fetch the first matched record.
        Returns:
        the first matched record.
      • fetchOne

        public T fetchOne​(int offset)
        Fetch a matched record at the given offset.
        Parameters:
        offset - the offset
        Returns:
        the matched record at given offset
      • count

        public long count()
        Returns the number of total records matched.
        Returns:
        total number
      • select

        public Query.Selector select​(String... names)
        Return a selector to select records with specific fields only.
        Parameters:
        names - field names to select
        Returns:
        a new instance of Query.Selector
      • update

        public int update​(Map<String,​Object> values)
        Perform mass update on the matched records with the given values.
        Parameters:
        values - the key value map
        Returns:
        total number of records updated
      • update

        public int update​(String name,
                          Object value)
        This is similar to update(Map) but updates only single field.
        Parameters:
        name - the field name whose value needs to be changed
        value - the new value
        Returns:
        total number of records updated
      • update

        public int update​(Map<String,​Object> values,
                          User updatedBy)
        Perform mass update on matched records with the given values.

        If updatedBy user is null, perform non-versioned update otherwise performed versioned update.

        Parameters:
        values - the key value map
        updatedBy - the user to set "updatedBy" field
        Returns:
        total number of records updated
      • update

        public int update​(String name,
                          Object value,
                          User updatedBy)
        This is similar to update(Map, User) but updates only single field.
        Parameters:
        name - the field name whose value needs to be changed
        value - the new value
        updatedBy - the user to set "updatedBy" field
        Returns:
        total number of records updated
      • delete

        public int delete()
        Bulk delete all the matched records.

        This method uses DELETE query and performs Query.executeUpdate().
        Returns:
        total number of records affected.
        See Also:
        remove()
      • remove

        public long remove()
        Remove all the matched records.

        In contrast to the delete() method, it performs EntityManager.remove(Object) operation by fetching objects in pages (100 at a time).
        Returns:
        total number of records removed.
        See Also:
        delete()
      • selectQuery

        protected String selectQuery​(boolean update)
      • selectQuery

        protected String selectQuery()
      • updateQuery

        protected String updateQuery()
      • countQuery

        protected String countQuery()
      • deleteQuery

        protected String deleteQuery​(String filter)
      • bind

        protected QueryBinder bind​(javax.persistence.Query query)
      • bind

        public Query<T> bind​(Map<String,​Object> params)
        Bind the named parameters of the query with the given values. Named parameter must me set after query is filtered.
        Parameters:
        params - mapping for named params.
        Returns:
        the same instance
      • bind

        public Query<T> bind​(String name,
                             Object value)
        Bind the given named parameter of the query with the given value.
        Parameters:
        name - the named parameter to bind
        value - the parameter value
        Returns:
        the same instance