Search in sources :

Example 21 with LargeProject

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

the class JUnitCriteriaSimpleTestSuiteBase method simpleTypeTest.

public void simpleTypeTest() {
    EntityManager em = createEntityManager();
    List expectedResult = getServerSession().readAllObjects(LargeProject.class);
    clearCache();
    // "SELECT OBJECT(proj) FROM Project proj WHERE TYPE(proj) = LargeProject"
    CriteriaBuilder qb = em.getCriteriaBuilder();
    CriteriaQuery<Project> cq = qb.createQuery(Project.class);
    cq.where(qb.equal(wrapper.from(cq, Project.class).type(), LargeProject.class));
    beginTransaction(em);
    try {
        List<Project> result = em.createQuery(cq).getResultList();
        assertTrue("SimpleTypeTest", comparer.compareObjects(result, expectedResult));
    } finally {
        rollbackTransaction(em);
        closeEntityManager(em);
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) Project(org.eclipse.persistence.testing.models.jpa.advanced.Project) HugeProject(org.eclipse.persistence.testing.models.jpa.advanced.HugeProject) SmallProject(org.eclipse.persistence.testing.models.jpa.advanced.SmallProject) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) List(java.util.List) ArrayList(java.util.ArrayList) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)

Example 22 with LargeProject

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

the class JUnitCriteriaSimpleTestSuiteBase method largeProjectCastTest.

public void largeProjectCastTest() {
    EntityManager em = createEntityManager();
    ReadAllQuery query = new ReadAllQuery();
    Expression selectionCriteria = new ExpressionBuilder().anyOf("projects").treat(LargeProject.class).get("budget").equal(5000);
    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();
    // "SELECT e from Employee e join cast(e.project, LargeProject) p where p.budget = 1000
    CriteriaBuilder qb1 = em.getCriteriaBuilder();
    CriteriaQuery<Employee> cq1 = qb1.createQuery(Employee.class);
    Root<Employee> empRoot = wrapper.from(cq1, Employee.class);
    Join<Employee, Project> join = wrapper.join(empRoot, Employee_projects);
    jakarta.persistence.criteria.Expression exp = wrapper.get((Path) join.as(LargeProject.class), LargeProject_budget);
    cq1.where(qb1.equal(exp, 5000));
    beginTransaction(em);
    try {
        List<Employee> result = em.createQuery(cq1).getResultList();
        assertTrue("LargeProject cast failed.", comparer.compareObjects(result, expectedResult));
    } finally {
        rollbackTransaction(em);
        closeEntityManager(em);
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder) Project(org.eclipse.persistence.testing.models.jpa.advanced.Project) HugeProject(org.eclipse.persistence.testing.models.jpa.advanced.HugeProject) SmallProject(org.eclipse.persistence.testing.models.jpa.advanced.SmallProject) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject) 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) Vector(java.util.Vector)

Example 23 with LargeProject

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

the class CriteriaQueryCastTestSuite 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");
        CriteriaBuilder qb = em.getCriteriaBuilder();
        CriteriaQuery<Number> cq = qb.createQuery(Number.class);
        Root<Employee> root = cq.from(Employee.class);
        Join<Object, LargeProject> l = qb.treat(root.join("projects"), LargeProject.class);
        cq.select(qb.max(l.get("budget")));
        List<Number> resultList = em.createQuery(cq).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 : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) EntityManager(jakarta.persistence.EntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)

Example 24 with LargeProject

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

the class CriteriaQueryCastTestSuite method testDowncastInSelect.

public void testDowncastInSelect() {
    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();
        // this would work in the past if TYPE was added to the where clause
        // Query query = em.createQuery("Select treat(c as LargeProject).budget from Project c");
        CriteriaBuilder qb = em.getCriteriaBuilder();
        CriteriaQuery<Project> cq = qb.createQuery(Project.class);
        Root<Project> root = cq.from(Project.class);
        Root<LargeProject> largeProjectRoot = qb.treat(root, LargeProject.class);
        cq.select(largeProjectRoot.get("budget"));
        List<Project> resultList = em.createQuery(cq).getResultList();
        assertTrue("Incorrect results returned", resultList.size() == 1);
    } finally {
        if (this.isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) SmallProject(org.eclipse.persistence.testing.models.jpa.advanced.SmallProject) Project(org.eclipse.persistence.testing.models.jpa.advanced.Project) SmallProject(org.eclipse.persistence.testing.models.jpa.advanced.SmallProject) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject) EntityManager(jakarta.persistence.EntityManager) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)

Example 25 with LargeProject

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

the class CriteriaQueryCastTestSuite 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();
        // JpaQuery query = (JpaQuery)em.createQuery("Select p from Project p where treat (p as LargeProject).budget > 100");
        CriteriaBuilder qb = em.getCriteriaBuilder();
        CriteriaQuery<Project> cq = qb.createQuery(Project.class);
        Root<Project> root = cq.from(Project.class);
        Root<LargeProject> largeProjectRoot = qb.treat(root, LargeProject.class);
        cq.where(qb.gt(largeProjectRoot.get("budget"), 100));
        List<Project> resultList = em.createQuery(cq).getResultList();
        assertTrue("Incorrect results returned", resultList.size() == 1);
    } finally {
        if (this.isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) SmallProject(org.eclipse.persistence.testing.models.jpa.advanced.SmallProject) Project(org.eclipse.persistence.testing.models.jpa.advanced.Project) SmallProject(org.eclipse.persistence.testing.models.jpa.advanced.SmallProject) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject) EntityManager(jakarta.persistence.EntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) 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