Class JpaExecutor
- All Implemented Interfaces:
Aware
,BeanFactoryAware
,InitializingBean
- entityClass
- JpQl Select Query
- Sql Native Query
- JpQl Named Query
- Sql Native Named Query
- delete the retrieved object
Message
payload.idExpression- Since:
- 2.2
- Author:
- Gunnar Hillert, Amol Nayak, Artem Bilan
-
Constructor Summary
ConstructorDescriptionJpaExecutor
(EntityManager entityManager) Constructor taking anEntityManager
directly.JpaExecutor
(EntityManagerFactory entityManagerFactory) Constructor taking anEntityManagerFactory
from which theEntityManager
can be obtained.JpaExecutor
(JpaOperations jpaOperations) If custom behavior is required a custom implementation ofJpaOperations
can be passed in. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Verify and sets the parameters.protected List<?>
doPoll
(ParameterSource jpaQLParameterSource, int firstResult, int maxNumberOfResults) executeOutboundJpaOperation
(Message<?> message) Execute the actual Jpa Operation.poll()
Execute the JPA operation.Execute a (typically retrieving) JPA operation.void
setBeanFactory
(BeanFactory beanFactory) void
setClearOnFlush
(boolean clearOnFlush) If set totrue
theEntityManager.clear()
will be called, and only if theEntityManager.flush()
was called after performing persistence operations.void
setDeleteAfterPoll
(boolean deleteAfterPoll) If set to 'true', the retrieved objects are deleted from the database upon being polled.void
setDeleteInBatch
(boolean deleteInBatch) If not set, this property defaults tofalse
, which means that deletion occurs on a per-object basis if a collection of entities is being deleted.void
setEntityClass
(Class<?> entityClass) Set the class type which is being used for retrieving entities from the database.void
setExpectSingleResult
(boolean expectSingleResult) This parameter indicates that only one result object shall be returned as a result from the executed JPA operation.void
setFirstResultExpression
(Expression firstResultExpression) Set the expression that will be evaluated to get the first result in the query executed.void
setFlush
(boolean flush) If set totrue
theEntityManager.flush()
will be called after persistence operation.void
setFlushSize
(int flushSize) If the provided value is greater than0
, thenEntityManager.flush()
will be called after persistence operations as well as within batch operations.void
setIdExpression
(Expression idExpression) Set the expression that will be evaluated to get theprimaryKey
forEntityManager.find(Class, Object)
.void
setIntegrationEvaluationContext
(EvaluationContext evaluationContext) void
setJpaParameters
(List<JpaParameter> jpaParameters) void
setJpaQuery
(String jpaQuery) void
setMaxNumberOfResults
(int maxNumberOfResults) Set the max number of results to retrieve from the database.void
setMaxResultsExpression
(Expression maxResultsExpression) Set the expression for maximum number of results expression.void
setNamedQuery
(String namedQuery) A named query can either refer to a named JPQL based query or a native SQL query.void
setNativeQuery
(String nativeQuery) You can also use native Sql queries to poll data from the database.void
setParameterSource
(ParameterSource parameterSource) Specify theParameterSource
that would be used to provide additional parameters.void
setParameterSourceFactory
(ParameterSourceFactory parameterSourceFactory) void
setPersistMode
(PersistMode persistMode) void
setUsePayloadAsParameterSource
(Boolean usePayloadAsParameterSource)
-
Constructor Details
-
JpaExecutor
Constructor taking anEntityManagerFactory
from which theEntityManager
can be obtained.- Parameters:
entityManagerFactory
- Must not be null.
-
JpaExecutor
Constructor taking anEntityManager
directly.- Parameters:
entityManager
- Must not be null.
-
JpaExecutor
If custom behavior is required a custom implementation ofJpaOperations
can be passed in. The implementations themselves typically provide access to theEntityManager
. See alsoDefaultJpaOperations
andAbstractJpaOperations
.- Parameters:
jpaOperations
- Must not be null.
-
-
Method Details
-
setIntegrationEvaluationContext
-
setEntityClass
Set the class type which is being used for retrieving entities from the database.- Parameters:
entityClass
- Must not be null.
-
setJpaQuery
- Parameters:
jpaQuery
- The provided JPA query must neither be null nor empty.
-
setNativeQuery
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.- Parameters:
nativeQuery
- The provided SQL query must neither be null nor empty.
-
setNamedQuery
A named query can either refer to a named JPQL based query or a native SQL query.- Parameters:
namedQuery
- Must neither be null nor empty
-
setPersistMode
-
setJpaParameters
-
setUsePayloadAsParameterSource
-
setFlush
public void setFlush(boolean flush) If set totrue
theEntityManager.flush()
will be called after persistence operation. Has the same effect, if theflushSize
is specified to1
. For convenience in cases when the provided entity to persist is not an instance ofIterable
.- Parameters:
flush
- defaults to 'false'.
-
setFlushSize
public void setFlushSize(int flushSize) If the provided value is greater than0
, thenEntityManager.flush()
will be called after persistence operations as well as within batch operations. This property has precedence over theflush
, if it is specified to a value greater than0
. If the entity to persist is not an instance ofIterable
and this property is greater than0
, then the entity will be flushed as if theflush
attribute was set totrue
.- Parameters:
flushSize
- defaults to '0'.
-
setClearOnFlush
public void setClearOnFlush(boolean clearOnFlush) If set totrue
theEntityManager.clear()
will be called, and only if theEntityManager.flush()
was called after performing persistence operations.- Parameters:
clearOnFlush
- defaults to 'false'.- See Also:
-
setDeleteInBatch
public void setDeleteInBatch(boolean deleteInBatch) If not set, this property defaults tofalse
, 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."
- Parameters:
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.- Parameters:
deleteAfterPoll
- Defaults to 'false'.
-
setParameterSourceFactory
- Parameters:
parameterSourceFactory
- Must not be null
-
setParameterSource
Specify theParameterSource
that would be used to provide additional parameters.- 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 totrue
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
istrue
, then aMessagingException
is thrown.If set to
false
, the complete result list is returned as the payload.- Parameters:
expectSingleResult
- true if a single object is expected.
-
setFirstResultExpression
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- Parameters:
firstResultExpression
- The first result expression.- See Also:
-
setIdExpression
Set the expression that will be evaluated to get theprimaryKey
forEntityManager.find(Class, Object)
.- Parameters:
idExpression
- the SpEL expression for entityprimaryKey
.- Since:
- 4.0
-
setMaxResultsExpression
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- Parameters:
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.- Parameters:
maxNumberOfResults
- Must not be negative.- See Also:
-
setBeanFactory
- Specified by:
setBeanFactory
in interfaceBeanFactoryAware
- Throws:
BeansException
-
afterPropertiesSet
public void afterPropertiesSet()Verify and sets the parameters. E.g. initializes the to be usedParameterSourceFactory
.- Specified by:
afterPropertiesSet
in interfaceInitializingBean
-
executeOutboundJpaOperation
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.
- Parameters:
message
- The message.- Returns:
- Either the number of affected entities when using a JPQL query. When using a merge/persist the updated/inserted itself is returned.
-
poll
Execute the JPA operation. Delegates topoll(Message)
.- Returns:
- The object or null.
-
poll
Execute a (typically retrieving) JPA operation. The requestMessage can be used to provide additional query parameters usingparameterSourceFactory
. If the requestMessage parameter is null thenparameterSource
is being used for providing query parameters.- Parameters:
requestMessage
- May be null.- Returns:
- The payload object, which may be null.
-
doPoll
protected List<?> doPoll(ParameterSource jpaQLParameterSource, int firstResult, int maxNumberOfResults)
-