Search in sources :

Example 6 with Department

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

the class AdvancedJPAJunitTest method testRemoveDepartmentWithPrivateOwnedEquipment.

/**
 * Tests a @PrivateOwned @OneToMany mapping.
 */
public void testRemoveDepartmentWithPrivateOwnedEquipment() {
    EntityManager em = createEntityManager("fieldaccess");
    beginTransaction(em);
    try {
        Department department = em.find(Department.class, deptId);
        if (department == null) {
            fail("Department with id=" + deptId + ", was not found.");
        } else {
            Collection<Equipment> equipment = department.getEquipment().values();
            if (equipment.isEmpty()) {
                fail("Department with id=" + deptId + ", did not have any equipment.");
            } else {
                em.remove(department);
                commitTransaction(em);
                assertNull("Department itself was not removed.", em.find(Department.class, deptId));
                for (Equipment e : equipment) {
                    assertNull("New equipment was not deleted.", em.find(Equipment.class, e.getId()));
                }
            }
        }
    } catch (RuntimeException e) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
        // Re-throw exception to ensure stacktrace appears in test result.
        throw e;
    }
    closeEntityManager(em);
}
Also used : EntityManager(jakarta.persistence.EntityManager) Department(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Department) Equipment(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Equipment)

Example 7 with Department

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

the class EntityManagerJUnitTestSuite method testOneToManyDefaultJoinTableName.

// gf 1217 - Ensure join table defaults correctly when 'mappedby' not specified
public void testOneToManyDefaultJoinTableName() {
    Department dept = new Department();
    Employee manager = new Employee();
    dept.addManager(manager);
    EntityManager em = createEntityManager();
    try {
        beginTransaction(em);
        em.persist(dept);
        commitTransaction(em);
    } catch (RuntimeException e) {
        throw e;
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
}
Also used : Department(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Department) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Employee)

Example 8 with Department

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

the class AdvancedJPAJunitTest method testAddNewEquipmentToDepartment.

/**
 * Tests adding objects to a 1-M mapping that uses a map.
 */
public void testAddNewEquipmentToDepartment() {
    EntityManager em = createEntityManager("fieldaccess");
    beginTransaction(em);
    try {
        EJBQueryImpl query = (EJBQueryImpl) em.createNamedQuery("findAllSQLEquipment");
        Collection<Equipment> equipment = query.getResultCollection();
        if (equipment.isEmpty()) {
            fail("No Equipment was found. testCreateNewEquipment should have created new equipment and should have run before this test.");
        } else {
            Department department = new Department();
            department.setName("Department with equipment");
            for (Equipment e : equipment) {
                department.addEquipment(e);
            }
            em.persist(department);
            deptId = department.getId();
            commitTransaction(em);
        }
    } catch (RuntimeException e) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
        // Re-throw exception to ensure stacktrace appears in test result.
        throw e;
    }
    closeEntityManager(em);
}
Also used : EntityManager(jakarta.persistence.EntityManager) Department(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Department) Equipment(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Equipment) EJBQueryImpl(org.eclipse.persistence.internal.jpa.EJBQueryImpl)

Aggregations

Department (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Department)8 EntityManager (jakarta.persistence.EntityManager)6 Employee (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Employee)6 Address (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Address)4 Equipment (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Equipment)4 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)3 PhoneNumber (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.PhoneNumber)3 Query (jakarta.persistence.Query)2 SessionLogEntry (org.eclipse.persistence.logging.SessionLogEntry)2 EmploymentPeriod (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.EmploymentPeriod)2 FormerEmployment (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.FormerEmployment)2 LargeProject (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.LargeProject)2 Project (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Project)2 SmallProject (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.SmallProject)2 SuperLargeProject (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.SuperLargeProject)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1