Search in sources :

Example 1 with Department

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

the class AdvancedCompositePKJunitTest method testGetIdentifier.

public void testGetIdentifier() {
    EntityManagerFactory emf = getEntityManagerFactory();
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        DepartmentPK pk = new DepartmentPK("DEPT B", "ROLE B", "LOCATION B");
        Department department = new Department();
        department.setName("DEPT B");
        department.setRole("ROLE B");
        department.setLocation("LOCATION B");
        em.persist(department);
        em.flush();
        PersistenceUnitUtil util = emf.getPersistenceUnitUtil();
        assertTrue("Got an incorrect id from persistenceUtil.getIdentifier()", pk.equals(util.getIdentifier(department)));
    } finally {
        rollbackTransaction(em);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) Department(org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Department) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) PersistenceUnitUtil(jakarta.persistence.PersistenceUnitUtil) DepartmentPK(org.eclipse.persistence.testing.models.jpa.advanced.compositepk.DepartmentPK)

Example 2 with Department

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

the class AdvancedCompositePKJunitTest method testCreateDepartment.

public void testCreateDepartment() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        // make sure the department is not left from the previous test run
        em.createQuery("DELETE FROM Department d WHERE d.name = 'DEPT A' AND d.role = 'ROLE A' AND d.location = 'LOCATION A'").executeUpdate();
        commitTransaction(em);
    } catch (RuntimeException e) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
        throw e;
    }
    clearCache();
    closeEntityManager(em);
    em = createEntityManager();
    beginTransaction(em);
    try {
        Department department = new Department();
        department.setName("DEPT A");
        department.setRole("ROLE A");
        department.setLocation("LOCATION A");
        Competency competency = new Competency();
        competency.description = "Manage groups";
        competency.rating = 9;
        department.addCompetency(competency);
        em.persist(department);
        commitTransaction(em);
        m_departmentPK = department.getPK();
    } catch (RuntimeException e) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
        throw e;
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) Department(org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Department) Competency(org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Competency)

Example 3 with Department

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

the class AdvancedCompositePKJunitTest method testAnyAndAll.

// bug gf672 - JPQL Select query with IN/ANY in WHERE clause and subselect fails.
public void testAnyAndAll() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        CriteriaBuilder qb = em.getCriteriaBuilder();
        CriteriaQuery<Scientist> cq = qb.createQuery(Scientist.class);
        Subquery sq = cq.subquery(Scientist.class);
        Root<Scientist> from_scientist = cq.from(Scientist.class);
        cq.where(qb.equal(from_scientist, qb.any(sq)));
        Query query1 = em.createQuery(cq);
        List<Scientist> results1 = query1.getResultList();
        // Query query2 = em.createQuery("SELECT s FROM Scientist s WHERE s = ALL (SELECT s2 FROM Scientist s2)");
        qb = em.getCriteriaBuilder();
        cq = qb.createQuery(Scientist.class);
        sq = cq.subquery(Scientist.class);
        from_scientist = cq.from(Scientist.class);
        cq.where(qb.equal(from_scientist, qb.all(sq)));
        Query query2 = em.createQuery(cq);
        List<Scientist> results2 = query2.getResultList();
        // Query query3 = em.createQuery("SELECT s FROM Scientist s WHERE s.department = ALL (SELECT DISTINCT d FROM Department d WHERE d.name = 'DEPT A' AND d.role = 'ROLE A' AND d.location = 'LOCATION A')");
        qb = em.getCriteriaBuilder();
        cq = qb.createQuery(Scientist.class);
        sq = cq.subquery(Department.class);
        sq.distinct(true);
        Root<Department> from_department = sq.from(Department.class);
        sq.where(qb.and(qb.and(qb.equal(from_department.get("name"), "DEPT A"), qb.equal(from_department.get("role"), "ROLE A")), qb.equal(from_department.get("location"), "LOCATION A")));
        from_scientist = cq.from(Scientist.class);
        cq.where(qb.equal(from_scientist.get("department"), qb.all(sq)));
        Query query3 = em.createQuery(cq);
        List<Scientist> results3 = query3.getResultList();
        // Query query4 = em.createQuery("SELECT s FROM Scientist s WHERE s.department = ANY (SELECT DISTINCT d FROM Department d JOIN d.scientists ds JOIN ds.cubicle c WHERE c.code = 'G')");
        qb = em.getCriteriaBuilder();
        cq = qb.createQuery(Scientist.class);
        sq = cq.subquery(Department.class);
        sq.distinct(true);
        from_department = sq.from(Department.class);
        Join c = from_department.join("scientists").join("cubicle");
        sq.where(qb.equal(c.get("code"), "G"));
        from_scientist = cq.from(Scientist.class);
        cq.where(qb.equal(from_scientist.get("department"), qb.any(sq)));
        Query query4 = em.createQuery(cq);
        List<Scientist> results4 = query4.getResultList();
        // control queries
        // Query controlQuery1 = em.createQuery("SELECT s FROM Scientist s");
        Query controlQuery1 = em.createQuery(em.getCriteriaBuilder().createQuery(Scientist.class));
        List<Scientist> controlResults1 = controlQuery1.getResultList();
        List<Scientist> controlResults2;
        if (controlResults1.size() == 1) {
            controlResults2 = controlResults1;
        } else {
            controlResults2 = new ArrayList<Scientist>();
        }
        // Query controlQuery3 = em.createQuery("SELECT s FROM Scientist s JOIN s.department d WHERE d.name = 'DEPT A' AND d.role = 'ROLE A' AND d.location = 'LOCATION A'");
        qb = em.getCriteriaBuilder();
        cq = qb.createQuery(Scientist.class);
        from_scientist = cq.from(Scientist.class);
        Join d = from_scientist.join("department");
        cq.where(qb.and(qb.and(qb.equal(d.get("name"), "DEPT A"), qb.equal(d.get("role"), "ROLE A")), qb.equal(d.get("location"), "LOCATION A")));
        Query controlQuery3 = em.createQuery(cq);
        List<Scientist> controlResults3 = controlQuery3.getResultList();
        // Query controlQuery4 = em.createQuery("SELECT s FROM Scientist s WHERE EXISTS (SELECT DISTINCT d FROM Department d JOIN d.scientists ds JOIN ds.cubicle c WHERE c.code = 'G' AND d = s.department)");
        qb = em.getCriteriaBuilder();
        cq = qb.createQuery(Scientist.class);
        sq = cq.subquery(Department.class);
        sq.distinct(true);
        from_department = sq.from(Department.class);
        from_scientist = cq.from(Scientist.class);
        c = from_department.join("scientists").join("cubicle");
        sq.where(qb.and(qb.equal(c.get("code"), "G"), qb.equal(from_department, from_scientist.get("department"))));
        cq.where(qb.exists(sq));
        Query controlQuery4 = em.createQuery(cq);
        List<Scientist> controlResults4 = controlQuery4.getResultList();
        // compare results - they should be the same
        compareResults(results1, controlResults1, "query1");
        compareResults(results2, controlResults2, "query2");
        compareResults(results3, controlResults3, "query3");
        compareResults(results4, controlResults4, "query4");
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) Department(org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Department) Query(jakarta.persistence.Query) Scientist(org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Scientist)

Example 4 with Department

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

the class AdvancedCompositePKJunitTest method testJoinColumnSharesPK.

public void testJoinColumnSharesPK() {
    EntityManagerFactory emf = getEntityManagerFactory();
    EntityManager em = createEntityManager();
    beginTransaction(em);
    org.eclipse.persistence.descriptors.ClassDescriptor descriptor = getServerSession().getDescriptor(Office.class);
    try {
        Department department = new Department();
        department.setName("DEPT B");
        department.setRole("ROLE B");
        department.setLocation("LOCATION B");
        em.persist(department);
        Office office = new Office();
        office.setId(1);
        office.setLocation("LOCATION B");
        office.setDepartment(department);
        em.persist(office);
        department.getOffices().add(office);
        em.flush();
        clearCache();
        office = em.find(Office.class, new OfficePK(1, "LOCATION B"));
        department = em.find(Department.class, new DepartmentPK("DEPT B", "ROLE B", "LOCATION B"));
        assertTrue("Office's department not properly persisted", office.getDepartment() != null);
        assertTrue("Department's offices not properly persisted", department.getOffices().size() > 0);
    } catch (Exception e) {
        fail("Exception thrown while inserting an object with a read-only column in a foreign key." + e);
    } finally {
        rollbackTransaction(em);
    }
}
Also used : Office(org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Office) EntityManager(jakarta.persistence.EntityManager) Department(org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Department) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) OfficePK(org.eclipse.persistence.testing.models.jpa.advanced.compositepk.OfficePK) DepartmentPK(org.eclipse.persistence.testing.models.jpa.advanced.compositepk.DepartmentPK) PersistenceException(jakarta.persistence.PersistenceException)

Example 5 with Department

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

the class AdvancedCompositePKJunitTest method testReadDepartment.

public void testReadDepartment() {
    Department department = createEntityManager().find(Department.class, m_departmentPK);
    assertTrue("Error on reading back the ordered department list.", department != null);
    assertTrue("The number of scientists were incorrect.", department.getScientists().size() > 0);
}
Also used : Department(org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Department)

Aggregations

Department (org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Department)9 EntityManager (jakarta.persistence.EntityManager)8 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)3 PersistenceException (jakarta.persistence.PersistenceException)2 Employee (org.eclipse.persistence.testing.models.jpa.advanced.Employee)2 Competency (org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Competency)2 DepartmentPK (org.eclipse.persistence.testing.models.jpa.advanced.compositepk.DepartmentPK)2 Scientist (org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Scientist)2 Administrator (org.eclipse.persistence.testing.models.jpa.advanced.derivedid.Administrator)2 DepartmentAdminRole (org.eclipse.persistence.testing.models.jpa.advanced.derivedid.DepartmentAdminRole)2 PersistenceUnitUtil (jakarta.persistence.PersistenceUnitUtil)1 Query (jakarta.persistence.Query)1 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)1 CopyGroup (org.eclipse.persistence.sessions.CopyGroup)1 Cubicle (org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Cubicle)1 JuniorScientist (org.eclipse.persistence.testing.models.jpa.advanced.compositepk.JuniorScientist)1 Office (org.eclipse.persistence.testing.models.jpa.advanced.compositepk.Office)1 OfficePK (org.eclipse.persistence.testing.models.jpa.advanced.compositepk.OfficePK)1 AdminPool (org.eclipse.persistence.testing.models.jpa.advanced.derivedid.AdminPool)1 DepartmentAdminRolePK (org.eclipse.persistence.testing.models.jpa.advanced.derivedid.DepartmentAdminRolePK)1