Search in sources :

Example 11 with Equipment

use of org.eclipse.persistence.testing.models.jpa.advanced.Equipment 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();
    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) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Department(org.eclipse.persistence.testing.models.jpa.advanced.Department) Equipment(org.eclipse.persistence.testing.models.jpa.advanced.Equipment) EJBQueryImpl(org.eclipse.persistence.internal.jpa.EJBQueryImpl)

Example 12 with Equipment

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

the class PessimisticLockingExtendedScopeTestSuite method testPESSMISTIC_ES3.

// Entity relationships for which the locked entity contains the foreign key
// will be locked with unidirectional many-to-one mapping(Scenario 1.3)
public void testPESSMISTIC_ES3() throws Exception {
    if (getPlatform().isSQLServer()) {
        warning("This test deadlocks on SQL Server");
        return;
    }
    final Equipment eq = new Equipment();
    final Actor<Equipment> actor = new Actor<>() {

        @Override
        public void setup(EntityManager em) {
            EquipmentCode eqCode = new EquipmentCode();
            eqCode.setCode("A");
            em.persist(eqCode);
            eq.setEquipmentCode(eqCode);
            em.persist(eq);
        }

        @Override
        public Equipment getEntityToLock(EntityManager em1) {
            return em1.find(Equipment.class, eq.getId());
        }

        @Override
        public void modify(EntityManager em2) {
            Equipment eq2 = em2.find(Equipment.class, eq.getId());
            eq2.setEquipmentCode(null);
        }

        @Override
        public void check(EntityManager em1, Equipment lockedEntity) {
            em1.refresh(lockedEntity);
            assertNotNull("other transaction modified row concurrently", lockedEntity.getEquipmentCode());
        }
    };
    testNonrepeatableRead(actor);
}
Also used : EntityManager(jakarta.persistence.EntityManager) Equipment(org.eclipse.persistence.testing.models.jpa.advanced.Equipment) EquipmentCode(org.eclipse.persistence.testing.models.jpa.advanced.EquipmentCode)

Example 13 with Equipment

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

the class Runner1 method run.

@Override
public void run() {
    try {
        EntityManager em = emf.createEntityManager();
        Department dept = em.find(Department.class, deptPK);
        Equipment equip = em.find(Equipment.class, equipPK);
        dept.getEquipment().put(equip.getId(), equip);
        UnitOfWorkImpl uow = ((EntityManagerImpl) em).getActivePersistenceContext(null);
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
        }
        uow.issueSQLbeforeCompletion(true);
        synchronized (this.waitOn) {
            try {
                this.waitOn.notify();
                this.waitOn.wait();
            } catch (InterruptedException e) {
            }
        }
        CacheKey cacheKey = uow.getParent().getParent().getIdentityMapAccessorInstance().getCacheKeyForObject(dept);
        synchronized (cacheKey) {
            cacheKey.notify();
        }
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
        }
        uow.mergeClonesAfterCompletion();
    } catch (Exception ex) {
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) Department(org.eclipse.persistence.testing.models.jpa.advanced.Department) Equipment(org.eclipse.persistence.testing.models.jpa.advanced.Equipment) EntityManagerImpl(org.eclipse.persistence.internal.jpa.EntityManagerImpl) UnitOfWorkImpl(org.eclipse.persistence.internal.sessions.UnitOfWorkImpl) CacheKey(org.eclipse.persistence.internal.identitymaps.CacheKey)

Example 14 with Equipment

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

the class NamedQueryJUnitTest method removeEmployee.

/**
 * Remove {@link Employee} entity including all related entities.
 * Transaction must be opened for provided <code>EntityManager</code> instance.
 * @param em       An <code>EntityManager</code> instance used to remove entities.
 * @param employee An <code>Employee</code> entity to be removed.
 */
private void removeEmployee(EntityManager em, Employee employee) {
    Collection<Equipment> equipmentColl = employee.getDepartment().getEquipment().values();
    em.remove(employee);
    em.remove(employee.getDepartment());
    em.remove(employee.getAddress());
    for (Equipment equipment : equipmentColl) {
        EquipmentCode ec = equipment.getEquipmentCode();
        em.remove(equipment);
        if (ec != null) {
            em.remove(ec);
        }
    }
}
Also used : Equipment(org.eclipse.persistence.testing.models.jpa.advanced.Equipment) EquipmentCode(org.eclipse.persistence.testing.models.jpa.advanced.EquipmentCode)

Example 15 with Equipment

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

the class AdvancedJPAJunitTest method testCreateNewEquipment.

/**
 * Creates some new equipment objects.
 */
public void testCreateNewEquipment() {
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        // Persist some equipment.
        Equipment equip1 = new Equipment();
        equip1.setDescription("Toaster");
        em.persist(equip1);
        Equipment equip2 = new Equipment();
        equip1.setDescription("Bucket");
        em.persist(equip2);
        Equipment equip3 = new Equipment();
        equip1.setDescription("Broom");
        em.persist(equip3);
        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) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Equipment(org.eclipse.persistence.testing.models.jpa.advanced.Equipment)

Aggregations

Equipment (org.eclipse.persistence.testing.models.jpa.advanced.Equipment)15 EntityManager (jakarta.persistence.EntityManager)12 Department (org.eclipse.persistence.testing.models.jpa.advanced.Department)10 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)7 EntityManagerImpl (org.eclipse.persistence.internal.jpa.EntityManagerImpl)4 EquipmentCode (org.eclipse.persistence.testing.models.jpa.advanced.EquipmentCode)4 UnitOfWorkImpl (org.eclipse.persistence.internal.sessions.UnitOfWorkImpl)3 Map (java.util.Map)2 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)2 Address (org.eclipse.persistence.testing.models.jpa.advanced.Address)2 Employee (org.eclipse.persistence.testing.models.jpa.advanced.Employee)2 EmploymentPeriod (org.eclipse.persistence.testing.models.jpa.advanced.EmploymentPeriod)2 FormerEmployment (org.eclipse.persistence.testing.models.jpa.advanced.FormerEmployment)2 LargeProject (org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)2 PhoneNumber (org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber)2 SmallProject (org.eclipse.persistence.testing.models.jpa.advanced.SmallProject)2 SuperLargeProject (org.eclipse.persistence.testing.models.jpa.advanced.SuperLargeProject)2 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)1 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1