Search in sources :

Example 1 with SQLResultSetMapping

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

the class JPAQuery method processStoredProcedureQuery.

/**
 * INTERNAL:
 * Convert the StoredProc call into a DatabaseQuery.
 */
public DatabaseQuery processStoredProcedureQuery(Session session) {
    DatabaseQuery query = null;
    ClassLoader loader = session.getDatasourcePlatform().getConversionManager().getLoader();
    if (resultClassNames != null) {
        List<SQLResultSetMapping> resultSetMappings = new ArrayList<>();
        for (String resultClass : resultClassNames) {
            SQLResultSetMapping mapping = new SQLResultSetMapping(resultClass);
            EntityResult entityResult = new EntityResult(resultClass);
            mapping.addResult(entityResult);
            resultSetMappings.add(mapping);
        }
        query = StoredProcedureQueryImpl.buildResultSetMappingQuery(resultSetMappings, call, hints, loader, (AbstractSession) session);
    } else if (resultSetMappingNames != null) {
        query = StoredProcedureQueryImpl.buildResultSetMappingNameQuery(resultSetMappingNames, call, hints, loader, (AbstractSession) session);
    } else if (resultClassName != null) {
        Class<?> clazz = session.getDatasourcePlatform().getConversionManager().convertClassNameToClass(resultClassName);
        query = StoredProcedureQueryImpl.buildStoredProcedureQuery(clazz, call, hints, loader, (AbstractSession) session);
    } else {
        // Neither a resultClass or resultSetMapping is specified so place in a temp query on the session.
        if (call.isStoredFunctionCall() || call.isStoredPLSQLProcedureCall()) {
            // If it is a function (plsql or not) or plsql procedure use the data read query.
            query = StoredProcedureQueryImpl.buildStoredProcedureQuery(call, hints, loader, (AbstractSession) session);
        } else {
            // Otherwise use a result set mapping query for stored procedure calls so users can use the execute
            // method on it (JPA 2.1 API). Will return the same result, that is, Object[] in this case.
            query = StoredProcedureQueryImpl.buildResultSetMappingQuery(new ArrayList<>(), call, hints, loader, (AbstractSession) session);
        }
    }
    query.setName(getName());
    return query;
}
Also used : DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) SQLResultSetMapping(org.eclipse.persistence.queries.SQLResultSetMapping) ArrayList(java.util.ArrayList) EntityResult(org.eclipse.persistence.queries.EntityResult) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 2 with SQLResultSetMapping

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

the class Project method convertClassNamesToClasses.

/**
 * INTERNAL:
 * Convert all the class-name-based settings in this project to actual class-based settings.
 * This will also reset any class references to the version of the class from the class loader.
 */
@Override
public void convertClassNamesToClasses(ClassLoader classLoader) {
    Iterator<ClassDescriptor> ordered = orderedDescriptors.iterator();
    while (ordered.hasNext()) {
        ClassDescriptor descriptor = ordered.next();
        descriptor.convertClassNamesToClasses(classLoader);
    }
    for (AttributeGroup group : this.getAttributeGroups().values()) {
        group.convertClassNamesToClasses(classLoader);
    }
    // Clear old descriptors to allow rehash on new classes.
    this.descriptors = new HashMap<>();
    // convert class names to classes for each SQLResultSetMapping
    if (this.sqlResultSetMappings != null) {
        for (SQLResultSetMapping mapping : this.sqlResultSetMappings.values()) {
            mapping.convertClassNamesToClasses(classLoader);
        }
    }
    if (this.partitioningPolicies != null) {
        for (PartitioningPolicy policy : this.partitioningPolicies.values()) {
            policy.convertClassNamesToClasses(classLoader);
        }
    }
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) SQLResultSetMapping(org.eclipse.persistence.queries.SQLResultSetMapping) AttributeGroup(org.eclipse.persistence.queries.AttributeGroup) PartitioningPolicy(org.eclipse.persistence.descriptors.partitioning.PartitioningPolicy)

Example 3 with SQLResultSetMapping

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

the class StoredProcedureQueryTestSuite method testQueryWithMultipleResultsFromCode.

/**
 * Test multiple result sets by setting the SQL results set mapping from code.
 */
public void testQueryWithMultipleResultsFromCode() throws Exception {
    if (supportsStoredProcedures() && getPlatform().isMySQL()) {
        // SQL result set mapping for employee.
        SQLResultSetMapping employeeResultSetMapping = new SQLResultSetMapping("EmployeeResultSetMapping");
        employeeResultSetMapping.addResult(new EntityResult(Employee.class));
        // SQL result set mapping for address.
        SQLResultSetMapping addressResultSetMapping = new SQLResultSetMapping("AddressResultSetMapping");
        addressResultSetMapping.addResult(new EntityResult(Address.class));
        // SQL result set mapping for project (using inheritance and more complex result)
        SQLResultSetMapping projectResultSetMapping = new SQLResultSetMapping("ProjectResultSetMapping");
        EntityResult projectEntityResult = new EntityResult(Project.class);
        projectResultSetMapping.addResult(projectEntityResult);
        projectEntityResult = new EntityResult(SmallProject.class);
        projectEntityResult.addFieldResult(new FieldResult("id", "SMALL_ID"));
        projectEntityResult.addFieldResult(new FieldResult("name", "SMALL_NAME"));
        projectEntityResult.addFieldResult(new FieldResult("description", "SMALL_DESCRIPTION"));
        projectEntityResult.addFieldResult(new FieldResult("teamLeader", "SMALL_TEAMLEAD"));
        projectEntityResult.addFieldResult(new FieldResult("version", "SMALL_VERSION"));
        projectEntityResult.setDiscriminatorColumn("SMALL_DESCRIM");
        projectResultSetMapping.addResult(projectEntityResult);
        projectResultSetMapping.addResult(new ColumnResult("BUDGET_SUM"));
        // SQL result set mapping for employee using constructor results.
        SQLResultSetMapping employeeConstrustorResultSetMapping = new SQLResultSetMapping("EmployeeConstructorResultSetMapping");
        ConstructorResult constructorResult = new ConstructorResult(EmployeeDetails.class);
        ColumnResult columnResult = new ColumnResult("EMP_ID");
        columnResult.getColumn().setType(Integer.class);
        constructorResult.addColumnResult(columnResult);
        columnResult = new ColumnResult("F_NAME");
        columnResult.getColumn().setType(String.class);
        constructorResult.addColumnResult(columnResult);
        columnResult = new ColumnResult("L_NAME");
        columnResult.getColumn().setType(String.class);
        constructorResult.addColumnResult(columnResult);
        columnResult = new ColumnResult("R_COUNT");
        columnResult.getColumn().setType(Integer.class);
        constructorResult.addColumnResult(columnResult);
        employeeConstrustorResultSetMapping.addResult(constructorResult);
        StoredProcedureCall call = new StoredProcedureCall();
        call.setProcedureName("Read_Multiple_Result_Sets");
        call.setHasMultipleResultSets(true);
        call.setReturnMultipleResultSetCollections(true);
        ResultSetMappingQuery query = new ResultSetMappingQuery(call);
        query.addSQLResultSetMapping(employeeResultSetMapping);
        query.addSQLResultSetMapping(addressResultSetMapping);
        query.addSQLResultSetMapping(projectResultSetMapping);
        query.addSQLResultSetMapping(employeeConstrustorResultSetMapping);
        List allResults = (List) getPersistenceUnitServerSession().executeQuery(query);
        assertNotNull("No results returned", allResults);
        assertTrue("Incorrect number of results returned", allResults.size() == 4);
        // Verify first result set mapping --> Employee
        List results0 = (List) allResults.get(0);
        assertNotNull("No Employee results returned", results0);
        assertTrue("Empty Employee results returned", results0.size() > 0);
        // Verify second result set mapping --> Address
        List results1 = (List) allResults.get(1);
        assertNotNull("No Address results returned", results1);
        assertTrue("Empty Address results returned", results1.size() > 0);
        // Verify third result set mapping --> Project
        List results2 = (List) allResults.get(2);
        assertNotNull("No Project results returned", results2);
        assertTrue("Empty Project results returned", results2.size() > 0);
        for (Object result2 : results2) {
            Object[] result2Element = (Object[]) result2;
            assertTrue("Failed to Return 3 items", (result2Element.length == 3));
            // Using Number as Different db/drivers  can return different types
            // e.g. Oracle with ijdbc14.jar returns BigDecimal where as Derby
            // with derbyclient.jar returns Double. NOTE: the order of checking
            // here is valid and as defined by the spec.
            assertTrue("Failed to return LargeProject", (result2Element[0] instanceof LargeProject));
            assertTrue("Failed To Return SmallProject", (result2Element[1] instanceof SmallProject));
            assertTrue("Failed to return column", (result2Element[2] instanceof Number));
            assertFalse("Returned same data in both result elements", ((SmallProject) result2Element[1]).getName().equals(((LargeProject) result2Element[0]).getName()));
        }
        // Verify fourth result set mapping --> Employee Constructor Result
        List results3 = (List) allResults.get(3);
        assertNotNull("No Employee constructor results returned", results3);
        assertTrue("Empty Employee constructor results returned", results3.size() > 0);
    }
}
Also used : SmallProject(org.eclipse.persistence.testing.models.jpa22.advanced.SmallProject) Address(org.eclipse.persistence.testing.models.jpa22.advanced.Address) StoredProcedureCall(org.eclipse.persistence.queries.StoredProcedureCall) SQLResultSetMapping(org.eclipse.persistence.queries.SQLResultSetMapping) ResultSetMappingQuery(org.eclipse.persistence.queries.ResultSetMappingQuery) EntityResult(org.eclipse.persistence.queries.EntityResult) Employee(org.eclipse.persistence.testing.models.jpa22.advanced.Employee) ConstructorResult(org.eclipse.persistence.queries.ConstructorResult) ColumnResult(org.eclipse.persistence.queries.ColumnResult) List(java.util.List) FieldResult(org.eclipse.persistence.queries.FieldResult) LargeProject(org.eclipse.persistence.testing.models.jpa22.advanced.LargeProject)

Example 4 with SQLResultSetMapping

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

the class StoredProcedureQueryTestSuite method testQueryWithMultipleResultsFromCode.

/**
 * Test multiple result sets by setting the SQL results set mapping from code.
 */
public void testQueryWithMultipleResultsFromCode() throws Exception {
    if (supportsStoredProcedures() && getPlatform().isMySQL()) {
        // SQL result set mapping for employee.
        SQLResultSetMapping employeeResultSetMapping = new SQLResultSetMapping("EmployeeResultSetMapping");
        employeeResultSetMapping.addResult(new EntityResult(Employee.class));
        // SQL result set mapping for address.
        SQLResultSetMapping addressResultSetMapping = new SQLResultSetMapping("AddressResultSetMapping");
        addressResultSetMapping.addResult(new EntityResult(Address.class));
        // SQL result set mapping for project (using inheritance and more complex result)
        SQLResultSetMapping projectResultSetMapping = new SQLResultSetMapping("ProjectResultSetMapping");
        EntityResult projectEntityResult = new EntityResult(Project.class);
        projectResultSetMapping.addResult(projectEntityResult);
        projectEntityResult = new EntityResult(SmallProject.class);
        projectEntityResult.addFieldResult(new FieldResult("id", "SMALL_ID"));
        projectEntityResult.addFieldResult(new FieldResult("name", "SMALL_NAME"));
        projectEntityResult.addFieldResult(new FieldResult("description", "SMALL_DESCRIPTION"));
        projectEntityResult.addFieldResult(new FieldResult("teamLeader", "SMALL_TEAMLEAD"));
        projectEntityResult.addFieldResult(new FieldResult("version", "SMALL_VERSION"));
        projectEntityResult.setDiscriminatorColumn("SMALL_DESCRIM");
        projectResultSetMapping.addResult(projectEntityResult);
        projectResultSetMapping.addResult(new ColumnResult("BUDGET_SUM"));
        // SQL result set mapping for employee using constructor results.
        SQLResultSetMapping employeeConstrustorResultSetMapping = new SQLResultSetMapping("EmployeeConstructorResultSetMapping");
        ConstructorResult constructorResult = new ConstructorResult(EmployeeDetails.class);
        ColumnResult columnResult = new ColumnResult("EMP_ID");
        columnResult.getColumn().setType(Integer.class);
        constructorResult.addColumnResult(columnResult);
        columnResult = new ColumnResult("F_NAME");
        columnResult.getColumn().setType(String.class);
        constructorResult.addColumnResult(columnResult);
        columnResult = new ColumnResult("L_NAME");
        columnResult.getColumn().setType(String.class);
        constructorResult.addColumnResult(columnResult);
        columnResult = new ColumnResult("R_COUNT");
        columnResult.getColumn().setType(Integer.class);
        constructorResult.addColumnResult(columnResult);
        employeeConstrustorResultSetMapping.addResult(constructorResult);
        StoredProcedureCall call = new StoredProcedureCall();
        call.setProcedureName("Read_Multiple_Result_Sets");
        call.setHasMultipleResultSets(true);
        call.setReturnMultipleResultSetCollections(true);
        ResultSetMappingQuery query = new ResultSetMappingQuery(call);
        query.addSQLResultSetMapping(employeeResultSetMapping);
        query.addSQLResultSetMapping(addressResultSetMapping);
        query.addSQLResultSetMapping(projectResultSetMapping);
        query.addSQLResultSetMapping(employeeConstrustorResultSetMapping);
        List allResults = (List) getPersistenceUnitServerSession().executeQuery(query);
        assertNotNull("No results returned", allResults);
        assertTrue("Incorrect number of results returned", allResults.size() == 4);
        // Verify first result set mapping --> Employee
        List results0 = (List) allResults.get(0);
        assertNotNull("No Employee results returned", results0);
        assertTrue("Empty Employee results returned", results0.size() > 0);
        // Verify second result set mapping --> Address
        List results1 = (List) allResults.get(1);
        assertNotNull("No Address results returned", results1);
        assertTrue("Empty Address results returned", results1.size() > 0);
        // Verify third result set mapping --> Project
        List results2 = (List) allResults.get(2);
        assertNotNull("No Project results returned", results2);
        assertTrue("Empty Project results returned", results2.size() > 0);
        for (Object result2 : results2) {
            Object[] result2Element = (Object[]) result2;
            assertTrue("Failed to Return 3 items", (result2Element.length == 3));
            // Using Number as Different db/drivers  can return different types
            // e.g. Oracle with ijdbc14.jar returns BigDecimal where as Derby
            // with derbyclient.jar returns Double. NOTE: the order of checking
            // here is valid and as defined by the spec.
            assertTrue("Failed to return LargeProject", (result2Element[0] instanceof LargeProject));
            assertTrue("Failed To Return SmallProject", (result2Element[1] instanceof SmallProject));
            assertTrue("Failed to return column", (result2Element[2] instanceof Number));
            assertFalse("Returned same data in both result elements", ((SmallProject) result2Element[1]).getName().equals(((LargeProject) result2Element[0]).getName()));
        }
        // Verify fourth result set mapping --> Employee Constructor Result
        List results3 = (List) allResults.get(3);
        assertNotNull("No Employee constructor results returned", results3);
        assertTrue("Empty Employee constructor results returned", results3.size() > 0);
    }
}
Also used : SmallProject(org.eclipse.persistence.testing.models.jpa21.advanced.SmallProject) Address(org.eclipse.persistence.testing.models.jpa21.advanced.Address) StoredProcedureCall(org.eclipse.persistence.queries.StoredProcedureCall) SQLResultSetMapping(org.eclipse.persistence.queries.SQLResultSetMapping) ResultSetMappingQuery(org.eclipse.persistence.queries.ResultSetMappingQuery) EntityResult(org.eclipse.persistence.queries.EntityResult) Employee(org.eclipse.persistence.testing.models.jpa21.advanced.Employee) ConstructorResult(org.eclipse.persistence.queries.ConstructorResult) ColumnResult(org.eclipse.persistence.queries.ColumnResult) List(java.util.List) FieldResult(org.eclipse.persistence.queries.FieldResult) LargeProject(org.eclipse.persistence.testing.models.jpa21.advanced.LargeProject)

Example 5 with SQLResultSetMapping

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

the class EntityManagerImpl method createStoredProcedureQuery.

/**
 * Create an instance of <code>StoredProcedureQuery</code> for executing a
 * stored procedure in the database.
 * <p>Parameters must be registered before the stored procedure can be
 * executed.
 * <p>The <code>resultClass</code> arguments must be specified in the order
 * in which the result sets will be returned by the stored procedure
 * invocation.
 *
 * @param procedureName name of the stored procedure in the database
 * @param resultClasses classes to which the result sets produced by the
 *        stored procedure are to be mapped
 * @return the new stored procedure query instance
 * @throws IllegalArgumentException if a stored procedure of the given name
 *         does not exist (or the query execution will fail)
 * @since EclipseLink 2.5/Java Persistence 2.1
 */
@Override
@SuppressWarnings({ "rawtypes" })
public StoredProcedureQuery createStoredProcedureQuery(String procedureName, Class... resultClasses) {
    try {
        verifyOpen();
        StoredProcedureCall call = new StoredProcedureCall();
        call.setProcedureName(procedureName);
        call.setHasMultipleResultSets(resultClasses.length > 1);
        List<SQLResultSetMapping> sqlResultSetMappings = new ArrayList<SQLResultSetMapping>();
        for (Class<?> resultClass : resultClasses) {
            sqlResultSetMappings.add(new SQLResultSetMapping(resultClass));
        }
        return new StoredProcedureQueryImpl(StoredProcedureQueryImpl.buildResultSetMappingQuery(sqlResultSetMappings, call), this);
    } catch (RuntimeException e) {
        setRollbackOnly();
        throw e;
    }
}
Also used : StoredProcedureCall(org.eclipse.persistence.queries.StoredProcedureCall) SQLResultSetMapping(org.eclipse.persistence.queries.SQLResultSetMapping) ArrayList(java.util.ArrayList)

Aggregations

SQLResultSetMapping (org.eclipse.persistence.queries.SQLResultSetMapping)5 EntityResult (org.eclipse.persistence.queries.EntityResult)3 StoredProcedureCall (org.eclipse.persistence.queries.StoredProcedureCall)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ColumnResult (org.eclipse.persistence.queries.ColumnResult)2 ConstructorResult (org.eclipse.persistence.queries.ConstructorResult)2 FieldResult (org.eclipse.persistence.queries.FieldResult)2 ResultSetMappingQuery (org.eclipse.persistence.queries.ResultSetMappingQuery)2 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)1 PartitioningPolicy (org.eclipse.persistence.descriptors.partitioning.PartitioningPolicy)1 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)1 AttributeGroup (org.eclipse.persistence.queries.AttributeGroup)1 DatabaseQuery (org.eclipse.persistence.queries.DatabaseQuery)1 Address (org.eclipse.persistence.testing.models.jpa21.advanced.Address)1 Employee (org.eclipse.persistence.testing.models.jpa21.advanced.Employee)1 LargeProject (org.eclipse.persistence.testing.models.jpa21.advanced.LargeProject)1 SmallProject (org.eclipse.persistence.testing.models.jpa21.advanced.SmallProject)1 Address (org.eclipse.persistence.testing.models.jpa22.advanced.Address)1 Employee (org.eclipse.persistence.testing.models.jpa22.advanced.Employee)1