public class JPAQueryTransformer extends EntityManagerLookupAware implements QueryTransformer, Serializable
List<Object[]>
PageInfo
as the input itself or last param in the parameter Array; if pagination is required.
Map
to this Transformer
. When a Map is passed then the Query params should be :paramNameAsKeyInMap and any value thats associated as a PageInfo
will be used to set the PageInfo
to the Query.
fieldMapping
is provided then it can take parameter names defined and map it to the fields in the mapping.
<bean class="com.neurosys.dao.jpa.JPAQueryTransformer">
<property name="query"><value><![CDATA[
SELECT orders from Order orders WHERE purchaseDate BETWEEN ?1 AND ?2 ORDER BY id
]]></value></property>
<property name="queryIsNative" value="false" />
</bean>
Example of a JPA query with List or Array as a single param:
<bean class="com.neurosys.dao.jpa.JPAQueryTransformer">
<property name="query" value="SELECT cf FROM ContentFacet cf WHERE id in (?1)" />
<property name="paramAsSingleInput" value="true" />
</bean>
Example of a JPA NATIVE query:
<bean id="deleteImageAssociation" class="com.neurosys.dao.jpa.JPAExecuteTransformer">
<property name="query"><value><![CDATA[
DELETE pa FROM product_attributes pa RIGHT OUTER JOIN product p ON pa.product_id=p.id WHERE pa.name in ('mainImgSrc','zoomImgSrc') AND p.productCode is NOT NULL
]]></value></property>
<property name="queryIsNative" value="true" />
</bean>
To convert native query result into entities can look @ QueryResultArrayToEntityTransformer and ObjectArrayToEntityTransformer.optimize()
,
overrideFinalQuery(String, Query)
,
Serialized FormModifier and Type | Field and Description |
---|---|
protected boolean |
expectSingleResult
If true it will attempt to return only 1 Object or null if no match to query
|
protected Map<String,?> |
fieldMapping
Map from query param name to field name or Transformer that will act on input to get param value.
|
protected String |
query |
protected boolean |
queryIsNative
If the Query is to be fired in JPA or native DB SQL language
|
protected QueryOptimizer |
queryOptimizer
An optional configurable
QueryOptimizer |
protected String |
queryOptimizerEntityAlias
Since the query will not change, within the Query what is the Alias of the entity/table
|
Constructor and Description |
---|
JPAQueryTransformer() |
JPAQueryTransformer(String query) |
Modifier and Type | Method and Description |
---|---|
String |
derivedQuery()
Opportunity to override core
query string. |
Map<String,?> |
getFieldMapping()
Map from query param name to field name or Transformer that will act on input to get param value.
|
PageInfo |
getPageInfo()
Optional.
|
String |
getQuery()
Query to process.
|
QueryOptimizer |
getQueryOptimizer()
QueryOptimizer to optimize() the set query
If isQueryIsNative() is true then the Optimizer should ensure it can work on native queries else on entities. |
String |
getQueryOptimizerEntityAlias()
Since the query will not change, within the Query what is the Alias of the entity/table
|
boolean |
isExpectSingleResult()
If true it will attempt to return only 1 Object or null if no match to query
|
boolean |
isParamAsSingleInput()
If the input is a List of Collection and we wish to process it as a single parameter
make this true OR wrap it in an Object Array.
|
boolean |
isQueryIsNative()
If the Query is to be fired in JPA or native DB SQL language
|
protected void |
optimize()
If
queryOptimizer is set then
optimize the #query. |
protected void |
overrideFinalQuery(String queryString,
javax.persistence.Query q)
Opportunity to override any thing in the final generated Query
Query
in the transform(Object) process. |
void |
setExpectSingleResult(boolean expectSingleResult)
If true it will attempt to return only 1 Object or null if no match to query
|
void |
setFieldMapping(Map<String,?> fieldMapping)
Map from query param name to field name or Transformer that will act on input to get param value.
|
void |
setPageInfo(PageInfo pageInfo)
Optional.
|
void |
setParamAsSingleInput(boolean paramAsSingleInput)
If the input is a List of Collection and we wish to process it as a single parameter
make this true OR wrap it in an Object Array.
|
void |
setQuery(String query)
Query to process.
|
void |
setQueryIsNative(boolean queryIsNative)
If the Query is to be fired in JPA or native DB SQL language
|
void |
setQueryOptimizer(QueryOptimizer queryOptimizer)
QueryOptimizer to optimize() the set query
If isQueryIsNative() is true then the Optimizer should ensure it can work on native queries else on entities. |
void |
setQueryOptimizerEntityAlias(String queryOptimizerEntityAlias)
Since the query will not change, within the Query what is the Alias of the entity/table
|
Object |
transform(Object input) |
getEntityManager, getEntityManagerProvider, setEntityManager, setEntityManagerProvider, updateContext
protected String query
protected boolean queryIsNative
protected boolean expectSingleResult
protected Map<String,?> fieldMapping
protected QueryOptimizer queryOptimizer
QueryOptimizer
protected String queryOptimizerEntityAlias
public JPAQueryTransformer()
public JPAQueryTransformer(String query)
@PostConstruct protected void optimize()
queryOptimizer
is set then
optimize the #query. PostConstruct
, this is automatically done, in an API call one has to explicitly call this.@Transactional(readOnly=true) public Object transform(Object input)
transform
in interface org.apache.commons.collections.Transformer
protected void overrideFinalQuery(String queryString, javax.persistence.Query q)
Query
in the transform(Object)
process.queryString
- as String (immutable, provided for reference purpose only)q
- as Queryto manipulate original query string dynamically.
public String derivedQuery()
query
string.overrideFinalQuery(String, Query)
public String getQuery()
QueryTransformer
getQuery
in interface QueryTransformer
public void setQuery(String query)
QueryTransformer
setQuery
in interface QueryTransformer
public boolean isQueryIsNative()
public void setQueryIsNative(boolean queryIsNative)
public boolean isExpectSingleResult()
isExpectSingleResult
in interface QueryTransformer
public void setExpectSingleResult(boolean expectSingleResult)
setExpectSingleResult
in interface QueryTransformer
public Map<String,?> getFieldMapping()
getFieldMapping
in interface QueryTransformer
public void setFieldMapping(Map<String,?> fieldMapping)
setFieldMapping
in interface QueryTransformer
public QueryOptimizer getQueryOptimizer()
QueryOptimizer
to optimize()
the set query
isQueryIsNative()
is true then the Optimizer should ensure it can work on native queries else on entities.public void setQueryOptimizer(QueryOptimizer queryOptimizer)
QueryOptimizer
to optimize()
the set query
isQueryIsNative()
is true then the Optimizer should ensure it can work on native queries else on entities.public String getQueryOptimizerEntityAlias()
public void setQueryOptimizerEntityAlias(String queryOptimizerEntityAlias)
public boolean isParamAsSingleInput()
isParamAsSingleInput
in interface QueryTransformer
public void setParamAsSingleInput(boolean paramAsSingleInput)
setParamAsSingleInput
in interface QueryTransformer
public PageInfo getPageInfo()
PageInfo
details passedCopyright © 2018. All rights reserved.