Class JpaExecutor

All Implemented Interfaces:
Aware, BeanFactoryAware, InitializingBean

public class JpaExecutor extends Object implements InitializingBean, BeanFactoryAware
Executes Jpa Operations that produce payload objects from the result of the provided:
  • entityClass
  • JpQl Select Query
  • Sql Native Query
  • JpQl Named Query
  • Sql Native Named Query
. When objects are being retrieved, it also possibly to:
  • delete the retrieved object
If neither entityClass nor any other query is specified then the entity-class is "guessed" from the Message payload.idExpression
Gunnar Hillert, Amol Nayak, Artem Bilan
  • Constructor Details

    • JpaExecutor

      public JpaExecutor(EntityManagerFactory entityManagerFactory)
      Constructor taking an EntityManagerFactory from which the EntityManager can be obtained.
      entityManagerFactory - Must not be null.
    • JpaExecutor

      public JpaExecutor(EntityManager entityManager)
      Constructor taking an EntityManager directly.
      entityManager - Must not be null.
    • JpaExecutor

      public JpaExecutor(JpaOperations jpaOperations)
      If custom behavior is required a custom implementation of JpaOperations can be passed in. The implementations themselves typically provide access to the EntityManager. See also DefaultJpaOperations and AbstractJpaOperations.
      jpaOperations - Must not be null.
  • Method Details

    • setIntegrationEvaluationContext

      public void setIntegrationEvaluationContext(EvaluationContext evaluationContext)
    • setEntityClass

      public void setEntityClass(Class<?> entityClass)
      Set the class type which is being used for retrieving entities from the database.
      entityClass - Must not be null.
    • setJpaQuery

      public void setJpaQuery(String jpaQuery)
      jpaQuery - The provided JPA query must neither be null nor empty.
    • setNativeQuery

      public void setNativeQuery(String nativeQuery)
      You can also use native Sql queries to poll data from the database. If set this property will allow you to use native SQL. Optionally you can also set the entityClass property at the same time. If specified the entityClass will be used as the result class for the native query.
      nativeQuery - The provided SQL query must neither be null nor empty.
    • setNamedQuery

      public void setNamedQuery(String namedQuery)
      A named query can either refer to a named JPQL based query or a native SQL query.
      namedQuery - Must neither be null nor empty
    • setPersistMode

      public void setPersistMode(PersistMode persistMode)
    • setJpaParameters

      public void setJpaParameters(List<JpaParameter> jpaParameters)
    • setUsePayloadAsParameterSource

      public void setUsePayloadAsParameterSource(Boolean usePayloadAsParameterSource)
    • setFlush

      public void setFlush(boolean flush)
      If set to true the EntityManager.flush() will be called after persistence operation. Has the same effect, if the flushSize is specified to 1. For convenience in cases when the provided entity to persist is not an instance of Iterable.
      flush - defaults to 'false'.
    • setFlushSize

      public void setFlushSize(int flushSize)
      If the provided value is greater than 0, then EntityManager.flush() will be called after persistence operations as well as within batch operations. This property has precedence over the flush, if it is specified to a value greater than 0. If the entity to persist is not an instance of Iterable and this property is greater than 0, then the entity will be flushed as if the flush attribute was set to true.
      flushSize - defaults to '0'.
    • setClearOnFlush

      public void setClearOnFlush(boolean clearOnFlush)
      If set to true the EntityManager.clear() will be called, and only if the EntityManager.flush() was called after performing persistence operations.
      clearOnFlush - defaults to 'false'.
      See Also:
    • setDeleteInBatch

      public void setDeleteInBatch(boolean deleteInBatch)
      If not set, this property defaults to false, which means that deletion occurs on a per-object basis if a collection of entities is being deleted.

      If set to 'true' the elements of the payload are deleted as a batch operation. Be aware that this exhibits issues in regard to cascaded deletes.

      The specification 'JSR 317: Java Persistence API, Version 2.0' does not support cascaded deletes in batch operations. The specification states in chapter 4.10:

      "A delete operation only applies to entities of the specified class and its subclasses. It does not cascade to related entities."

      deleteInBatch - Defaults to 'false' if not set.
    • setDeleteAfterPoll

      public void setDeleteAfterPoll(boolean deleteAfterPoll)
      If set to 'true', the retrieved objects are deleted from the database upon being polled. May not work in all situations, e.g. for Native SQL Queries.
      deleteAfterPoll - Defaults to 'false'.
    • setParameterSourceFactory

      public void setParameterSourceFactory(ParameterSourceFactory parameterSourceFactory)
      parameterSourceFactory - Must not be null
    • setParameterSource

      public void setParameterSource(ParameterSource parameterSource)
      Specify the ParameterSource that would be used to provide additional parameters.
      parameterSource - Must not be null.
    • setExpectSingleResult

      public void setExpectSingleResult(boolean expectSingleResult)
      This parameter indicates that only one result object shall be returned as a result from the executed JPA operation. If set to true and the result list from the JPA operations contains only 1 element, then that 1 element is extracted and returned as payload.

      If the result map contains more than 1 element and expectSingleResult is true, then a MessagingException is thrown.

      If set to false, the complete result list is returned as the payload.

      expectSingleResult - true if a single object is expected.
    • setFirstResultExpression

      public void setFirstResultExpression(Expression firstResultExpression)
      Set the expression that will be evaluated to get the first result in the query executed. If a null expression is set, all the results in the result set will be retrieved
      firstResultExpression - The first result expression.
      See Also:
    • setIdExpression

      public void setIdExpression(Expression idExpression)
      Set the expression that will be evaluated to get the primaryKey for EntityManager.find(Class, Object).
      idExpression - the SpEL expression for entity primaryKey.
    • setMaxResultsExpression

      public void setMaxResultsExpression(Expression maxResultsExpression)
      Set the expression for maximum number of results expression. It has to be a non-null value Not setting one will default to the behavior of fetching all the records
      maxResultsExpression - The maximum results expression.
    • setMaxNumberOfResults

      public void setMaxNumberOfResults(int maxNumberOfResults)
      Set the max number of results to retrieve from the database. Defaults to 0, which means that all possible objects shall be retrieved.
      maxNumberOfResults - Must not be negative.
      See Also:
    • setBeanFactory

      public void setBeanFactory(BeanFactory beanFactory) throws BeansException
      Specified by:
      setBeanFactory in interface BeanFactoryAware
    • afterPropertiesSet

      public void afterPropertiesSet()
      Verify and sets the parameters. E.g. initializes the to be used ParameterSourceFactory.
      Specified by:
      afterPropertiesSet in interface InitializingBean
    • executeOutboundJpaOperation

      public Object executeOutboundJpaOperation(Message<?> message)
      Execute the actual Jpa Operation. Call this method, if you need access to process return values. These methods return a Map that contains either the number of affected entities or the affected entity itself.

      Keep in mind that the number of entities effected by the operation may not necessarily correlate with the number of rows effected in the database.

      message - The message.
      Either the number of affected entities when using a JPQL query. When using a merge/persist the updated/inserted itself is returned.
    • poll

      @Nullable public Object poll()
      Execute the JPA operation. Delegates to poll(Message).
      The object or null.
    • poll

      @Nullable public Object poll(@Nullable Message<?> requestMessage)
      Execute a (typically retrieving) JPA operation. The requestMessage can be used to provide additional query parameters using parameterSourceFactory. If the requestMessage parameter is null then parameterSource is being used for providing query parameters.
      requestMessage - May be null.
      The payload object, which may be null.
    • doPoll

      protected List<?> doPoll(ParameterSource jpaQLParameterSource, int firstResult, int maxNumberOfResults)