Search in sources :

Example 1 with EntityResult

use of org.eclipse.persistence.queries.EntityResult 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 EntityResult

use of org.eclipse.persistence.queries.EntityResult 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 3 with EntityResult

use of org.eclipse.persistence.queries.EntityResult 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 4 with EntityResult

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

the class EntityResultMetadata method process.

/**
 * INTERNAL:
 * Process the entity result for the given sql result set mapping.
 */
public EntityResult process() {
    // Create a new entity result.
    EntityResult entityResult = new EntityResult(getJavaClassName(getEntityClass()));
    // Process the field results.
    for (FieldResultMetadata fieldResult : getFieldResults()) {
        entityResult.addFieldResult(fieldResult.process());
    }
    // Process the discriminator value;
    if (getDiscriminatorColumn() != null) {
        DatabaseField discriminatorField = new DatabaseField();
        // Process the name (taking into consideration delimiters etc.)
        setFieldName(discriminatorField, getDiscriminatorColumn());
        // Set the discriminator column on the entity result.
        entityResult.setDiscriminatorColumn(discriminatorField);
    }
    // Return the entity result to the caller.
    return entityResult;
}
Also used : DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) EntityResult(org.eclipse.persistence.queries.EntityResult)

Aggregations

EntityResult (org.eclipse.persistence.queries.EntityResult)4 SQLResultSetMapping (org.eclipse.persistence.queries.SQLResultSetMapping)3 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 StoredProcedureCall (org.eclipse.persistence.queries.StoredProcedureCall)2 ArrayList (java.util.ArrayList)1 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)1 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)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 LargeProject (org.eclipse.persistence.testing.models.jpa22.advanced.LargeProject)1 SmallProject (org.eclipse.persistence.testing.models.jpa22.advanced.SmallProject)1