Search in sources :

Example 11 with Project

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

the class EntityManagerJUnitTestSuite method testCascadeMergeManaged.

// Test cascade merge on a managed entity
// Test for GF#1139 - Cascade doesn't work when merging managed entity
public void testCascadeMergeManaged() {
    // setup
    Project p1 = new Project();
    p1.setName("Project1");
    Project p2 = new Project();
    p1.setName("Project2");
    Employee e1 = new Employee();
    e1.setFirstName("Employee1");
    Employee e2 = new Employee();
    e2.setFirstName("Employee2");
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        em.persist(p1);
        em.persist(p2);
        em.persist(e1);
        em.persist(e2);
        commitTransaction(em);
    } catch (RuntimeException re) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        throw re;
    }
    closeEntityManager(em);
    // end of setup
    // p1,p2,e1,e2 are detached
    em = createEntityManager();
    beginTransaction(em);
    try {
        Project mp1 = em.merge(p1);
        assertTrue(em.contains(mp1));
        assertTrue("Managed instance and detached instance must not be same", mp1 != p1);
        // p1 -> e1 (one-to-one)
        mp1.setTeamLeader(e1);
        // merge again - trigger cascade merge
        mp1 = em.merge(mp1);
        Employee me1 = mp1.getTeamLeader();
        assertTrue("Cascade merge failed", em.contains(me1));
        assertTrue("Managed instance and detached instance must not be same", me1 != e1);
        // e1 -> e2 (one-to-many)
        me1.addManagedEmployee(e2);
        // merge again - trigger cascade merge
        me1 = em.merge(me1);
        Employee me2 = me1.getManagedEmployees().iterator().next();
        assertTrue("Cascade merge failed", em.contains(me2));
        assertTrue("Managed instance and detached instance must not be same", me2 != e2);
        // e2 -> p2 (many-to-many)
        me2.addProject(p2);
        p2.addTeamMember(me2);
        // merge again - trigger cascade merge
        me2 = em.merge(me2);
        Project mp2 = me2.getProjects().iterator().next();
        assertTrue("Cascade merge failed", em.contains(mp2));
        assertTrue("Managed instance and detached instance must not be same", mp2 != p2);
        commitTransaction(em);
    } catch (RuntimeException re) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        throw re;
    }
    closeEntityManager(em);
}
Also used : 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) SuperLargeProject(org.eclipse.persistence.testing.models.jpa.advanced.SuperLargeProject) SpecialHugeProject(org.eclipse.persistence.testing.models.jpa.advanced.SpecialHugeProject) 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)

Example 12 with Project

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

the class EntityManagerJUnitTestSuite method internalTestUpdateAllProjectsWithName.

protected void internalTestUpdateAllProjectsWithName(Class<?> cls) {
    if ((JUnitTestCase.getServerSession()).getPlatform().isSymfoware()) {
        getServerSession().logMessage("Test testUpdateAll*ProjectsWithName skipped for this platform, " + "Symfoware doesn't support UpdateAll/DeleteAll on multi-table objects (see rfe 298193).");
        return;
    }
    String className = Helper.getShortClassName(cls);
    String name = "testUpdateAllProjects";
    String newName = "testUpdateAllProjectsNEW";
    boolean ok = false;
    try {
        // setup
        // make sure no projects with the specified names exist
        deleteProjectsWithName(name);
        deleteProjectsWithName(newName);
        // populate Projects
        EntityManager em = createEntityManager();
        beginTransaction(em);
        try {
            createProjectsWithName(name, null, em);
            commitTransaction(em);
        } finally {
            closeEntityManagerAndTransaction(em);
        }
        // test
        em = createEntityManager();
        beginTransaction(em);
        try {
            em.createQuery("UPDATE " + className + " p set p.name = '" + newName + "' WHERE p.name = '" + name + "'").executeUpdate();
            commitTransaction(em);
        } finally {
            closeEntityManagerAndTransaction(em);
        }
        // verify
        em = createEntityManager();
        String errorMsg = "";
        try {
            List projects = em.createQuery("SELECT OBJECT(p) FROM Project p WHERE p.name = '" + newName + "' OR p.name = '" + name + "'").getResultList();
            for (int i = 0; i < projects.size(); i++) {
                Project p = (Project) projects.get(i);
                String readName = p.getName();
                if (cls.isInstance(p)) {
                    if (!readName.equals(newName)) {
                        errorMsg = errorMsg + "haven't updated name: " + p + "; ";
                    }
                } else {
                    if (readName.equals(newName)) {
                        errorMsg = errorMsg + "have updated name: " + p + "; ";
                    }
                }
            }
        } catch (RuntimeException ex) {
            if (usesSOP() && !isSOPRecoverable()) {
                if (ex instanceof PersistenceException) {
                    if (ex.getCause() instanceof QueryException && ((QueryException) ex.getCause()).getErrorCode() == QueryException.SOP_OBJECT_IS_NOT_FOUND) {
                    // getResultList is expected to fail because SOP field is set to null after bulk update
                    } else {
                        fail("Wrong cause of PersistenceException: " + ex.getCause());
                    }
                } else {
                    fail("PersistenceException was expected");
                }
            } else {
                throw ex;
            }
        } finally {
            closeEntityManager(em);
        }
        if (errorMsg.length() > 0) {
            fail(errorMsg);
        } else {
            ok = true;
        }
    } finally {
        // make sure no projects with the specified names left
        try {
            deleteProjectsWithName(name);
            deleteProjectsWithName(newName);
        } catch (RuntimeException ex) {
            // eat clean-up exception in case the test failed
            if (ok) {
                throw ex;
            }
        }
    }
}
Also used : 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) SuperLargeProject(org.eclipse.persistence.testing.models.jpa.advanced.SuperLargeProject) SpecialHugeProject(org.eclipse.persistence.testing.models.jpa.advanced.SpecialHugeProject) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) QueryException(org.eclipse.persistence.exceptions.QueryException) PersistenceException(jakarta.persistence.PersistenceException) IndirectList(org.eclipse.persistence.indirection.IndirectList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 13 with Project

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

the class EntityMethodPostLoadTransactionTest method test.

@Override
public void test() throws Exception {
    beginTransaction();
    // New object, count starts at 0.
    m_beforeEvent = 0;
    Project project = getEntityManager().find(Project.class, m_project.getId());
    m_afterEvent = project.post_load_count;
    this.rollbackTransaction();
}
Also used : Project(org.eclipse.persistence.testing.models.jpa.advanced.Project)

Example 14 with Project

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

the class EntityMethodPostUpdateTest method test.

@Override
public void test() throws Exception {
    // Loading a new object to update, count starts at 0.
    m_beforeEvent = 0;
    Project project = updateProject();
    m_afterEvent = project.post_update_count;
}
Also used : Project(org.eclipse.persistence.testing.models.jpa.advanced.Project)

Example 15 with Project

use of org.eclipse.persistence.testing.models.jpa.advanced.Project 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 largeProjectRoot = qb.treat(root, LargeProject.class);
        cq.select(largeProjectRoot.get("budget"));
        List 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) Root(jakarta.persistence.criteria.Root) List(java.util.List) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)

Aggregations

Project (org.eclipse.persistence.testing.models.jpa.advanced.Project)65 EntityManager (jakarta.persistence.EntityManager)52 Employee (org.eclipse.persistence.testing.models.jpa.advanced.Employee)42 SmallProject (org.eclipse.persistence.testing.models.jpa.advanced.SmallProject)39 LargeProject (org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)38 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)31 HugeProject (org.eclipse.persistence.testing.models.jpa.advanced.HugeProject)25 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)19 SpecialHugeProject (org.eclipse.persistence.testing.models.jpa.advanced.SpecialHugeProject)18 SuperLargeProject (org.eclipse.persistence.testing.models.jpa.advanced.SuperLargeProject)18 List (java.util.List)15 ArrayList (java.util.ArrayList)14 JpaCriteriaBuilder (org.eclipse.persistence.jpa.JpaCriteriaBuilder)12 Query (jakarta.persistence.Query)9 ReadAllQuery (org.eclipse.persistence.queries.ReadAllQuery)9 PhoneNumber (org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber)8 PersistenceException (jakarta.persistence.PersistenceException)6 Predicate (jakarta.persistence.criteria.Predicate)6 TypedQuery (jakarta.persistence.TypedQuery)5 HashSet (java.util.HashSet)5