Search in sources :

Example 1 with Address

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

the class OptimisticLockForceIncrementTestSuite method testVersionIncrementMultipleEntities.

public void testVersionIncrementMultipleEntities() {
    QuerySQLTracker counter = null;
    try {
        counter = new QuerySQLTracker(getServerSession());
        EntityManager em = getEntityManagerFactory().createEntityManager();
        em.getTransaction().begin();
        List<Address> addresses = em.createQuery("select a from Address a").getResultList();
        counter.getSqlStatements().clear();
        assertNotNull("Null query results returned", addresses);
        assertNotSame("No query results returned", addresses.size(), 0);
        Address entity1 = addresses.get(0);
        Address entity2 = addresses.get(1);
        assertNotNull("Entity 1: Address cannot be null", entity1);
        assertNotNull("Entity 2: Address cannot be null", entity2);
        int startVersion1 = entity1.getVersion();
        int startVersion2 = entity2.getVersion();
        em.lock(entity1, LockModeType.OPTIMISTIC_FORCE_INCREMENT);
        // update entity 2
        entity2.setCity("Kamloops");
        entity2.setProvince("BC");
        entity2.setCountry("Canada");
        em.flush();
        // update entity 1
        entity1.setCity("New Glasgow");
        entity1.setProvince("NS");
        entity1.setCountry("Canada");
        em.flush();
        int expectedVersion1 = (startVersion1 + 1);
        int actualVersion1 = entity1.getVersion();
        int expectedVersion2 = (startVersion2 + 1);
        int actualVersion2 = entity2.getVersion();
        em.getTransaction().rollback();
        em.close();
        assertEquals("Entity 1: Version number incremented incorrectly: ", expectedVersion1, actualVersion1);
        assertEquals("Entity 2: Version number incremented incorrectly: ", expectedVersion2, actualVersion2);
        // 3 SQL update statements expected
        assertEquals("3 SQL update statement execution(s)", 3, countNumberOfUpdateStatements(counter));
    } finally {
        if (counter != null) {
            counter.remove();
        }
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) Address(org.eclipse.persistence.testing.models.jpa.advanced.Address) QuerySQLTracker(org.eclipse.persistence.testing.framework.QuerySQLTracker)

Example 2 with Address

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

the class OptimisticLockForceIncrementTestSuite method testVersionIncrementNoChanges.

public void testVersionIncrementNoChanges() {
    if (usesSOP()) {
        // extra version increment.
        return;
    }
    QuerySQLTracker counter = null;
    try {
        counter = new QuerySQLTracker(getServerSession());
        EntityManager em = getEntityManagerFactory().createEntityManager();
        em.getTransaction().begin();
        List<Address> addresses = em.createQuery("select a from Address a").getResultList();
        assertNotNull("Null query results returned", addresses);
        assertNotSame("No query results returned", addresses.size(), 0);
        Address entity = addresses.get(0);
        assertNotNull("Entity: Address cannot be null", entity);
        counter.getSqlStatements().clear();
        int startVersion = entity.getVersion();
        em.lock(entity, LockModeType.OPTIMISTIC_FORCE_INCREMENT);
        em.flush();
        em.getTransaction().commit();
        int expectedVersion = (startVersion + 1);
        int actualVersion = entity.getVersion();
        em.close();
        assertEquals("Version number incremented incorrectly: ", expectedVersion, actualVersion);
        // SQL statements expected - 1 x update
        assertEquals("1 SQL update statement execution(s)", 1, countNumberOfUpdateStatements(counter));
    } finally {
        if (counter != null) {
            counter.remove();
        }
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) Address(org.eclipse.persistence.testing.models.jpa.advanced.Address) QuerySQLTracker(org.eclipse.persistence.testing.framework.QuerySQLTracker)

Example 3 with Address

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

the class OptimisticLockForceIncrementTestSuite method testVersionIncrementPromoteLock.

public void testVersionIncrementPromoteLock() {
    QuerySQLTracker counter = null;
    try {
        counter = new QuerySQLTracker(getServerSession());
        EntityManager em = getEntityManagerFactory().createEntityManager();
        em.getTransaction().begin();
        List<Address> addresses = em.createQuery("select a from Address a").getResultList();
        counter.getSqlStatements().clear();
        assertNotNull("Null query results returned", addresses);
        assertNotSame("No query results returned", addresses.size(), 0);
        Address entity = addresses.get(0);
        assertNotNull("Entity: Address cannot be null", entity);
        int startVersion = entity.getVersion();
        em.lock(entity, LockModeType.OPTIMISTIC);
        entity.setCity("Banff");
        entity.setProvince("AB");
        entity.setCountry("Canada");
        em.flush();
        em.lock(entity, LockModeType.OPTIMISTIC_FORCE_INCREMENT);
        entity.setCity("London");
        entity.setProvince("ON");
        entity.setCountry("Canada");
        em.getTransaction().commit();
        int expectedVersion = (startVersion + 1);
        int actualVersion = entity.getVersion();
        em.close();
        assertEquals("Version number incremented incorrectly: ", expectedVersion, actualVersion);
        // SQL statements expected
        assertEquals("2 SQL update statement execution(s)", 2, countNumberOfUpdateStatements(counter));
    } finally {
        if (counter != null) {
            counter.remove();
        }
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) Address(org.eclipse.persistence.testing.models.jpa.advanced.Address) QuerySQLTracker(org.eclipse.persistence.testing.framework.QuerySQLTracker)

Example 4 with Address

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

the class OptimisticLockForceIncrementTestSuite method testVersionIncrementPreFlushChanges.

public void testVersionIncrementPreFlushChanges() {
    if (usesSOP()) {
        // extra version increment.
        return;
    }
    QuerySQLTracker counter = null;
    try {
        counter = new QuerySQLTracker(getServerSession());
        EntityManager em = getEntityManagerFactory().createEntityManager();
        em.getTransaction().begin();
        List<Address> addresses = em.createQuery("select a from Address a").getResultList();
        counter.getSqlStatements().clear();
        assertNotNull("Null query results returned", addresses);
        assertNotSame("No query results returned", addresses.size(), 0);
        Address entity = addresses.get(0);
        assertNotNull("Entity: Address cannot be null", entity);
        int startVersion = entity.getVersion();
        em.lock(entity, LockModeType.OPTIMISTIC_FORCE_INCREMENT);
        entity.setCity("Vancouver");
        entity.setProvince("BC");
        entity.setCountry("Canada");
        em.flush();
        em.getTransaction().commit();
        int expectedVersion = (startVersion + 1);
        int actualVersion = entity.getVersion();
        em.close();
        assertEquals("Version number incremented incorrectly: ", expectedVersion, actualVersion);
        assertEquals("Entity's name should be changed", "Vancouver", entity.getCity());
        // SQL statements expected - 1 x update
        assertEquals("1 SQL update statement execution(s)", 1, countNumberOfUpdateStatements(counter));
    } finally {
        if (counter != null) {
            counter.remove();
        }
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) Address(org.eclipse.persistence.testing.models.jpa.advanced.Address) QuerySQLTracker(org.eclipse.persistence.testing.framework.QuerySQLTracker)

Example 5 with Address

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

the class PessimisticLockEntityRefreshTestSuite method doFindWithLockModes.

/**
 * Test utility method
 * @param lockMode1 - the lock mode for the first find()
 * @param lockMode2 - the lock mode for the second find()
 */
public void doFindWithLockModes(LockModeType lockMode1, LockModeType lockMode2) {
    if (!isSelectForUpateSupported()) {
        return;
    }
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        Address entity = new Address("Wellington St.", "Ottawa", "ON", "Canada", "K1A0A6");
        em.persist(entity);
        em.flush();
        int entityId = entity.getID();
        entity = em.find(Address.class, entityId, lockMode1);
        assertNotNull("Test data: entity should not be null", entity);
        entity.setCountry("changed");
        Address entityRetrievedAgain = em.find(Address.class, entityId, lockMode2);
        assertNotNull("Test data: entity retrieved again should not be null", entityRetrievedAgain);
        assertTrue("Same entity instance should be returned from find", entity == entityRetrievedAgain);
        assertEquals("Field value is expected to be the same", "changed", entityRetrievedAgain.getCountry());
        assertEquals("Version field is expected to be the same", entity.getVersion(), entityRetrievedAgain.getVersion());
    } catch (RuntimeException ex) {
        throw ex;
    } finally {
        closeEntityManagerAndTransaction(em);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) Address(org.eclipse.persistence.testing.models.jpa.advanced.Address)

Aggregations

Address (org.eclipse.persistence.testing.models.jpa.advanced.Address)79 EntityManager (jakarta.persistence.EntityManager)76 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)53 Employee (org.eclipse.persistence.testing.models.jpa.advanced.Employee)46 PhoneNumber (org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber)13 Query (jakarta.persistence.Query)12 QuerySQLTracker (org.eclipse.persistence.testing.framework.QuerySQLTracker)12 ArrayList (java.util.ArrayList)9 List (java.util.List)9 HashMap (java.util.HashMap)7 TypedQuery (jakarta.persistence.TypedQuery)6 ReadAllQuery (org.eclipse.persistence.queries.ReadAllQuery)6 Department (org.eclipse.persistence.testing.models.jpa.advanced.Department)6 Test (org.junit.Test)6 EntityExistsException (jakarta.persistence.EntityExistsException)5 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)5 Map (java.util.Map)5 DatabaseQuery (org.eclipse.persistence.queries.DatabaseQuery)5 UnitOfWork (org.eclipse.persistence.sessions.UnitOfWork)5 PersistenceException (jakarta.persistence.PersistenceException)4