Search in sources :

Example 16 with PersistenceUnitUtil

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

the class EntityManagerJUnitTestSuite method testIsLoaded.

public void testIsLoaded() {
    EntityManagerFactory emf = getEntityManagerFactory();
    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);
        em.persist(emp);
        em.flush();
        em.clear();
        clearCache();
        emp = em.find(Employee.class, emp.getId());
        PersistenceUnitUtil util = emf.getPersistenceUnitUtil();
        assertTrue("PersistenceUnitUtil says employee is not loaded when it is.", util.isLoaded(emp));
    } finally {
        rollbackTransaction(em);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Address(org.eclipse.persistence.testing.models.jpa.advanced.Address) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) JpaEntityManagerFactory(org.eclipse.persistence.jpa.JpaEntityManagerFactory) PersistenceUnitUtil(jakarta.persistence.PersistenceUnitUtil) PhoneNumber(org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber)

Example 17 with PersistenceUnitUtil

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

the class EntityManagerJUnitTestSuite method testIsLoadedAttribute.

public void testIsLoadedAttribute() {
    EntityManagerFactory emf = getEntityManagerFactory();
    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);
        SmallProject project = new SmallProject();
        project.setName("Utility Testing");
        project.addTeamMember(emp);
        emp.addProject(project);
        em.persist(emp);
        em.flush();
        em.clear();
        clearCache();
        emp = em.find(Employee.class, emp.getId());
        PersistenceUnitUtil util = emf.getPersistenceUnitUtil();
        if (emp instanceof PersistenceWeaved) {
            assertFalse("PersistenceUnitUtil says address is loaded when it is not", util.isLoaded(emp, "address"));
        } else {
            assertTrue("Non-weaved PersistenceUnitUtil says address is not loaded", util.isLoaded(emp, "address"));
        }
        assertFalse("PersistenceUnitUtil says projects is loaded when it is not", util.isLoaded(emp, "projects"));
        emp.getPhoneNumbers().size();
        assertTrue("PersistenceUnitUtil says phoneNumbers is not loaded when it is", util.isLoaded(emp, "phoneNumbers"));
        assertTrue("PersistenceUnitUtil says firstName is not loaded when it is", util.isLoaded(emp, "firstName"));
    } finally {
        rollbackTransaction(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) PersistenceWeaved(org.eclipse.persistence.internal.weaving.PersistenceWeaved) Address(org.eclipse.persistence.testing.models.jpa.advanced.Address) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) JpaEntityManagerFactory(org.eclipse.persistence.jpa.JpaEntityManagerFactory) PersistenceUnitUtil(jakarta.persistence.PersistenceUnitUtil) PhoneNumber(org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber)

Example 18 with PersistenceUnitUtil

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

the class EntityManagerJUnitTestSuite method testIsLoaded.

public void testIsLoaded() {
    EntityManagerFactory emf = getEntityManagerFactory();
    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);
        em.persist(emp);
        em.flush();
        em.clear();
        clearCache();
        emp = em.find(Employee.class, emp.getId());
        PersistenceUnitUtil util = emf.getPersistenceUnitUtil();
        assertTrue("PersistenceUnitUtil says employee is not loaded when it is.", util.isLoaded(emp));
    } finally {
        rollbackTransaction(em);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.composite.advanced.member_2.Employee) Address(org.eclipse.persistence.testing.models.jpa.composite.advanced.member_1.Address) JpaEntityManagerFactory(org.eclipse.persistence.jpa.JpaEntityManagerFactory) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) PersistenceUnitUtil(jakarta.persistence.PersistenceUnitUtil) PhoneNumber(org.eclipse.persistence.testing.models.jpa.composite.advanced.member_3.PhoneNumber)

Example 19 with PersistenceUnitUtil

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

the class EntityGraphTestSuite method testsubclassSubgraphs.

public void testsubclassSubgraphs() {
    EntityManager em = createEntityManager();
    EntityGraph<Project> employeeGraph = em.createEntityGraph(Project.class);
    employeeGraph.addSubclassSubgraph(LargeProject.class).addAttributeNodes("budget");
    employeeGraph.addAttributeNodes("description");
    List<Project> result = em.createQuery("Select p from Project p where type(p) = LargeProject").setHint(QueryHints.JPA_FETCH_GRAPH, employeeGraph).getResultList();
    PersistenceUnitUtil util = em.getEntityManagerFactory().getPersistenceUnitUtil();
    for (Project project : result) {
        assertFalse("Fetch Group was not applied", util.isLoaded(project, "name"));
        assertTrue("Fetch Group was not applied", util.isLoaded(project, "description"));
        assertTrue("Fetch Group was not applied", util.isLoaded(project, "budget"));
    }
}
Also used : Project(org.eclipse.persistence.testing.models.jpa21.advanced.Project) LargeProject(org.eclipse.persistence.testing.models.jpa21.advanced.LargeProject) EntityManager(jakarta.persistence.EntityManager) PersistenceUnitUtil(jakarta.persistence.PersistenceUnitUtil) LargeProject(org.eclipse.persistence.testing.models.jpa21.advanced.LargeProject)

Example 20 with PersistenceUnitUtil

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

the class EntityGraphTestSuite method testNestedEmbeddedFetchGroup.

public void testNestedEmbeddedFetchGroup() {
    EntityManager em = createEntityManager();
    EntityGraph<Runner> fetchGraph = em.createEntityGraph(Runner.class);
    fetchGraph.addSubgraph("info").addSubgraph("status").addAttributeNodes("runningStatus");
    Runner result = (Runner) em.createQuery("Select r from Runner r").setMaxResults(1).setHint(QueryHints.JPA_FETCH_GRAPH, fetchGraph).getResultList().get(0);
    PersistenceUnitUtil util = em.getEntityManagerFactory().getPersistenceUnitUtil();
    assertFalse("FetchGroup was not applied", util.isLoaded(result, "gender"));
    assertFalse("FetchGroup was not applied", util.isLoaded(result.getInfo(), "health"));
    assertTrue("FetchGroup was not applied", util.isLoaded(result.getInfo(), "status"));
    assertTrue("FetchGroup was not applied", util.isLoaded(result.getInfo().getStatus(), "runningStatus"));
    result.getInfo().getHealth();
    assertTrue("FetchGroup was not applied", util.isLoaded(result.getInfo(), "status"));
    assertTrue("FetchGroup was not applied", util.isLoaded(result, "gender"));
}
Also used : Runner(org.eclipse.persistence.testing.models.jpa21.advanced.Runner) EntityManager(jakarta.persistence.EntityManager) PersistenceUnitUtil(jakarta.persistence.PersistenceUnitUtil)

Aggregations

PersistenceUnitUtil (jakarta.persistence.PersistenceUnitUtil)27 EntityManager (jakarta.persistence.EntityManager)24 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)9 JpaEntityManagerFactory (org.eclipse.persistence.jpa.JpaEntityManagerFactory)7 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)6 Employee (org.eclipse.persistence.testing.models.jpa21.advanced.Employee)4 Employee (org.eclipse.persistence.testing.models.jpa22.advanced.Employee)4 Address (org.eclipse.persistence.testing.models.jpa.advanced.Address)3 Employee (org.eclipse.persistence.testing.models.jpa.advanced.Employee)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 PersistenceWeaved (org.eclipse.persistence.internal.weaving.PersistenceWeaved)2 LargeProject (org.eclipse.persistence.testing.models.jpa21.advanced.LargeProject)2 Project (org.eclipse.persistence.testing.models.jpa21.advanced.Project)2 Runner (org.eclipse.persistence.testing.models.jpa21.advanced.Runner)2 LargeProject (org.eclipse.persistence.testing.models.jpa22.advanced.LargeProject)2 Project (org.eclipse.persistence.testing.models.jpa22.advanced.Project)2 Runner (org.eclipse.persistence.testing.models.jpa22.advanced.Runner)2