Search in sources :

Example 1 with ProviderUtil

use of jakarta.persistence.spi.ProviderUtil in project eclipselink by eclipse-ee4j.

the class EntityManagerJUnitTestSuite method testIsLoadedWithoutReference.

public void testIsLoadedWithoutReference() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        Employee emp = new Employee();
        emp.setFirstName("Abe");
        emp.setLastName("Jones");
        Address addr = new Address();
        addr.setCity("Palo Alto");
        emp.setAddress(addr);
        PhoneNumber pn = new PhoneNumber();
        pn.setNumber("1234456");
        pn.setType("Home");
        emp.addPhoneNumber(pn);
        pn.setOwner(emp);
        Employee manager = new Employee();
        manager.addManagedEmployee(emp);
        emp.setManager(manager);
        em.persist(emp);
        em.flush();
        em.clear();
        clearCache();
        ProviderUtil util = (new PersistenceProvider()).getProviderUtil();
        if (emp instanceof PersistenceWeaved) {
            assertTrue("ProviderUtil did not return LOADED for isLoaded when it should.", util.isLoaded(emp).equals(LoadState.LOADED));
            emp = em.getReference(Employee.class, emp.getId());
            // If fetch group weaving is off then we will load EAGER mappings and LOADED will be returned.
            if (isWeavingForFetchGroupsEnabled()) {
                assertTrue("ProviderUtil did not return NOT_LOADED for isLoaded when it should.", util.isLoaded(emp).equals(LoadState.NOT_LOADED));
            } else {
                assertTrue("ProviderUtil did not return LOADED for isLoaded when it should.", util.isLoaded(emp).equals(LoadState.LOADED));
            }
        } else {
            assertTrue("(NonWeaved) ProviderUtil did not return UNKNOWN for isLoaded when it should.", util.isLoaded(emp).equals(LoadState.UNKNOWN));
            emp = em.getReference(Employee.class, emp.getId());
            assertTrue("(NonWeaved)  ProviderUtil did not return UNKNOWN for isLoaded when it should.", util.isLoaded(emp).equals(LoadState.UNKNOWN));
        }
        assertTrue("ProviderUtil did not return UNKNOWN for isLoaded when it should.", util.isLoaded(new NonEntity()).equals(LoadState.UNKNOWN));
    } finally {
        rollbackTransaction(em);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) ProviderUtil(jakarta.persistence.spi.ProviderUtil) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) PersistenceWeaved(org.eclipse.persistence.internal.weaving.PersistenceWeaved) Address(org.eclipse.persistence.testing.models.jpa.advanced.Address) PersistenceProvider(org.eclipse.persistence.jpa.PersistenceProvider) PhoneNumber(org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber) NonEntity(org.eclipse.persistence.testing.models.jpa.advanced.NonEntity)

Example 2 with ProviderUtil

use of jakarta.persistence.spi.ProviderUtil in project eclipselink by eclipse-ee4j.

the class AdvancedJPAJunitTest method testNonTriggerLazyForSProc.

// Test for lazy OneToOne relation not getting triggered
public void testNonTriggerLazyForSProc() {
    if (!supportsStoredProcedures()) {
        return;
    }
    Employee employee2;
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        Employee employee1 = new Employee();
        employee1.setFirstName("Me");
        employee1.setId(11);
        HugeProject hp = new HugeProject("big proj");
        employee1.setHugeProject(hp);
        em.persist(hp);
        em.persist(employee1);
        commitTransaction(em);
        em.clear();
        beginTransaction(em);
        Query q = em.createNamedQuery("SProcEmployee");
        q.setParameter("EMP_ID", employee1.getId());
        q.setFlushMode(FlushModeType.COMMIT);
        q.setHint(QueryHints.REFRESH, HintValues.TRUE);
        q.setHint(QueryHints.REFRESH_CASCADE, CascadePolicy.CascadeByMapping);
        employee2 = (Employee) q.getSingleResult();
        ProviderUtil util = (new PersistenceProvider()).getProviderUtil();
        // status can be LoadState.NOT_LOADED or LoadState.UNKNOWN
        assertFalse("ProviderUtil returned LOADED for isLoaded for hugeProject when it should not.", util.isLoadedWithReference(employee2, "hugeProject").equals(LoadState.LOADED));
    } catch (RuntimeException e) {
        // Re-throw exception to ensure stacktrace appears in test result.
        throw e;
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) ProviderUtil(jakarta.persistence.spi.ProviderUtil) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Query(jakarta.persistence.Query) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) DoesExistQuery(org.eclipse.persistence.queries.DoesExistQuery) TypedQuery(jakarta.persistence.TypedQuery) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) HugeProject(org.eclipse.persistence.testing.models.jpa.advanced.HugeProject) PersistenceProvider(org.eclipse.persistence.jpa.PersistenceProvider)

Example 3 with ProviderUtil

use of jakarta.persistence.spi.ProviderUtil in project eclipselink by eclipse-ee4j.

the class EntityManagerJUnitTestSuite method testIsLoadedWithReference.

public void testIsLoadedWithReference() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        Employee emp = new Employee();
        emp.setFirstName("Abe");
        emp.setLastName("Jones");
        Address addr = new Address();
        addr.setCity("Palo Alto");
        emp.setAddress(addr);
        PhoneNumber pn = new PhoneNumber();
        pn.setNumber("1234456");
        pn.setType("Home");
        emp.addPhoneNumber(pn);
        pn.setOwner(emp);
        Employee manager = new Employee();
        manager.addManagedEmployee(emp);
        emp.setManager(manager);
        em.persist(emp);
        em.flush();
        em.clear();
        clearCache();
        emp = em.find(Employee.class, emp.getId());
        emp.getAddress().getCity();
        emp.getPhoneNumbers().size();
        ProviderUtil util = (new PersistenceProvider()).getProviderUtil();
        assertTrue("ProviderUtil did not return LOADED for isLoaded for address when it should.", util.isLoadedWithReference(emp, "address").equals(LoadState.LOADED));
        assertTrue("ProviderUtil did not return LOADED for isLoaded for phoneNumbers when it should.", util.isLoadedWithReference(emp, "phoneNumbers").equals(LoadState.LOADED));
        if (emp instanceof PersistenceWeaved) {
            assertTrue("ProviderUtil did not return NOT_LOADED for isLoaded for manager when it should.", util.isLoadedWithReference(emp, "manager").equals(LoadState.NOT_LOADED));
        } else {
            assertTrue("(NonWeaved) ProviderUtil did not return LOADED for isLoaded for manager when it should.", util.isLoadedWithReference(emp, "manager").equals(LoadState.LOADED));
        }
        assertTrue("ProviderUtil did not return NOT_LOADED for isLoaded for projects when it should.", util.isLoadedWithReference(emp, "projects").equals(LoadState.NOT_LOADED));
    } finally {
        rollbackTransaction(em);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) ProviderUtil(jakarta.persistence.spi.ProviderUtil) Employee(org.eclipse.persistence.testing.models.jpa.composite.advanced.member_2.Employee) PersistenceWeaved(org.eclipse.persistence.internal.weaving.PersistenceWeaved) Address(org.eclipse.persistence.testing.models.jpa.composite.advanced.member_1.Address) PersistenceProvider(org.eclipse.persistence.jpa.PersistenceProvider) PhoneNumber(org.eclipse.persistence.testing.models.jpa.composite.advanced.member_3.PhoneNumber)

Example 4 with ProviderUtil

use of jakarta.persistence.spi.ProviderUtil in project eclipselink by eclipse-ee4j.

the class EntityManagerJUnitTestSuite method testIsLoadedWithoutReferenceAttribute.

public void testIsLoadedWithoutReferenceAttribute() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        Employee emp = new Employee();
        emp.setFirstName("Abe");
        emp.setLastName("Jones");
        Address addr = new Address();
        addr.setCity("Palo Alto");
        emp.setAddress(addr);
        PhoneNumber pn = new PhoneNumber();
        pn.setNumber("1234456");
        pn.setType("Home");
        emp.addPhoneNumber(pn);
        pn.setOwner(emp);
        Employee manager = new Employee();
        manager.addManagedEmployee(emp);
        emp.setManager(manager);
        em.persist(emp);
        em.flush();
        em.clear();
        clearCache();
        emp = em.find(Employee.class, emp.getId());
        emp.getAddress().getCity();
        emp.getPhoneNumbers().size();
        ProviderUtil util = (new PersistenceProvider()).getProviderUtil();
        if (emp instanceof PersistenceWeaved) {
            assertTrue("ProviderUtil did not return LOADED for isLoaded for address when it should.", util.isLoadedWithReference(emp, "address").equals(LoadState.LOADED));
            assertTrue("ProviderUtil did not return NOT_LOADED for isLoaded for manager when it should.", util.isLoadedWithReference(emp, "manager").equals(LoadState.NOT_LOADED));
        } else {
            assertTrue("(Unweaved) ProviderUtil did not return LOADED for isLoaded for address when it should.", util.isLoadedWithReference(emp, "address").equals(LoadState.LOADED));
            assertTrue("(Unweaved) ProviderUtil did not return LOADED for isLoaded for manager when it should.", util.isLoadedWithReference(emp, "manager").equals(LoadState.LOADED));
        }
        assertTrue("ProviderUtil did not return LOADED for isLoaded for phoneNumbers when it should.", util.isLoadedWithReference(emp, "phoneNumbers").equals(LoadState.LOADED));
        assertTrue("ProviderUtil did not return NOT_LOADED for isLoaded for projects when it should.", util.isLoadedWithReference(emp, "projects").equals(LoadState.NOT_LOADED));
        assertTrue("ProviderUtil did not return UNKNOWN for isLoaded when it should.", util.isLoaded(new NonEntity()).equals(LoadState.UNKNOWN));
    } finally {
        rollbackTransaction(em);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) ProviderUtil(jakarta.persistence.spi.ProviderUtil) Employee(org.eclipse.persistence.testing.models.jpa.composite.advanced.member_2.Employee) PersistenceWeaved(org.eclipse.persistence.internal.weaving.PersistenceWeaved) Address(org.eclipse.persistence.testing.models.jpa.composite.advanced.member_1.Address) PersistenceProvider(org.eclipse.persistence.jpa.PersistenceProvider) PhoneNumber(org.eclipse.persistence.testing.models.jpa.composite.advanced.member_3.PhoneNumber) NonEntity(org.eclipse.persistence.testing.models.jpa.composite.advanced.member_1.NonEntity)

Example 5 with ProviderUtil

use of jakarta.persistence.spi.ProviderUtil in project eclipselink by eclipse-ee4j.

the class EntityManagerJUnitTestSuite method testIsLoadedWithoutReferenceAttribute.

public void testIsLoadedWithoutReferenceAttribute() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        Employee emp = new Employee();
        emp.setFirstName("Abe");
        emp.setLastName("Jones");
        Address addr = new Address();
        addr.setCity("Palo Alto");
        emp.setAddress(addr);
        PhoneNumber pn = new PhoneNumber();
        pn.setNumber("1234456");
        pn.setType("Home");
        emp.addPhoneNumber(pn);
        pn.setOwner(emp);
        Employee manager = new Employee();
        manager.addManagedEmployee(emp);
        emp.setManager(manager);
        em.persist(emp);
        em.flush();
        em.clear();
        clearCache();
        emp = em.find(Employee.class, emp.getId());
        emp.getAddress().getCity();
        emp.getPhoneNumbers().size();
        ProviderUtil util = (new PersistenceProvider()).getProviderUtil();
        if (emp instanceof PersistenceWeaved) {
            assertTrue("ProviderUtil did not return LOADED for isLoaded for address when it should.", util.isLoadedWithReference(emp, "address").equals(LoadState.LOADED));
            assertTrue("ProviderUtil did not return NOT_LOADED for isLoaded for manager when it should.", util.isLoadedWithReference(emp, "manager").equals(LoadState.NOT_LOADED));
        } else {
            assertTrue("(Unweaved) ProviderUtil did not return LOADED for isLoaded for address when it should.", util.isLoadedWithReference(emp, "address").equals(LoadState.LOADED));
            assertTrue("(Unweaved) ProviderUtil did not return LOADED for isLoaded for manager when it should.", util.isLoadedWithReference(emp, "manager").equals(LoadState.LOADED));
        }
        assertTrue("ProviderUtil did not return LOADED for isLoaded for phoneNumbers when it should.", util.isLoadedWithReference(emp, "phoneNumbers").equals(LoadState.LOADED));
        assertTrue("ProviderUtil did not return NOT_LOADED for isLoaded for projects when it should.", util.isLoadedWithReference(emp, "projects").equals(LoadState.NOT_LOADED));
        assertTrue("ProviderUtil did not return UNKNOWN for isLoaded when it should.", util.isLoaded(new NonEntity()).equals(LoadState.UNKNOWN));
    } finally {
        rollbackTransaction(em);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) ProviderUtil(jakarta.persistence.spi.ProviderUtil) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) PersistenceWeaved(org.eclipse.persistence.internal.weaving.PersistenceWeaved) Address(org.eclipse.persistence.testing.models.jpa.advanced.Address) PersistenceProvider(org.eclipse.persistence.jpa.PersistenceProvider) PhoneNumber(org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber) NonEntity(org.eclipse.persistence.testing.models.jpa.advanced.NonEntity)

Aggregations

EntityManager (jakarta.persistence.EntityManager)7 ProviderUtil (jakarta.persistence.spi.ProviderUtil)7 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)7 PersistenceProvider (org.eclipse.persistence.jpa.PersistenceProvider)7 PersistenceWeaved (org.eclipse.persistence.internal.weaving.PersistenceWeaved)6 Employee (org.eclipse.persistence.testing.models.jpa.advanced.Employee)4 Address (org.eclipse.persistence.testing.models.jpa.advanced.Address)3 PhoneNumber (org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber)3 Address (org.eclipse.persistence.testing.models.jpa.composite.advanced.member_1.Address)3 Employee (org.eclipse.persistence.testing.models.jpa.composite.advanced.member_2.Employee)3 PhoneNumber (org.eclipse.persistence.testing.models.jpa.composite.advanced.member_3.PhoneNumber)3 NonEntity (org.eclipse.persistence.testing.models.jpa.advanced.NonEntity)2 NonEntity (org.eclipse.persistence.testing.models.jpa.composite.advanced.member_1.NonEntity)2 Query (jakarta.persistence.Query)1 TypedQuery (jakarta.persistence.TypedQuery)1 DatabaseQuery (org.eclipse.persistence.queries.DatabaseQuery)1 DoesExistQuery (org.eclipse.persistence.queries.DoesExistQuery)1 ReadAllQuery (org.eclipse.persistence.queries.ReadAllQuery)1 HugeProject (org.eclipse.persistence.testing.models.jpa.advanced.HugeProject)1