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
ConstructorsConstructorDescriptionJpaExecutor(EntityManager entityManager) Constructor taking anEntityManagerdirectly.JpaExecutor(EntityManagerFactory entityManagerFactory) Constructor taking anEntityManagerFactoryfrom which theEntityManagercan be obtained.JpaExecutor(JpaOperations jpaOperations) If custom behavior is required a custom implementation ofJpaOperationscan be passed in. -
Method Summary
Modifier and TypeMethodDescriptionvoidVerify 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.voidsetBeanFactory(BeanFactory beanFactory) voidsetClearOnFlush(boolean clearOnFlush) If set totruetheEntityManager.clear()will be called, and only if theEntityManager.flush()was called after performing persistence operations.voidsetDeleteAfterPoll(boolean deleteAfterPoll) If set to 'true', the retrieved objects are deleted from the database upon being polled.voidsetDeleteInBatch(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.voidsetEntityClass(Class<?> entityClass) Set the class type which is being used for retrieving entities from the database.voidsetExpectSingleResult(boolean expectSingleResult) This parameter indicates that only one result object shall be returned as a result from the executed JPA operation.voidsetFirstResultExpression(Expression firstResultExpression) Set the expression that will be evaluated to get the first result in the query executed.voidsetFlush(boolean flush) If set totruetheEntityManager.flush()will be called after persistence operation.voidsetFlushSize(int flushSize) If the provided value is greater than0, thenEntityManager.flush()will be called after persistence operations as well as within batch operations.voidsetIdExpression(Expression idExpression) Set the expression that will be evaluated to get theprimaryKeyforEntityManager.find(Class, Object).voidsetIntegrationEvaluationContext(EvaluationContext evaluationContext) voidsetJpaParameters(List<JpaParameter> jpaParameters) voidsetJpaQuery(String jpaQuery) voidsetMaxNumberOfResults(int maxNumberOfResults) Set the max number of results to retrieve from the database.voidsetMaxResultsExpression(Expression maxResultsExpression) Set the expression for maximum number of results expression.voidsetNamedQuery(String namedQuery) A named query can either refer to a named JPQL based query or a native SQL query.voidsetNativeQuery(String nativeQuery) You can also use native Sql queries to poll data from the database.voidsetParameterSource(ParameterSource parameterSource) Specify theParameterSourcethat would be used to provide additional parameters.voidsetParameterSourceFactory(ParameterSourceFactory parameterSourceFactory) voidsetPersistMode(PersistMode persistMode) voidsetUsePayloadAsParameterSource(Boolean usePayloadAsParameterSource)
-
Constructor Details
-
JpaExecutor
Constructor taking anEntityManagerFactoryfrom which theEntityManagercan be obtained.- Parameters:
entityManagerFactory- Must not be null.
-
JpaExecutor
Constructor taking anEntityManagerdirectly.- Parameters:
entityManager- Must not be null.
-
JpaExecutor
If custom behavior is required a custom implementation ofJpaOperationscan be passed in. The implementations themselves typically provide access to theEntityManager. See alsoDefaultJpaOperationsandAbstractJpaOperations.- 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 totruetheEntityManager.flush()will be called after persistence operation. Has the same effect, if theflushSizeis 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 ofIterableand this property is greater than0, then the entity will be flushed as if theflushattribute was set totrue.- Parameters:
flushSize- defaults to '0'.
-
setClearOnFlush
public void setClearOnFlush(boolean clearOnFlush) If set totruetheEntityManager.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 theParameterSourcethat 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 totrueand 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
expectSingleResultistrue, then aMessagingExceptionis 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 theprimaryKeyforEntityManager.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:
setBeanFactoryin interfaceBeanFactoryAware- Throws:
BeansException
-
afterPropertiesSet
public void afterPropertiesSet()Verify and sets the parameters. E.g. initializes the to be usedParameterSourceFactory.- Specified by:
afterPropertiesSetin 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 thenparameterSourceis 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)
-