Spring Integration

org.springframework.integration.jpa.core
Class JpaExecutor

java.lang.Object
  extended by org.springframework.integration.jpa.core.JpaExecutor
All Implemented Interfaces:
org.springframework.beans.factory.InitializingBean

public class JpaExecutor
extends java.lang.Object
implements org.springframework.beans.factory.InitializingBean

Executes Jpa Operations that produce payload objects from the result of the provided:

When objects are being retrieved, it also possibly to: If neither entityClass nor any other query is specified then the entity-class is "guessed" from the Message payload.

Since:
2.2
Author:
Gunnar Hillert, Amol Nayak

Constructor Summary
JpaExecutor(javax.persistence.EntityManager entityManager)
          Constructor taking an EntityManager directly.
JpaExecutor(javax.persistence.EntityManagerFactory entityManagerFactory)
          Constructor taking an EntityManagerFactory from which the EntityManager can be obtained.
JpaExecutor(JpaOperations jpaOperations)
          If custom behavior is required a custom implementation of JpaOperations can be passed in.
 
Method Summary
 void afterPropertiesSet()
          Verifies and sets the parameters.
protected  java.util.List<?> doPoll(ParameterSource jpaQLParameterSource)
           
 java.lang.Object executeOutboundJpaOperation(Message<?> message)
          Executes the actual Jpa Operation.
 java.lang.Object poll()
          Execute the JPA operation.
 java.lang.Object poll(Message<?> requestMessage)
          Execute a (typically retrieving) JPA operation.
 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 to false, which means that deletion occurs on a per object basis if a collection of entities is being deleted.
 void setEntityClass(java.lang.Class<?> entityClass)
          Sets 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 setJpaParameters(java.util.List<JpaParameter> jpaParameters)
           
 void setJpaQuery(java.lang.String jpaQuery)
           
 void setMaxNumberOfResults(int maxNumberOfResults)
          Set the max number of results to retrieve from the database.
 void setNamedQuery(java.lang.String namedQuery)
          A named query can either refer to a named JPQL based query or a native SQL query.
 void setNativeQuery(java.lang.String nativeQuery)
          You can also use native Sql queries to poll data from the database.
 void setParameterSource(ParameterSource parameterSource)
          Specifies the ParameterSource that would be used to provide additional parameters.
 void setParameterSourceFactory(ParameterSourceFactory parameterSourceFactory)
           
 void setPersistMode(PersistMode persistMode)
           
 void setUsePayloadAsParameterSource(java.lang.Boolean usePayloadAsParameterSource)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JpaExecutor

public JpaExecutor(javax.persistence.EntityManagerFactory entityManagerFactory)
Constructor taking an EntityManagerFactory from which the EntityManager can be obtained.

Parameters:
entityManagerFactory - Must not be null.

JpaExecutor

public JpaExecutor(javax.persistence.EntityManager entityManager)
Constructor taking an EntityManager directly.

Parameters:
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.

Parameters:
jpaOperations - Must not be null.
Method Detail

afterPropertiesSet

public void afterPropertiesSet()
Verifies and sets the parameters. E.g. initializes the to be used ParameterSourceFactory.

Specified by:
afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean

executeOutboundJpaOperation

public java.lang.Object executeOutboundJpaOperation(Message<?> message)
Executes the actual Jpa Operation. Call this method, if you need access to process return values. This 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 -
Returns:
Either the number of affected entities when using a JPAQL query. When using a merge/persist the updated/inserted itself is returned.

poll

public java.lang.Object poll(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.

Parameters:
requestMessage - May be null.
Returns:
The payload object, which may be null.

poll

public java.lang.Object poll()
Execute the JPA operation. Delegates to poll(Message).


doPoll

protected java.util.List<?> doPoll(ParameterSource jpaQLParameterSource)

setEntityClass

public void setEntityClass(java.lang.Class<?> entityClass)
Sets the class type which is being used for retrieving entities from the database.

Parameters:
entityClass - Must not be null.

setJpaQuery

public void setJpaQuery(java.lang.String jpaQuery)
Parameters:
jpaQuery - The provided JPA query must neither be null nor empty.

setNativeQuery

public void setNativeQuery(java.lang.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.

Parameters:
nativeQuery - The provided SQL query must neither be null nor empty.

setNamedQuery

public void setNamedQuery(java.lang.String namedQuery)
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

public void setPersistMode(PersistMode persistMode)

setJpaParameters

public void setJpaParameters(java.util.List<JpaParameter> jpaParameters)

setUsePayloadAsParameterSource

public void setUsePayloadAsParameterSource(java.lang.Boolean usePayloadAsParameterSource)

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 regards 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

public void setParameterSourceFactory(ParameterSourceFactory parameterSourceFactory)
Parameters:
parameterSourceFactory - Must not be null

setParameterSource

public void setParameterSource(ParameterSource parameterSource)
Specifies the ParameterSource 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 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.


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:
Query.setMaxResults(int)

Spring Integration