Search in sources :

Example 16 with LargeProject

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

the class EntityManagerJUnitTestSuite method testDatabaseSyncNewObject.

public void testDatabaseSyncNewObject() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        Project project = new LargeProject();
        em.persist(project);
        project.setName("Blah");
        project.setTeamLeader(new Employee());
        project.getTeamLeader().addProject(project);
        em.flush();
    } catch (RuntimeException ex) {
        rollbackTransaction(em);
        if (ex instanceof IllegalStateException)
            return;
    }
    fail("Failed to throw illegal argument when finding unregistered new object cascading on database sync");
}
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) SuperLargeProject(org.eclipse.persistence.testing.models.jpa.advanced.SuperLargeProject) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)

Example 17 with LargeProject

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

the class EntityManagerJUnitTestSuite method testOverwrittingAndOverLoadingMethods.

// This test weaving works with over-writing methods in subclasses, and overloading methods.
public void testOverwrittingAndOverLoadingMethods() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        Address address = new Address();
        address.setCity("Ottawa");
        Employee employee = new Employee();
        employee.setAddress(address);
        LargeProject project = new LargeProject();
        project.setTeamLeader(employee);
        em.persist(employee);
        em.persist(project);
        commitTransaction(em);
        closeEntityManager(em);
        em = createEntityManager();
        beginTransaction(em);
        employee = em.find(Employee.class, employee.getId());
        project = em.find(LargeProject.class, project.getId());
        if ((employee.getAddress("Home") == null) || (!employee.getAddress("Home").getCity().equals("Ottawa"))) {
            fail("Get address did not work.");
        }
        employee.setAddress("Toronto");
        if (!employee.getAddress().getCity().equals("Toronto")) {
            fail("Set address did not work.");
        }
        if (project.getTeamLeader() != employee) {
            fail("Get team leader did not work, team is: " + project.getTeamLeader() + " but should be:" + employee);
        }
        em.remove(employee.getAddress());
        em.remove(employee);
        em.remove(project);
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(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) SuperLargeProject(org.eclipse.persistence.testing.models.jpa.advanced.SuperLargeProject) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)

Example 18 with LargeProject

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

the class EntityManagerJUnitTestSuite method testWeaving.

/**
 * Test that all of the classes in the advanced model were weaved as expected.
 */
public void testWeaving() {
    // Only test if weaving was on, test runs without weaving must set this system property.
    if (isWeavingEnabled()) {
        internalTestWeaving(new Employee(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), true);
        internalTestWeaving(new FormerEmployment(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new Address(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new PhoneNumber(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new EmploymentPeriod(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        // field-locking
        internalTestWeaving(new Buyer(), false, isWeavingForFetchGroupsEnabled(), false);
        // field-locking
        internalTestWeaving(new GoldBuyer(), false, isWeavingForFetchGroupsEnabled(), false);
        // field-locking
        internalTestWeaving(new PlatinumBuyer(), false, isWeavingForFetchGroupsEnabled(), false);
        // eager 1-m
        internalTestWeaving(new Department(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new Golfer(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new GolferPK(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new SmallProject(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new LargeProject(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new Man(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new Woman(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        // serialized
        internalTestWeaving(new Vegetable(), false, isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new VegetablePK(), false, isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new WorldRank(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new Equipment(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new EquipmentCode(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new PartnerLink(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
    }
}
Also used : EmploymentPeriod(org.eclipse.persistence.testing.models.jpa.advanced.EmploymentPeriod) SmallProject(org.eclipse.persistence.testing.models.jpa.advanced.SmallProject) VegetablePK(org.eclipse.persistence.testing.models.jpa.advanced.VegetablePK) WorldRank(org.eclipse.persistence.testing.models.jpa.advanced.WorldRank) Address(org.eclipse.persistence.testing.models.jpa.advanced.Address) EquipmentCode(org.eclipse.persistence.testing.models.jpa.advanced.EquipmentCode) FormerEmployment(org.eclipse.persistence.testing.models.jpa.advanced.FormerEmployment) GoldBuyer(org.eclipse.persistence.testing.models.jpa.advanced.GoldBuyer) PlatinumBuyer(org.eclipse.persistence.testing.models.jpa.advanced.PlatinumBuyer) Buyer(org.eclipse.persistence.testing.models.jpa.advanced.Buyer) GoldBuyer(org.eclipse.persistence.testing.models.jpa.advanced.GoldBuyer) PlatinumBuyer(org.eclipse.persistence.testing.models.jpa.advanced.PlatinumBuyer) Department(org.eclipse.persistence.testing.models.jpa.advanced.Department) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Equipment(org.eclipse.persistence.testing.models.jpa.advanced.Equipment) GolferPK(org.eclipse.persistence.testing.models.jpa.advanced.GolferPK) PhoneNumber(org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber) Golfer(org.eclipse.persistence.testing.models.jpa.advanced.Golfer) Man(org.eclipse.persistence.testing.models.jpa.advanced.Man) Woman(org.eclipse.persistence.testing.models.jpa.advanced.Woman) PartnerLink(org.eclipse.persistence.testing.models.jpa.advanced.PartnerLink) Vegetable(org.eclipse.persistence.testing.models.jpa.advanced.Vegetable) SuperLargeProject(org.eclipse.persistence.testing.models.jpa.advanced.SuperLargeProject) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)

Example 19 with LargeProject

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

the class EntityManagerJUnitTestSuite method testNonRefreshLazyRelationship.

// Bug 335322
public void testNonRefreshLazyRelationship() {
    if (isOnServer()) {
        return;
    }
    EntityManager setupEm = createEntityManager();
    setupEm.getTransaction().begin();
    Employee emp = new Employee();
    setupEm.persist(emp);
    Employee direct = new Employee("Managed", "EMployee");
    setupEm.persist(direct);
    emp.getManagedEmployees().add(direct);
    direct.setManager(emp);
    Dealer dealer = new Dealer("Jim", "Bob");
    emp.getDealers().add(dealer);
    setupEm.persist(dealer);
    Project project = new LargeProject("Alpha Alpha Zulu");
    setupEm.persist(project);
    emp.getProjects().add(project);
    setupEm.getTransaction().commit();
    clearCache();
    EntityManager em1 = createEntityManager();
    EntityManager em2 = createEntityManager();
    emp = em1.find(Employee.class, emp.getId());
    Employee emp2 = em2.find(Employee.class, emp.getId());
    Employee direct2 = em2.find(Employee.class, direct.getId());
    Dealer dealer2 = em2.find(Dealer.class, dealer.getId());
    Project project2 = em2.find(Project.class, project.getId());
    em1.getTransaction().begin();
    emp.getManagedEmployees().iterator().next().setFirstName("something");
    emp.getDealers().get(0).setFirstName("Something");
    emp.getProjects().iterator().next().setName("Beta Beta Wiskey");
    em1.getTransaction().commit();
    em2.refresh(emp2);
    try {
        assertEquals("Did not cascade refresh across untriggered lazy", emp2.getManagedEmployees().iterator().next().getFirstName(), "something");
        assertEquals("Cascaded refresh to non cascade refresh entity", emp2.getDealers().get(0).getFirstName(), "Jim");
        assertEquals("Cascade refresh to non cascade refresh", emp2.getProjects().iterator().next().getName(), "Alpha Alpha Zulu");
    } finally {
        try {
            em2.getTransaction().begin();
            em2.remove(emp2);
            em2.remove(direct2);
            em2.remove(dealer2);
            em2.remove(project2);
            em2.getTransaction().commit();
        } catch (Exception e) {
        }
    }
}
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) TestProblemException(org.eclipse.persistence.testing.framework.TestProblemException) ValidationException(org.eclipse.persistence.exceptions.ValidationException) EclipseLinkException(org.eclipse.persistence.exceptions.EclipseLinkException) EntityExistsException(jakarta.persistence.EntityExistsException) OptimisticLockException(jakarta.persistence.OptimisticLockException) QueryException(org.eclipse.persistence.exceptions.QueryException) TransactionRequiredException(jakarta.persistence.TransactionRequiredException) IntegrityException(org.eclipse.persistence.exceptions.IntegrityException) EntityNotFoundException(jakarta.persistence.EntityNotFoundException) SQLException(java.sql.SQLException) RollbackException(jakarta.persistence.RollbackException) NoResultException(jakarta.persistence.NoResultException) PersistenceException(jakarta.persistence.PersistenceException) PersistenceUnitLoadingException(org.eclipse.persistence.exceptions.PersistenceUnitLoadingException) Dealer(org.eclipse.persistence.testing.models.jpa.advanced.Dealer) SuperLargeProject(org.eclipse.persistence.testing.models.jpa.advanced.SuperLargeProject) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)

Example 20 with LargeProject

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

the class EntityManagerJUnitTestSuite method testDeleteAllProjects.

public void testDeleteAllProjects() {
    if ((JUnitTestCase.getServerSession()).getPlatform().isSymfoware()) {
        getServerSession().logMessage("Test testDeleteAllProjects skipped for this platform, " + "Symfoware doesn't support UpdateAll/DeleteAll on multi-table objects (see rfe 298193).");
        return;
    }
    String errorMsg = "";
    SmallProject sp = null;
    LargeProject lp = null;
    HugeProject hp = null;
    SpecialHugeProject slp = null;
    EntityManager em = createEntityManager();
    beginTransaction(em);
    String[] classesToDelete = { "Project", "SmallProject", "LargeProject", "HugeProject", "SpecialHugeProject" };
    try {
        for (int i = 0; i < classesToDelete.length; i++) {
            if (sp == null) {
                sp = new SmallProject();
                em.persist(sp);
            }
            if (lp == null) {
                lp = new LargeProject();
                em.persist(lp);
            }
            if (hp == null) {
                hp = new HugeProject();
                em.persist(hp);
            }
            if (slp == null) {
                slp = new SpecialHugeProject();
                em.persist(slp);
            }
            em.flush();
            String classToDelete = classesToDelete[i];
            em.createQuery("DELETE FROM " + classToDelete + " p").executeUpdate();
            em.clear();
            sp = em.find(SmallProject.class, sp.getId());
            lp = em.find(LargeProject.class, lp.getId());
            hp = em.find(HugeProject.class, hp.getId());
            slp = em.find(SpecialHugeProject.class, slp.getId());
            String unexpectedlyDeleted = "";
            String unexpectedlyNotDeleted = "";
            if (sp == null) {
                if (classToDelete.equals("LargeProject") || classToDelete.equals("HugeProject") || classToDelete.equals("SpecialHugeProject")) {
                    unexpectedlyDeleted += "SmallProject; ";
                }
            } else {
                if (classToDelete.equals("Project") || classToDelete.equals("SmallProject")) {
                    unexpectedlyNotDeleted += "SmallProject; ";
                }
            }
            if (lp == null) {
                if (classToDelete.equals("SmallProject") || classToDelete.equals("HugeProject") || classToDelete.equals("SpecialHugeProject")) {
                    unexpectedlyDeleted += "LargeProject; ";
                }
            } else {
                if (classToDelete.equals("Project") || classToDelete.equals("LargeProject")) {
                    unexpectedlyNotDeleted += "LargeProject; ";
                }
            }
            if (hp == null) {
                if (classToDelete.equals("SmallProject") || classToDelete.equals("SpecialHugeProject")) {
                    unexpectedlyDeleted += "HugeProject; ";
                }
            } else {
                if (classToDelete.equals("Project") || classToDelete.equals("LargeProject") || classToDelete.equals("HugeProject")) {
                    unexpectedlyNotDeleted += "HugeProject; ";
                }
            }
            if (slp == null) {
                if (classToDelete.equals("SmallProject")) {
                    unexpectedlyDeleted += "SpecialHugeProject; ";
                }
            } else {
                if (classToDelete.equals("Project") || classToDelete.equals("LargeProject") || classToDelete.equals("HugeProject") || classToDelete.equals("SpecialHugeProject")) {
                    unexpectedlyNotDeleted += "SpecialHugeProject; ";
                }
            }
            String localErrorMsg = "";
            if (unexpectedlyDeleted.length() > 0) {
                localErrorMsg += "\n\t\tUnexpectedlyDeleted: " + unexpectedlyDeleted;
            }
            if (unexpectedlyNotDeleted.length() > 0) {
                localErrorMsg += "\n\t\tUnexpectedlyNotDeleted: " + unexpectedlyNotDeleted;
            }
            if (localErrorMsg.length() > 0) {
                errorMsg += "\n\tDELETE FROM " + classToDelete + " p" + localErrorMsg;
            }
        }
    } finally {
        closeEntityManagerAndTransaction(em);
    }
    if (errorMsg.length() > 0) {
        fail(errorMsg);
    }
}
Also used : SmallProject(org.eclipse.persistence.testing.models.jpa.advanced.SmallProject) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) HugeProject(org.eclipse.persistence.testing.models.jpa.advanced.HugeProject) SpecialHugeProject(org.eclipse.persistence.testing.models.jpa.advanced.SpecialHugeProject) SpecialHugeProject(org.eclipse.persistence.testing.models.jpa.advanced.SpecialHugeProject) SuperLargeProject(org.eclipse.persistence.testing.models.jpa.advanced.SuperLargeProject) 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