FileDocCategorySizeDatePackage
QueryWrapper.javaAPI DocGlassfish v2 API25252Fri May 04 22:35:58 BST 2007com.sun.enterprise.util

QueryWrapper

public class QueryWrapper extends Object implements Query
Wrapper class for javax.persistence.Query objects returned from non-transactional access of a container-managed transactional EntityManager. Proxying the Query object prevents the EntityManagerWrapper from having to keep a physical EntityManager open when returning Query objects for non-transactional access. This results in a cleaner implementation of the non-transactional EntityManager behavior and minimizes the amount of time non-transactional EntityManager objects are left open. It is likely that physical EntityManager objects will have heavy-weight resources such as DB connections open even after clear() is called. This is one of the main reasons to minimize the number of open non-transactional EntityManager objects held internally within injected/looked-up container-managed EntityManagers. The EntityManager and Query delegate objects are provided at QueryWrapper creation time. These objects must exist in order for the EntityManagerWrapper to provide the correct exception behavior to the application when a Query is requested. Likewise, the actual delegates must be available to handle the majority of the Query API operations such as performing validation on the various setter parameters. The Query/EntityManager delegates are closed/discarded after each call to getSingleResult/getResultList. A new Query/EntityManager delegate pair is then created lazily the next time the Query delegate is needed. The QueryWrapper maintains a list of all setter operations invoked by the application. These are re-applied in the same order whenever a new Query delegate is created to ensure that the state of the Query delegate object matches what it would have been if there wasn't any QueryWrapper.

Fields Summary
private Query
queryDelegate
private EntityManager
entityManagerDelegate
private EntityManagerFactory
entityMgrFactory
private Map
entityMgrProperties
private QueryType
queryType
private String
queryString
private Class
queryResultClass
private String
queryResultSetMapping
private List
setterInvocations
Constructors Summary
private QueryWrapper(EntityManagerFactory emf, Map emProperties, EntityManager emDelegate, Query qDelegate, QueryType type, String query, Class resultClass, String resultSetMapping)

        entityMgrFactory = emf;
        entityMgrProperties = emProperties;

        entityManagerDelegate = emDelegate;
        queryDelegate = qDelegate;

        queryType = type;
        queryString = query;
        queryResultClass = resultClass;
        queryResultSetMapping = resultSetMapping;

        setterInvocations = new LinkedList<SetterData>();
    
Methods Summary
private voidclearDelegates()


        queryDelegate = null;

        if( entityManagerDelegate != null ) {
            entityManagerDelegate.close();
            entityManagerDelegate = null;
        }
        
    
public static javax.persistence.QuerycreateNamedQueryWrapper(javax.persistence.EntityManagerFactory emf, java.util.Map emProperties, javax.persistence.EntityManager emDelegate, javax.persistence.Query queryDelegate, java.lang.String name)

        return new QueryWrapper(emf, emProperties, emDelegate,
                                queryDelegate, QueryType.NAMED,
                                name, null, null);
    
public static javax.persistence.QuerycreateNativeQueryWrapper(javax.persistence.EntityManagerFactory emf, java.util.Map emProperties, javax.persistence.EntityManager emDelegate, javax.persistence.Query queryDelegate, java.lang.String sqlString)


        return new QueryWrapper(emf, emProperties, emDelegate,
                                queryDelegate, QueryType.NATIVE,
                                sqlString, null, null);
        
    
public static javax.persistence.QuerycreateNativeQueryWrapper(javax.persistence.EntityManagerFactory emf, java.util.Map emProperties, javax.persistence.EntityManager emDelegate, javax.persistence.Query queryDelegate, java.lang.String sqlString, java.lang.Class resultClass)


        return new QueryWrapper(emf, emProperties, emDelegate,
                                queryDelegate, QueryType.NATIVE,
                                sqlString, resultClass, null);
        
    
public static javax.persistence.QuerycreateNativeQueryWrapper(javax.persistence.EntityManagerFactory emf, java.util.Map emProperties, javax.persistence.EntityManager emDelegate, javax.persistence.Query queryDelegate, java.lang.String sqlString, java.lang.String resultSetMapping)


        return new QueryWrapper(emf, emProperties, emDelegate,
                                queryDelegate, QueryType.NATIVE,
                                sqlString, null, resultSetMapping);
        
    
public static javax.persistence.QuerycreateQueryWrapper(javax.persistence.EntityManagerFactory emf, java.util.Map emProperties, javax.persistence.EntityManager emDelegate, javax.persistence.Query queryDelegate, java.lang.String ejbqlString)


        return new QueryWrapper(emf, emProperties, emDelegate,
                                queryDelegate, QueryType.EJBQL,
                                ejbqlString, null, null);
    
public intexecuteUpdate()

        Agent callFlowAgent = Switch.getSwitch().getCallFlowAgent();
        if(callFlowAgent.isEnabled()) {
            callFlowAgent.entityManagerQueryStart(EntityManagerQueryMethod.EXECUTE_UPDATE);
            callFlowAgent.entityManagerQueryEnd();
        }
        throw new TransactionRequiredException("executeUpdate is not supported for a Query object obtained through non-transactional access of a container-managed transactional EntityManager");
    
private javax.persistence.QuerygetQueryDelegate()


        if( queryDelegate == null ) {

            entityManagerDelegate = 
                entityMgrFactory.createEntityManager(entityMgrProperties);

            switch(queryType) {

              case EJBQL :

                  queryDelegate = 
                      entityManagerDelegate.createQuery(queryString);
                  break;
                  
              case NAMED :

                  queryDelegate = 
                      entityManagerDelegate.createNamedQuery(queryString);
                  break;

              case NATIVE :

                  if( queryResultClass != null ) {

                      queryDelegate = entityManagerDelegate.createNativeQuery
                          (queryString, queryResultClass);

                  } else if( queryResultSetMapping != null ) {

                      queryDelegate = entityManagerDelegate.createNativeQuery
                          (queryString, queryResultSetMapping);

                  } else {

                      queryDelegate = entityManagerDelegate.createNativeQuery
                          (queryString);
                  }

                  break;
            }

            // Now recreate the sequence of valid setter invocations applied 
            // to this query.
            for(SetterData setterData : setterInvocations) {
                setterData.apply(queryDelegate);
            }

        }


        return queryDelegate;

    
public java.util.ListgetResultList()

        Agent callFlowAgent = Switch.getSwitch().getCallFlowAgent();
        try {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerQueryStart(EntityManagerQueryMethod.GET_RESULT_LIST);
            }
            Query delegate = getQueryDelegate();
            return delegate.getResultList();
        } finally {
            clearDelegates();
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerQueryEnd();
            }
        }
    
public java.lang.ObjectgetSingleResult()

        Agent callFlowAgent = Switch.getSwitch().getCallFlowAgent();
        try {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerQueryStart(EntityManagerQueryMethod.GET_SINGLE_RESULT);
            }
            Query delegate = getQueryDelegate();
            return delegate.getSingleResult();

        } finally {
            clearDelegates();
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerQueryEnd();
            }
        }
    
public javax.persistence.QuerysetFirstResult(int startPosition)

        Agent callFlowAgent = Switch.getSwitch().getCallFlowAgent();
        
        try {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerQueryStart(EntityManagerQueryMethod.SET_FIRST_RESULT);
            }
            if( startPosition < 0 ) {
                throw new IllegalArgumentException
                        ("startPosition cannot be negative");
            }
            
            Query delegate = getQueryDelegate();
            delegate.setFirstResult(startPosition);
            
            SetterData setterData = SetterData.createFirstResult(startPosition);
            setterInvocations.add(setterData);
        } finally {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerQueryEnd();
            }
        }
        
        return this;
    
public javax.persistence.QuerysetFlushMode(javax.persistence.FlushModeType flushMode)

        Agent callFlowAgent = Switch.getSwitch().getCallFlowAgent();
        
        try {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerQueryStart(EntityManagerQueryMethod.SET_FLUSH_MODE);
            }
            Query delegate = getQueryDelegate();
            delegate.setFlushMode(flushMode);
            
            SetterData setterData = SetterData.createFlushMode(flushMode);
            setterInvocations.add(setterData);
        } finally {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerQueryEnd();
            }
        }
        
        return this;
    
public javax.persistence.QuerysetHint(java.lang.String hintName, java.lang.Object value)

        Agent callFlowAgent = Switch.getSwitch().getCallFlowAgent();
        
        try {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerQueryStart(EntityManagerQueryMethod.SET_HINT);
            }
            Query delegate = getQueryDelegate();
            delegate.setHint(hintName, value);
            
            SetterData setterData = SetterData.createHint(hintName, value);
            setterInvocations.add(setterData);
        } finally {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerQueryEnd();
            }
        }
        
        return this;
    
public javax.persistence.QuerysetMaxResults(int maxResults)

        Agent callFlowAgent = Switch.getSwitch().getCallFlowAgent();
        
        try {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerQueryStart(EntityManagerQueryMethod.SET_MAX_RESULTS);
            }
            if( maxResults < 0 ) {
                throw new IllegalArgumentException("maxResult cannot be negative");
            }
            
            Query delegate = getQueryDelegate();
            delegate.setMaxResults(maxResults);
            
            SetterData setterData = SetterData.createMaxResults(maxResults);
            setterInvocations.add(setterData);
        } finally {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerQueryEnd();
            }
        }
        
        return this;
    
public javax.persistence.QuerysetParameter(java.lang.String name, java.lang.Object value)

        Agent callFlowAgent = Switch.getSwitch().getCallFlowAgent();
        
        try {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerQueryStart(EntityManagerQueryMethod.SET_PARAMETER_STRING_OBJECT);
            }
            Query delegate = getQueryDelegate();
            delegate.setParameter(name, value);
            
            SetterData setterData = SetterData.createParameter(name, value);
            setterInvocations.add(setterData);
        } finally {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerQueryEnd();
            }
        }
        
        return this;
    
public javax.persistence.QuerysetParameter(java.lang.String name, java.util.Date value, javax.persistence.TemporalType temporalType)

        Agent callFlowAgent = Switch.getSwitch().getCallFlowAgent();
        
        try {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerQueryStart(EntityManagerQueryMethod.SET_PARAMETER_STRING_DATE_TEMPORAL_TYPE);
            }
            Query delegate = getQueryDelegate();
            delegate.setParameter(name, value, temporalType);
            
            SetterData setterData = SetterData.createParameter(name, value,
                    temporalType);
            setterInvocations.add(setterData);
        } finally {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerQueryEnd();
            }
        }
        return this;
    
public javax.persistence.QuerysetParameter(java.lang.String name, java.util.Calendar value, javax.persistence.TemporalType temporalType)

        Agent callFlowAgent = Switch.getSwitch().getCallFlowAgent();
        try {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerQueryStart(EntityManagerQueryMethod.SET_PARAMETER_STRING_CALENDAR_TEMPORAL_TYPE);
            }
            Query delegate = getQueryDelegate();
            delegate.setParameter(name, value, temporalType);
            
            SetterData setterData = SetterData.createParameter(name, value,
                    temporalType);
            setterInvocations.add(setterData);
        } finally {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerQueryEnd();
            }
        }
        return this;
    
public javax.persistence.QuerysetParameter(int position, java.lang.Object value)

        Agent callFlowAgent = Switch.getSwitch().getCallFlowAgent();
        
        try {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerQueryStart(EntityManagerQueryMethod.SET_PARAMETER_INT_OBJECT);
            }
        Query delegate = getQueryDelegate();
        delegate.setParameter(position, value);

        SetterData setterData = SetterData.createParameter(position, value);
        setterInvocations.add(setterData);
        } finally {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerQueryEnd();
            }
        }

        return this;
    
public javax.persistence.QuerysetParameter(int position, java.util.Date value, javax.persistence.TemporalType temporalType)

        Agent callFlowAgent = Switch.getSwitch().getCallFlowAgent();
        
        try {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerQueryStart(EntityManagerQueryMethod.SET_PARAMETER_INT_DATE_TEMPORAL_TYPE);
            }
            Query delegate = getQueryDelegate();
            delegate.setParameter(position, value, temporalType);
            
            SetterData setterData = SetterData.createParameter(position, value,
                    temporalType);
            setterInvocations.add(setterData);
        } finally {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerQueryEnd();
            }
        }
        
        return this;
    
public javax.persistence.QuerysetParameter(int position, java.util.Calendar value, javax.persistence.TemporalType temporalType)

        Agent callFlowAgent = Switch.getSwitch().getCallFlowAgent();
        
        try {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerQueryStart(EntityManagerQueryMethod.SET_PARAMETER_INT_CALENDAR_TEMPORAL_TYPE);
            }
            Query delegate = getQueryDelegate();
            delegate.setParameter(position, value, temporalType);
            
            SetterData setterData = SetterData.createParameter(position, value,
                    temporalType);
            setterInvocations.add(setterData);
        } finally {
            if(callFlowAgent.isEnabled()) {
                callFlowAgent.entityManagerQueryEnd();
            }
        }
        
        return this;