Search in sources :

Example 31 with LargeProject

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

the class QueryCastTestSuite method testTreatInSelect.

public void testTreatInSelect() {
    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);
        em.flush();
        clearCache();
        em.clear();
        Query query = em.createQuery("Select treat(c as LargeProject).budget from Project c");
        List resultList = query.getResultList();
        assertTrue("Incorrect results returned, expected 1, received: " + resultList.size(), 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) ReportQuery(org.eclipse.persistence.queries.ReportQuery) JpaQuery(org.eclipse.persistence.jpa.JpaQuery) Query(jakarta.persistence.Query) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) ReadObjectQuery(org.eclipse.persistence.queries.ReadObjectQuery) List(java.util.List) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)

Example 32 with LargeProject

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

the class QueryCastTestSuite method testSelectCast.

public void testSelectCast() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        LargeProject proj = new LargeProject();
        proj.setBudget(1000);
        proj.setName("test1");
        em.persist(proj);
        LargeProject lp = new LargeProject();
        lp.setBudget(100);
        lp.setName("sp1");
        em.persist(lp);
        Employee emp = new Employee();
        emp.setFirstName("Reggie");
        emp.setLastName("Josephson");
        emp.addProject(proj);
        proj.addTeamMember(emp);
        emp.addProject(lp);
        lp.addTeamMember(emp);
        em.persist(emp);
        emp = new Employee();
        emp.setFirstName("Ron");
        emp.setLastName("Josephson");
        emp.addProject(lp);
        lp.addTeamMember(emp);
        em.persist(emp);
        em.flush();
        clearCache();
        em.clear();
        Query query = em.createQuery("Select max(l.budget) from Employee e join treat(e.projects as LargeProject) l");
        List resultList = query.getResultList();
        assertTrue("Incorrect result size returned", resultList.size() == 1);
        assertTrue("Incorrect results returned", (Double) resultList.get(0) == 1000);
    } finally {
        if (this.isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) ReportQuery(org.eclipse.persistence.queries.ReportQuery) JpaQuery(org.eclipse.persistence.jpa.JpaQuery) Query(jakarta.persistence.Query) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) ReadObjectQuery(org.eclipse.persistence.queries.ReadObjectQuery) List(java.util.List) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)

Example 33 with LargeProject

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

the class QueryCastTestSuite method testCastInSubselect.

public void testCastInSubselect() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        LargeProject proj = new LargeProject();
        proj.setBudget(1000);
        proj.setName("test1");
        em.persist(proj);
        LargeProject lp = new LargeProject();
        lp.setBudget(100);
        lp.setName("sp1");
        em.persist(lp);
        Employee emp = new Employee();
        emp.setFirstName("Reggie");
        emp.setLastName("Josephson");
        emp.addProject(proj);
        proj.addTeamMember(emp);
        emp.addProject(lp);
        lp.addTeamMember(emp);
        emp.setSalary(10000);
        em.persist(emp);
        emp = new Employee();
        emp.setFirstName("Ron");
        emp.setLastName("Josephson");
        emp.addProject(lp);
        lp.addTeamMember(emp);
        em.persist(emp);
        emp.setSalary(100);
        em.flush();
        clearCache();
        em.clear();
        Query query = em.createQuery("select e from Employee e where e.salary > (Select max(l.budget) from Employee emp join treat(emp.projects as LargeProject) l)");
        List resultList = query.getResultList();
        assertTrue("Incorrect result size returned", resultList.size() == 1);
    } finally {
        if (this.isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) ReportQuery(org.eclipse.persistence.queries.ReportQuery) JpaQuery(org.eclipse.persistence.jpa.JpaQuery) Query(jakarta.persistence.Query) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) ReadObjectQuery(org.eclipse.persistence.queries.ReadObjectQuery) List(java.util.List) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)

Example 34 with LargeProject

use of org.eclipse.persistence.testing.models.jpa.advanced.LargeProject 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 35 with LargeProject

use of org.eclipse.persistence.testing.models.jpa.advanced.LargeProject 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)

Aggregations

LargeProject (org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)36 EntityManager (jakarta.persistence.EntityManager)33 SmallProject (org.eclipse.persistence.testing.models.jpa.advanced.SmallProject)24 Employee (org.eclipse.persistence.testing.models.jpa.advanced.Employee)23 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)22 List (java.util.List)15 Project (org.eclipse.persistence.testing.models.jpa.advanced.Project)14 SuperLargeProject (org.eclipse.persistence.testing.models.jpa.advanced.SuperLargeProject)13 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)12 ReadAllQuery (org.eclipse.persistence.queries.ReadAllQuery)11 HugeProject (org.eclipse.persistence.testing.models.jpa.advanced.HugeProject)11 JpaQuery (org.eclipse.persistence.jpa.JpaQuery)9 ReportQuery (org.eclipse.persistence.queries.ReportQuery)9 SpecialHugeProject (org.eclipse.persistence.testing.models.jpa.advanced.SpecialHugeProject)9 Query (jakarta.persistence.Query)8 ReadObjectQuery (org.eclipse.persistence.queries.ReadObjectQuery)8 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 PersistenceException (jakarta.persistence.PersistenceException)4 TypedQuery (jakarta.persistence.TypedQuery)3