Search in sources :

Example 86 with DatabaseQuery

use of org.eclipse.persistence.queries.DatabaseQuery in project eclipselink by eclipse-ee4j.

the class EJBQueryImpl method getResultCollection.

/**
 * Non-standard method to return results of a ReadQuery that has a
 * containerPolicy that returns objects as a collection rather than a List
 *
 * @return Collection of results
 */
@Override
public Collection getResultCollection() {
    // bug51411440: need to throw IllegalStateException if query
    // executed on closed em
    this.entityManager.verifyOpenWithSetRollbackOnly();
    setAsSQLReadQuery();
    propagateResultProperties();
    // bug:4297903, check container policy class and throw exception if its
    // not the right type
    DatabaseQuery query = getDatabaseQueryInternal();
    try {
        if (query.isReadAllQuery()) {
            Class<?> containerClass = ((ReadAllQuery) getDatabaseQueryInternal()).getContainerPolicy().getContainerClass();
            if (!Helper.classImplementsInterface(containerClass, ClassConstants.Collection_Class)) {
                throw QueryException.invalidContainerClass(containerClass, ClassConstants.Collection_Class);
            }
        } else if (query.isReadObjectQuery()) {
            List<Object> resultList = new ArrayList<>();
            Object result = executeReadQuery();
            if (result != null) {
                resultList.add(executeReadQuery());
            }
            return resultList;
        } else if (!query.isReadQuery()) {
            throw new IllegalStateException(ExceptionLocalization.buildMessage("incorrect_query_for_get_result_collection"));
        }
        return (Collection) executeReadQuery();
    } catch (LockTimeoutException exception) {
        throw exception;
    } catch (PersistenceException exception) {
        setRollbackOnly();
        throw exception;
    } catch (IllegalStateException exception) {
        setRollbackOnly();
        throw exception;
    } catch (RuntimeException exception) {
        setRollbackOnly();
        throw new PersistenceException(exception);
    }
}
Also used : DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) PersistenceException(jakarta.persistence.PersistenceException) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) LockTimeoutException(jakarta.persistence.LockTimeoutException)

Aggregations

DatabaseQuery (org.eclipse.persistence.queries.DatabaseQuery)86 ArrayList (java.util.ArrayList)18 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)15 List (java.util.List)14 Vector (java.util.Vector)12 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)12 ObjectLevelReadQuery (org.eclipse.persistence.queries.ObjectLevelReadQuery)12 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)8 EntityManager (jakarta.persistence.EntityManager)6 HashMap (java.util.HashMap)6 QName (javax.xml.namespace.QName)6 EJBQueryImpl (org.eclipse.persistence.internal.jpa.EJBQueryImpl)6 PersistenceContext (org.eclipse.persistence.jpa.rs.PersistenceContext)6 DataReadQuery (org.eclipse.persistence.queries.DataReadQuery)6 Session (org.eclipse.persistence.sessions.Session)6 Test (org.junit.Test)6 NonSynchronizedVector (org.eclipse.persistence.internal.helper.NonSynchronizedVector)5 ReadQuery (org.eclipse.persistence.queries.ReadQuery)5 ReportQuery (org.eclipse.persistence.queries.ReportQuery)5 PersistenceException (jakarta.persistence.PersistenceException)4