Search in sources :

Example 1 with EmploymentPeriod

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

the class JUnitJPQLExamplesTestSuite method aggregateParameterTest.

// Bug5040609 Test case for aggregates as parameters in EJBQL
public void aggregateParameterTest() {
    EntityManager em = createEntityManager();
    ExpressionBuilder builder = new ExpressionBuilder();
    ReportQuery query = new ReportQuery(org.eclipse.persistence.testing.models.jpa.advanced.Employee.class, builder);
    query.returnWithoutReportQueryResult();
    query.addItem("employee", builder);
    org.eclipse.persistence.testing.models.jpa.advanced.EmploymentPeriod period = new EmploymentPeriod();
    Calendar startCalendar = Calendar.getInstance();
    Calendar endCalendar = Calendar.getInstance();
    startCalendar.set(1901, 11, 31, 0, 0, 0);
    endCalendar.set(1995, 0, 12, 0, 0, 0);
    period.setStartDate(new java.sql.Date(startCalendar.getTime().getTime()));
    period.setEndDate(new java.sql.Date(endCalendar.getTime().getTime()));
    Expression exp = builder.get("period").equal(builder.getParameter("period"));
    query.setSelectionCriteria(exp);
    query.addArgument("period", EmploymentPeriod.class);
    Vector args = new Vector();
    args.add(period);
    List expectedResult = (Vector) getServerSession().executeQuery(query, args);
    List result = em.createQuery("SELECT e FROM Employee e WHERE e.period = :period ").setParameter("period", period).getResultList();
    Assert.assertTrue("aggregateParameterTest failed", comparer.compareObjects(expectedResult, result));
}
Also used : EmploymentPeriod(org.eclipse.persistence.testing.models.jpa.advanced.EmploymentPeriod) ReportQuery(org.eclipse.persistence.queries.ReportQuery) Calendar(java.util.Calendar) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) ConstantExpression(org.eclipse.persistence.internal.expressions.ConstantExpression) Expression(org.eclipse.persistence.expressions.Expression) List(java.util.List) EmploymentPeriod(org.eclipse.persistence.testing.models.jpa.advanced.EmploymentPeriod) Vector(java.util.Vector)

Example 2 with EmploymentPeriod

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

the class EntityManagerJUnitTestSuite method internalTestChangeRecordKeepOldValue.

public void internalTestChangeRecordKeepOldValue(boolean addSecondStep) {
    if (isOnServer()) {
        // the test relies upon keeping attached objects between transactions.
        return;
    }
    ServerSession ss = getServerSession();
    ChangeRecordKeepOldValueListener listener = new ChangeRecordKeepOldValueListener();
    ss.getEventManager().addListener(listener);
    EntityManager em = createEntityManager();
    try {
        // setup
        // create original object and referenced objects
        Employee emp = new Employee();
        emp.setFirstName("TestChangeRecordKeepOldValue");
        emp.setLastName("Manager_OriginalName");
        emp.setMale();
        emp.setSalary(35000);
        emp.setPeriod(new EmploymentPeriod(Helper.dateFromYearMonthDate(1993, 0, 1), Helper.dateFromYearMonthDate(1996, 11, 31)));
        emp.setFormerEmployment(new FormerEmployment("Original", new EmploymentPeriod(Helper.dateFromYearMonthDate(1990, 0, 1), Helper.dateFromYearMonthDate(1993, 11, 31))));
        Address address = new Address();
        address.setCountry("Original");
        address.setProvince("Original");
        address.setCity("Original");
        address.setStreet("Original");
        address.setPostalCode("Original");
        emp.setAddress(address);
        Department dep = new Department();
        dep.setName("Original");
        emp.setDepartment(dep);
        emp.addResponsibility("Original_1");
        emp.addResponsibility("Original_2");
        PhoneNumber phone1 = new PhoneNumber("Original_1", "111", "1111111");
        emp.addPhoneNumber(phone1);
        PhoneNumber phone2 = new PhoneNumber("Original_2", "222", "2222222");
        emp.addPhoneNumber(phone2);
        emp.setNormalHours(new Time[] { Helper.timeFromHourMinuteSecond(9, 0, 0), Helper.timeFromHourMinuteSecond(17, 0, 0) });
        Employee emp1 = new Employee();
        emp1.setFirstName("TestChangeRecordKeepOldValue");
        emp1.setLastName("Original_1");
        emp.addManagedEmployee(emp1);
        Employee emp2 = new Employee();
        emp2.setFirstName("TestChangeRecordKeepOldValue");
        emp2.setLastName("Original_2");
        emp.addManagedEmployee(emp2);
        Project proj1 = new SmallProject();
        proj1.setName("Original_1");
        emp.addProject(proj1);
        Project proj2 = new LargeProject();
        proj2.setName("Original_2");
        emp.addProject(proj2);
        Project proj3 = new LargeProject();
        proj3.setName("Original_3");
        proj3.setTeamLeader(emp);
        emp1.addProject(proj3);
        emp2.addProject(proj3);
        // persist original object and all referenced objects
        beginTransaction(em);
        Equipment equipment1 = new Equipment();
        equipment1.setDescription("Original_1");
        // persist is not cascaded for department.equipment - have to explicitly persist it.
        em.persist(equipment1);
        dep.addEquipment(equipment1);
        Equipment equipment2 = new Equipment();
        equipment2.setDescription("Original_2");
        // persist is not cascaded for department.equipment - have to explicitly persist it.
        // moreover, equipment.id is used as a key in dep.equipment Map, therefore equipmet
        // should be persisted before been added to dep.
        em.persist(equipment2);
        dep.addEquipment(equipment2);
        // persist is not cascaded for employee.department - have to explicitly persist it.
        // moreover, equipment.id is used as a key in dep.equipment Map, therefore equipmet
        // should be persisted before been added to dep.
        em.persist(dep);
        em.persist(emp);
        commitTransaction(em);
        // backup original object and all referenced objects
        CopyGroup copyGroupBackup = new CopyGroup();
        copyGroupBackup.cascadeAllParts();
        ss.copy(emp, copyGroupBackup);
        // emp references (directly or through other objects) all the created objects, therefore all of them are copied.
        Map backupMap = copyGroupBackup.getCopies();
        // verify that backup objects are identical to originals
        Iterator<Map.Entry> it = backupMap.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry entry = it.next();
            Object original = entry.getKey();
            ClassDescriptor descriptor = ss.getDescriptor(original);
            if (!descriptor.isAggregateDescriptor()) {
                Object backup = entry.getValue();
                if (original == backup) {
                    fail("Test problem: backup failed: original == backup: " + original);
                }
                if (!ss.compareObjects(original, backup)) {
                    fail("Test problem: backup failed: compareObjects(original, backup) == false: " + original + "; " + backup);
                }
            }
        }
        // change original object
        beginTransaction(em);
        // DirectToField
        emp.setLastName("Manager_NewName");
        // DirectToField
        emp.setSalary(100);
        // Aggregate
        emp.getPeriod().setStartDate(Helper.dateFromYearMonthDate(2000, 0, 1));
        emp.getPeriod().setEndDate(Helper.dateFromYearMonthDate(2002, 0, 1));
        // Aggregate
        // emp.setFormerEmployment(new FormerEmployment("New", new EmploymentPeriod(Helper.dateFromYearMonthDate(1989, 0, 1), Helper.dateFromYearMonthDate(1992, 11, 31))));
        emp.getFormerEmployment().setFormerCompany("New");
        emp.getFormerEmployment().getPeriod().setStartDate(Helper.dateFromYearMonthDate(1989, 0, 1));
        emp.getFormerEmployment().getPeriod().setStartDate(Helper.dateFromYearMonthDate(1992, 11, 31));
        // Transformation
        emp.setStartTime(Helper.timeFromHourMinuteSecond(10, 0, 0));
        // DirectCollection
        emp.removeResponsibility("Original_1");
        emp.addResponsibility("New_1");
        // Private Collection 1 to many
        PhoneNumber phone1New = new PhoneNumber("New_1", "111", "1111111");
        emp.addPhoneNumber(phone1New);
        emp.removePhoneNumber(phone1);
        // Collection 1 to many
        emp.removeManagedEmployee(emp1);
        emp1.setManager(null);
        em.remove(emp1);
        Employee emp1New = new Employee();
        emp1New.setFirstName("TestChangeRecordKeepOldValue");
        emp1New.setLastName("New_1");
        emp1New.addProject(proj3);
        emp.addManagedEmployee(emp1New);
        // Collection many to many
        emp.removeProject(proj1);
        em.remove(proj1);
        Project proj1New = new LargeProject();
        proj1New.setName("New_1");
        emp.addProject(proj1New);
        // ObjectReference
        proj3.setTeamLeader(null);
        // ObjectReference
        Address addressNew = new Address();
        addressNew.setCountry("New");
        addressNew.setProvince("New");
        addressNew.setCity("New");
        addressNew.setStreet("New");
        addressNew.setPostalCode("New");
        emp.setAddress(addressNew);
        em.remove(address);
        // Map 1 to many
        dep.getEquipment().remove(equipment1.getId());
        Equipment equipment1New = new Equipment();
        equipment1New.setDescription("New_1");
        em.persist(equipment1New);
        dep.addEquipment(equipment1New);
        // additional change typically overrides existing aggregate or collection with the new one, then alters aggregate or adds to collection.
        if (addSecondStep) {
            // Aggregate
            emp.setPeriod(new EmploymentPeriod());
            emp.getPeriod().setStartDate(Helper.dateFromYearMonthDate(2001, 0, 1));
            emp.getPeriod().setEndDate(Helper.dateFromYearMonthDate(2003, 0, 1));
            // Aggregate
            emp.setFormerEmployment(new FormerEmployment("New_New", new EmploymentPeriod(Helper.dateFromYearMonthDate(1988, 0, 1), Helper.dateFromYearMonthDate(1991, 11, 31))));
            emp.getFormerEmployment().setPeriod(new EmploymentPeriod());
            emp.getFormerEmployment().getPeriod().setStartDate(Helper.dateFromYearMonthDate(1987, 0, 1));
            emp.getFormerEmployment().getPeriod().setEndDate(Helper.dateFromYearMonthDate(1990, 0, 1));
            // Transformation
            emp.setEndTime(Helper.timeFromHourMinuteSecond(18, 0, 0));
            // DirectCollection
            emp.setResponsibilities(new ArrayList());
            emp.addResponsibility("New_New_1");
            // Private Collection 1 to many
            emp.setPhoneNumbers(new HashSet());
            PhoneNumber phone1NewNew = new PhoneNumber("New_New_1", "111", "1111111");
            emp.addPhoneNumber(phone1NewNew);
            // Collection 1 to many
            emp1New.setManager(null);
            emp2.setManager(null);
            emp.setManagedEmployees(new Vector());
            Employee emp1NewNew = new Employee();
            emp1NewNew.setFirstName("TestChangeRecordKeepOldValue");
            emp1NewNew.setLastName("New_New_1");
            emp1NewNew.addProject(proj3);
            emp.addManagedEmployee(emp1NewNew);
            em.remove(emp1New);
            em.remove(emp2);
            // Collection many to many
            emp.setProjects(new ArrayList());
            Project proj1NewNew = new LargeProject();
            proj1NewNew.setName("New_New_1");
            emp.addProject(proj1NewNew);
            em.remove(proj1New);
            em.remove(proj2);
            // ObjectReference
            Address addressNewNew = new Address();
            addressNewNew.setCountry("New_New");
            addressNewNew.setProvince("New_New");
            addressNewNew.setCity("New_New");
            addressNewNew.setStreet("New_New");
            addressNewNew.setPostalCode("New_New");
            emp.setAddress(addressNewNew);
            em.remove(addressNew);
            // Map 1 to many
            // We are about to override equipment map.
            // It's a private OneToMany mapping so normally all the
            // members would be removed automatically.
            // However in theis case we have explicitly persisted the the new member added in thhe current transaction
            // (because Equipment's id is used as its key in the Department.equipment Map)
            // therefore we have to explicitly em.remove it.
            em.remove(equipment1New);
            dep.setEquipment(new HashMap());
            Equipment equipment1NewNew = new Equipment();
            equipment1NewNew.setDescription("New_New_1");
            em.persist(equipment1NewNew);
            dep.addEquipment(equipment1NewNew);
        }
        commitTransaction(em);
        // backup updated objects
        CopyGroup copyGroupUpdated = new CopyGroup();
        copyGroupUpdated.cascadeAllParts();
        ss.copy(emp, copyGroupUpdated);
        // copies of the updated objects will be altered to contain old values.
        // if altering the test, make sure that emp still references (directly or through other objects) all the updated objects, so that all of them are copied.
        Map oldValueMap = copyGroupUpdated.getCopies();
        // using ChangeRecords bring back the original state of the object
        Iterator<Map.Entry<Object, org.eclipse.persistence.internal.sessions.ObjectChangeSet>> itChangeSets = ((org.eclipse.persistence.internal.sessions.UnitOfWorkChangeSet) listener.uowChangeSet).getCloneToObjectChangeSet().entrySet().iterator();
        while (itChangeSets.hasNext()) {
            Map.Entry<Object, org.eclipse.persistence.internal.sessions.ObjectChangeSet> entry = itChangeSets.next();
            Object object = entry.getKey();
            ClassDescriptor descriptor = ss.getDescriptor(object);
            if (!descriptor.isAggregateDescriptor()) {
                org.eclipse.persistence.internal.sessions.ObjectChangeSet changeSet = entry.getValue();
                if (!changeSet.shouldBeDeleted() && !changeSet.isNew()) {
                    List<ChangeRecord> changes = changeSet.getChanges();
                    if (changes != null && !changes.isEmpty()) {
                        Object oldValueObject = oldValueMap.get(object);
                        for (ChangeRecord changeRecord : changeSet.getChanges()) {
                            Object oldValue = changeRecord.getOldValue();
                            DatabaseMapping mapping = ((org.eclipse.persistence.internal.sessions.ChangeRecord) changeRecord).getMapping();
                            mapping.setRealAttributeValueInObject(oldValueObject, oldValue);
                        }
                    }
                }
            }
        }
        String errorMsgOldValues = "";
        // now compare oldValue objects with corresponding backup objects
        itChangeSets = ((org.eclipse.persistence.internal.sessions.UnitOfWorkChangeSet) listener.uowChangeSet).getCloneToObjectChangeSet().entrySet().iterator();
        while (itChangeSets.hasNext()) {
            Map.Entry<Object, org.eclipse.persistence.internal.sessions.ObjectChangeSet> entry = itChangeSets.next();
            Object object = entry.getKey();
            ClassDescriptor descriptor = ss.getDescriptor(object);
            if (!descriptor.isAggregateDescriptor()) {
                ObjectChangeSet changeSet = entry.getValue();
                if (!changeSet.shouldBeDeleted() && !changeSet.isNew()) {
                    List<ChangeRecord> changes = changeSet.getChanges();
                    if (changes != null && !changes.isEmpty()) {
                        Object oldValueObject = oldValueMap.get(object);
                        Object backupObject = backupMap.get(object);
                        // compare oldValue with backup object
                        if (!ss.compareObjects(oldValueObject, backupObject)) {
                            errorMsgOldValues += '\t' + object.toString() + '\n';
                        }
                    }
                }
            }
        }
        // set of attached to em objects
        Set updatedObjects = new HashSet();
        for (Object object : oldValueMap.keySet()) {
            ClassDescriptor descriptor = ss.getDescriptor(object);
            if (!descriptor.isAggregateDescriptor()) {
                updatedObjects.add(object);
            }
        }
        // verify that the objects were correctly written to the db
        String errorMsgDb = "";
        clearCache();
        Map updatedToReadBack = new HashMap();
        EntityManager em2 = createEntityManager();
        beginTransaction(em2);
        UnitOfWork uow2 = ((EntityManagerImpl) em2.getDelegate()).getActivePersistenceContext(null);
        // before comparison clear all read-only mappings that we didn't set in the attached to em objects.
        for (Object object : updatedObjects) {
            Object readBack = uow2.readObject(object);
            if (readBack instanceof Project) {
                ((Project) readBack).getTeamMembers().clear();
            } else if (readBack instanceof Address) {
                ((Address) readBack).getEmployees().clear();
            } else if (readBack instanceof PhoneNumber) {
                ((PhoneNumber) readBack).setId(null);
            } else if (readBack instanceof Department) {
                ((Department) readBack).getEmployees().clear();
            }
            updatedToReadBack.put(object, readBack);
        }
        // now compare object attached to em with the object read from the db
        for (Object entryObject : updatedToReadBack.entrySet()) {
            Map.Entry entry = (Map.Entry) entryObject;
            Object object = entry.getKey();
            Object readBack = entry.getValue();
            if (!ss.compareObjects(object, readBack)) {
                errorMsgDb += '\t' + object.toString() + '\n';
            }
        }
        rollbackTransaction(em2);
        // clean up
        beginTransaction(em);
        Set objectsToRemove = new HashSet();
        // remove all dependencies and add cache objects to be directly removed.
        for (Object object : updatedObjects) {
            if (object instanceof Employee) {
                ((Employee) object).getManagedEmployees().clear();
                ((Employee) object).setManager(null);
                ((Employee) object).setAddress((Address) null);
                ((Employee) object).getProjects().clear();
                ((Employee) object).setDepartment(null);
                objectsToRemove.add(object);
            } else if (object instanceof Project) {
                ((Project) object).setTeamLeader(null);
                objectsToRemove.add(object);
            } else if (object instanceof Address || object instanceof Department) {
                objectsToRemove.add(object);
            }
        }
        // remove objects that should be removed directly.
        for (Object object : objectsToRemove) {
            em.remove(object);
        }
        commitTransaction(em);
        String errorMsg = "";
        if (errorMsgOldValues.length() > 0) {
            errorMsgOldValues = "Some old values and back up objects are not equal:\n" + errorMsgOldValues;
            errorMsg += errorMsgOldValues;
        }
        if (errorMsgDb.length() > 0) {
            errorMsgDb = "\nSome values were incorrectly written into the db:\n" + errorMsgDb;
            errorMsg += errorMsgDb;
        }
        if (errorMsg.length() > 0) {
            fail(errorMsg);
        }
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
        ss.getEventManager().removeListener(listener);
    }
}
Also used : SmallProject(org.eclipse.persistence.testing.models.jpa.advanced.SmallProject) UnitOfWork(org.eclipse.persistence.sessions.UnitOfWork) RepeatableWriteUnitOfWork(org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork) ObjectChangeSet(org.eclipse.persistence.internal.sessions.ObjectChangeSet) HashSet(java.util.HashSet) ResultSet(java.sql.ResultSet) Set(java.util.Set) UnitOfWorkChangeSet(org.eclipse.persistence.sessions.changesets.UnitOfWorkChangeSet) Address(org.eclipse.persistence.testing.models.jpa.advanced.Address) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) HashMap(java.util.HashMap) EntityManagerImpl(org.eclipse.persistence.internal.jpa.EntityManagerImpl) FormerEmployment(org.eclipse.persistence.testing.models.jpa.advanced.FormerEmployment) UnitOfWorkChangeSet(org.eclipse.persistence.sessions.changesets.UnitOfWorkChangeSet) ArrayList(java.util.ArrayList) Department(org.eclipse.persistence.testing.models.jpa.advanced.Department) ObjectChangeSet(org.eclipse.persistence.internal.sessions.ObjectChangeSet) Vector(java.util.Vector) HashSet(java.util.HashSet) EmploymentPeriod(org.eclipse.persistence.testing.models.jpa.advanced.EmploymentPeriod) ServerSession(org.eclipse.persistence.sessions.server.ServerSession) ObjectChangeSet(org.eclipse.persistence.internal.sessions.ObjectChangeSet) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) 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) Equipment(org.eclipse.persistence.testing.models.jpa.advanced.Equipment) CopyGroup(org.eclipse.persistence.sessions.CopyGroup) PhoneNumber(org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber) Map(java.util.Map) HashMap(java.util.HashMap) ChangeRecord(org.eclipse.persistence.sessions.changesets.ChangeRecord) SuperLargeProject(org.eclipse.persistence.testing.models.jpa.advanced.SuperLargeProject) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)

Example 3 with EmploymentPeriod

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

the class EntityManagerJUnitTestSuite method testSelectEmbeddable.

// Bug 324406 - Wrong Index in ReportItem when @Embeddable Objects are used in ReportQuery
public void testSelectEmbeddable() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    Employee emp = new Employee();
    emp.setFirstName("Robin");
    emp.setLastName("Van Persie");
    EmploymentPeriod period = new EmploymentPeriod();
    ;
    period.setStartDate(new Date((new GregorianCalendar(2009, 1, 1)).getTimeInMillis()));
    period.setEndDate(new Date((new GregorianCalendar(2010, 1, 1)).getTimeInMillis()));
    emp.setPeriod(period);
    em.persist(emp);
    em.flush();
    em.clear();
    clearCache();
    EmployeeHolder results = (EmployeeHolder) em.createQuery("select new org.eclipse.persistence.testing.models.jpa.advanced.EmployeeHolder(e.id, e.period, e.firstName) from Employee e where e.id = :id").setParameter("id", emp.getId()).getSingleResult();
    assertTrue("Incorrect id", emp.getId().equals(results.getId()));
    assertTrue("Incorrect period start date", emp.getPeriod().getStartDate().equals(results.getPeriod().getStartDate()));
    assertTrue("Incorrect period end date", emp.getPeriod().getEndDate().equals(results.getPeriod().getEndDate()));
    assertTrue("Incorrect name", emp.getFirstName().equals(results.getName()));
    rollbackTransaction(em);
}
Also used : EmploymentPeriod(org.eclipse.persistence.testing.models.jpa.advanced.EmploymentPeriod) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) EmployeeHolder(org.eclipse.persistence.testing.models.jpa.advanced.EmployeeHolder) GregorianCalendar(java.util.GregorianCalendar) Date(java.sql.Date)

Example 4 with EmploymentPeriod

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

the class EntityManagerJUnitTestSuite method testCloneEmbeddable.

// bug 239505 - Ensure our Embeddables succeed a call to clone
public void testCloneEmbeddable() {
    EmployeePopulator populator = new EmployeePopulator();
    EmploymentPeriod period = populator.employmentPeriodExample1();
    try {
        period.clone();
    } catch (Exception e) {
        fail("Exception thrown when trying to clone an Embeddable: " + e.toString());
    }
}
Also used : EmploymentPeriod(org.eclipse.persistence.testing.models.jpa.advanced.EmploymentPeriod) EmployeePopulator(org.eclipse.persistence.testing.models.jpa.advanced.EmployeePopulator) 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)

Example 5 with EmploymentPeriod

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

the class EntityManagerJUnitTestSuite method testSetNewNestedAggregate.

// Bug 307433 - Regression in Auditing Support when using defaults.
public void testSetNewNestedAggregate() {
    // setup
    EntityManager em = createEntityManager();
    Employee emp = new Employee();
    emp.setFormerEmployment(new FormerEmployment("A", new EmploymentPeriod(Helper.dateFromYearMonthDate(1988, 0, 1), Helper.dateFromYearMonthDate(1991, 11, 31))));
    beginTransaction(em);
    em.persist(emp);
    commitTransaction(em);
    int id = emp.getId();
    // test
    beginTransaction(em);
    if (isOnServer() && isJTA()) {
        emp = em.find(Employee.class, id);
    }
    emp.setFormerEmployment(new FormerEmployment("B", new EmploymentPeriod(Helper.dateFromYearMonthDate(1987, 0, 1), Helper.dateFromYearMonthDate(1990, 11, 31))));
    commitTransaction(em);
    closeEntityManager(em);
    // verify
    clearCache();
    em = createEntityManager();
    beginTransaction(em);
    Employee empRead = em.find(Employee.class, id);
    // clean-up
    em.remove(empRead);
    commitTransaction(em);
    closeEntityManager(em);
    assertTrue("Wrong emp.getFormerEmployment().getPeriod() inserted into db", emp.getFormerEmployment().getPeriod().equals(empRead.getFormerEmployment().getPeriod()));
}
Also used : EmploymentPeriod(org.eclipse.persistence.testing.models.jpa.advanced.EmploymentPeriod) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) FormerEmployment(org.eclipse.persistence.testing.models.jpa.advanced.FormerEmployment)

Aggregations

EmploymentPeriod (org.eclipse.persistence.testing.models.jpa.advanced.EmploymentPeriod)14 EntityManager (jakarta.persistence.EntityManager)12 Employee (org.eclipse.persistence.testing.models.jpa.advanced.Employee)12 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)10 FormerEmployment (org.eclipse.persistence.testing.models.jpa.advanced.FormerEmployment)4 Date (java.sql.Date)2 Vector (java.util.Vector)2 Expression (org.eclipse.persistence.expressions.Expression)2 ExpressionBuilder (org.eclipse.persistence.expressions.ExpressionBuilder)2 ReportQuery (org.eclipse.persistence.queries.ReportQuery)2 CopyGroup (org.eclipse.persistence.sessions.CopyGroup)2 Address (org.eclipse.persistence.testing.models.jpa.advanced.Address)2 EntityExistsException (jakarta.persistence.EntityExistsException)1 EntityNotFoundException (jakarta.persistence.EntityNotFoundException)1 NoResultException (jakarta.persistence.NoResultException)1 OptimisticLockException (jakarta.persistence.OptimisticLockException)1 PersistenceException (jakarta.persistence.PersistenceException)1 Query (jakarta.persistence.Query)1 RollbackException (jakarta.persistence.RollbackException)1 TransactionRequiredException (jakarta.persistence.TransactionRequiredException)1