Search in sources :

Example 26 with SmallProject

use of org.eclipse.persistence.testing.models.jpa.advanced.SmallProject in project eclipselink by eclipse-ee4j.

the class QueryCastTestSuite method testDowncastManyToManyExpressionBuilder.

public void testDowncastManyToManyExpressionBuilder() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        LargeProject proj = new LargeProject();
        proj.setBudget(1000);
        proj.setName("test1");
        em.persist(proj);
        SmallProject sp = new SmallProject();
        sp.setName("sp1");
        em.persist(sp);
        Employee emp = new Employee();
        emp.setFirstName("Reggie");
        emp.setLastName("Josephson");
        emp.addProject(proj);
        proj.addTeamMember(emp);
        emp.addProject(sp);
        sp.addTeamMember(emp);
        em.persist(emp);
        emp = new Employee();
        emp.setFirstName("Ron");
        emp.setLastName("Josephson");
        emp.addProject(sp);
        sp.addTeamMember(emp);
        em.persist(emp);
        em.flush();
        clearCache();
        em.clear();
        // create a shell query using JPA and alter the expression criteria
        // this allows us to access the base of the query
        JpaQuery query = (JpaQuery) em.createQuery("Select p from Project p");
        ReadAllQuery raq = new ReadAllQuery(Project.class);
        query.setDatabaseQuery(raq);
        Expression exp = raq.getExpressionBuilder();
        Expression criteria = exp.treat(LargeProject.class).get("budget").greaterThan(100);
        raq.setSelectionCriteria(criteria);
        List resultList = query.getResultList();
        assertTrue("Incorrect results returned", resultList.size() == 1);
    } finally {
        if (this.isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : SmallProject(org.eclipse.persistence.testing.models.jpa.advanced.SmallProject) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Expression(org.eclipse.persistence.expressions.Expression) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) List(java.util.List) JpaQuery(org.eclipse.persistence.jpa.JpaQuery) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)

Example 27 with SmallProject

use of org.eclipse.persistence.testing.models.jpa.advanced.SmallProject in project eclipselink by eclipse-ee4j.

the class SQLResultSetMappingTestSuite method testComplicateResultWithInheritance.

public void testComplicateResultWithInheritance() throws Exception {
    SQLResultSetMapping resultSetMapping = new SQLResultSetMapping("ComplicatedInheritance");
    resultSetMapping.addResult(new ColumnResult("BUDGET_SUM"));
    EntityResult entityResult = new EntityResult(Project.class);
    resultSetMapping.addResult(entityResult);
    entityResult = new EntityResult(SmallProject.class);
    entityResult.addFieldResult(new FieldResult("id", "SMALL_ID"));
    entityResult.addFieldResult(new FieldResult("name", "SMALL_NAME"));
    entityResult.addFieldResult(new FieldResult("description", "SMALL_DESCRIPTION"));
    entityResult.addFieldResult(new FieldResult("teamLeader", "SMALL_TEAMLEAD"));
    entityResult.addFieldResult(new FieldResult("version", "SMALL_VERSION"));
    entityResult.setDiscriminatorColumn("SMALL_DESCRIM");
    resultSetMapping.addResult(entityResult);
    SQLCall call = new SQLCall("SELECT (t1.BUDGET/t0.PROJ_ID) AS BUDGET_SUM, t0.PROJ_ID, t0.PROJ_TYPE, t0.PROJ_NAME, t0.DESCRIP, t0.LEADER_ID, t0.VERSION, t1.BUDGET, t2.PROJ_ID AS SMALL_ID, t2.PROJ_TYPE AS SMALL_DESCRIM, t2.PROJ_NAME AS SMALL_NAME, t2.DESCRIP AS SMALL_DESCRIPTION, t2.LEADER_ID AS SMALL_TEAMLEAD, t2.VERSION AS SMALL_VERSION FROM CMP3_PROJECT t0, CMP3_PROJECT t2, CMP3_LPROJECT t1 WHERE t1.PROJ_ID = t0.PROJ_ID AND t2.PROJ_TYPE='S'");
    ResultSetMappingQuery query = new ResultSetMappingQuery(call);
    query.setSQLResultSetMapping(resultSetMapping);
    List results = (List) getServerSession().executeQuery(query);
    assertNotNull("No result returned", results);
    assertTrue("Empty list returned", (results.size() != 0));
    for (Iterator iterator = results.iterator(); iterator.hasNext(); ) {
        Object[] resultElement = (Object[]) iterator.next();
        assertTrue("Failed to Return 3 items", (resultElement.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
        assertTrue("Failed to return column", (resultElement[0] instanceof Number));
        assertTrue("Failed to return LargeProject", (resultElement[1] instanceof LargeProject));
        assertTrue("Failed To Return SmallProject", (resultElement[2] instanceof SmallProject));
        assertFalse("Returned same data in both result elements", ((SmallProject) resultElement[2]).getName().equals(((LargeProject) resultElement[1]).getName()));
    }
}
Also used : SmallProject(org.eclipse.persistence.testing.models.jpa.advanced.SmallProject) Iterator(java.util.Iterator) List(java.util.List) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)

Example 28 with SmallProject

use of org.eclipse.persistence.testing.models.jpa.advanced.SmallProject in project eclipselink by eclipse-ee4j.

the class SQLResultSetMappingTestSuite method testPessimisticLocking.

public void testPessimisticLocking() throws Exception {
    // Not all database support locking or the for update syntax.
    if (!getDatabaseSession().getPlatform().isOracle() && !getDatabaseSession().getPlatform().isMySQL()) {
        warning("FOR UPDATE syntax not supported.");
        return;
    }
    EntityManager em = createEntityManager();
    SmallProject smallProject = (SmallProject) getServerSession().readObject(SmallProject.class);
    SQLResultSetMapping resultSetMapping = new SQLResultSetMapping("PessimisticLocking");
    EntityResult entityResult = new EntityResult(SmallProject.class);
    resultSetMapping.addResult(entityResult);
    SQLCall call = new SQLCall("SELECT t0.PROJ_ID, t0.PROJ_TYPE, t0.PROJ_NAME, t0.DESCRIP, t0.LEADER_ID, t0.VERSION FROM CMP3_PROJECT t0 WHERE t0.PROJ_ID = " + smallProject.getId() + " FOR UPDATE");
    ResultSetMappingQuery query = new ResultSetMappingQuery(call);
    query.setSQLResultSetMapping(resultSetMapping);
    query.setLockMode(ObjectBuildingQuery.LOCK);
    beginTransaction(em);
    try {
        List results = (List) ((JpaEntityManager) em.getDelegate()).getActiveSession().executeQuery(query);
        assertNotNull("No result returned", results);
        assertTrue("Empty list returned", (results.size() != 0));
        smallProject = (SmallProject) (results.get(0));
        smallProject.setDescription("A relatively new Description");
        commitTransaction(em);
    } catch (RuntimeException ex) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
        throw ex;
    }
    smallProject = em.find(SmallProject.class, smallProject.getId());
    closeEntityManager(em);
    assertTrue("Failed to update the new description", smallProject.getDescription().equals("A relatively new Description"));
}
Also used : SmallProject(org.eclipse.persistence.testing.models.jpa.advanced.SmallProject) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) List(java.util.List)

Example 29 with SmallProject

use of org.eclipse.persistence.testing.models.jpa.advanced.SmallProject in project eclipselink by eclipse-ee4j.

the class JUnitJPQLSimpleTestSuite method smallProjectMemberOfProjectsTest.

public void smallProjectMemberOfProjectsTest() {
    EntityManager em = createEntityManager();
    ReadAllQuery query = new ReadAllQuery();
    Expression selectionCriteria = new ExpressionBuilder().anyOf("projects").equal(new ExpressionBuilder(SmallProject.class));
    query.setSelectionCriteria(selectionCriteria);
    query.setReferenceClass(Employee.class);
    // gf 1395 changed jpql to not use distinct on joins
    query.dontUseDistinct();
    Vector expectedResult = (Vector) getServerSession().executeQuery(query);
    clearCache();
    // setup the EJBQL to do the same
    String ejbqlString = "SELECT OBJECT(employee) FROM Employee employee, SmallProject sp WHERE ";
    ejbqlString = ejbqlString + "sp MEMBER OF employee.projects";
    List result = em.createQuery(ejbqlString).getResultList();
    Assert.assertTrue("Simple small Project Member Of Projects test failed", comparer.compareObjects(result, expectedResult));
}
Also used : SmallProject(org.eclipse.persistence.testing.models.jpa.advanced.SmallProject) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Expression(org.eclipse.persistence.expressions.Expression) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) ArrayList(java.util.ArrayList) List(java.util.List) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder) Vector(java.util.Vector)

Example 30 with SmallProject

use of org.eclipse.persistence.testing.models.jpa.advanced.SmallProject in project eclipselink by eclipse-ee4j.

the class JUnitJPQLSimpleTestSuite method smallProjectNOTMemberOfProjectsTest.

public void smallProjectNOTMemberOfProjectsTest() {
    EntityManager em = createEntityManager();
    // query for those employees with Project named "Enterprise" (which should be
    // a SmallProject)
    ReadObjectQuery smallProjectQuery = new ReadObjectQuery();
    smallProjectQuery.setReferenceClass(SmallProject.class);
    smallProjectQuery.setSelectionCriteria(new ExpressionBuilder().get("name").equal("Enterprise"));
    SmallProject smallProject = (SmallProject) getServerSession().executeQuery(smallProjectQuery);
    ReadAllQuery query = new ReadAllQuery();
    query.addArgument("smallProject");
    Expression selectionCriteria = new ExpressionBuilder().noneOf("projects", new ExpressionBuilder().equal(new ExpressionBuilder().getParameter("smallProject")));
    query.setSelectionCriteria(selectionCriteria);
    query.setReferenceClass(Employee.class);
    Vector arguments = new Vector();
    arguments.add(smallProject);
    Vector expectedResult = (Vector) getServerSession().executeQuery(query, arguments);
    // setup the EJBQL to do the same
    String ejbqlString = "SELECT OBJECT(employee) FROM Employee employee WHERE ";
    ejbqlString = ejbqlString + "?1 NOT MEMBER OF employee.projects";
    List result = em.createQuery(ejbqlString).setParameter(1, smallProject).getResultList();
    Assert.assertTrue("Simple small Project NOT Member Of Projects test failed", comparer.compareObjects(result, expectedResult));
}
Also used : SmallProject(org.eclipse.persistence.testing.models.jpa.advanced.SmallProject) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) ReadObjectQuery(org.eclipse.persistence.queries.ReadObjectQuery) Expression(org.eclipse.persistence.expressions.Expression) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) ArrayList(java.util.ArrayList) List(java.util.List) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder) Vector(java.util.Vector)

Aggregations

SmallProject (org.eclipse.persistence.testing.models.jpa.advanced.SmallProject)30 EntityManager (jakarta.persistence.EntityManager)27 LargeProject (org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)21 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)19 Employee (org.eclipse.persistence.testing.models.jpa.advanced.Employee)17 List (java.util.List)13 ReadAllQuery (org.eclipse.persistence.queries.ReadAllQuery)11 Project (org.eclipse.persistence.testing.models.jpa.advanced.Project)11 ReadObjectQuery (org.eclipse.persistence.queries.ReadObjectQuery)9 SuperLargeProject (org.eclipse.persistence.testing.models.jpa.advanced.SuperLargeProject)9 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)8 HugeProject (org.eclipse.persistence.testing.models.jpa.advanced.HugeProject)8 SpecialHugeProject (org.eclipse.persistence.testing.models.jpa.advanced.SpecialHugeProject)8 JpaQuery (org.eclipse.persistence.jpa.JpaQuery)7 Query (jakarta.persistence.Query)6 ArrayList (java.util.ArrayList)6 ReportQuery (org.eclipse.persistence.queries.ReportQuery)6 Vector (java.util.Vector)5 Expression (org.eclipse.persistence.expressions.Expression)5 ExpressionBuilder (org.eclipse.persistence.expressions.ExpressionBuilder)5