Search in sources :

Example 1 with VersionedPerson

use of org.datanucleus.samples.annotations.versioned.VersionedPerson in project tests by datanucleus.

the class EntityManagerFactoryTest method testPersistenceUnitUtilGetIdentifier.

/**
 * Test for emf.getPersistenceUnitUtil.getIdentifier() method
 */
public void testPersistenceUnitUtilGetIdentifier() {
    try {
        PersistenceUnitUtil util = emf.getPersistenceUnitUtil();
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            Person p = new Person(101, "Fred", "Flintstone", "fred.flintstone@jpox.com");
            p.setGlobalNum("First");
            em.persist(p);
            assertTrue(util.getIdentifier(p) instanceof Person.PK);
            VersionedPerson vp = new VersionedPerson(1, 1);
            em.persist(vp);
            Object vpId = util.getIdentifier(vp);
            assertTrue(vpId instanceof Long && ((Long) vpId) == 1);
            tx.rollback();
        } catch (Exception e) {
            LOG.error(">> Exception on persist before serialisation", e);
            fail("Exception on persist : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
    // No cleanup needed
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) PersistenceUnitUtil(javax.persistence.PersistenceUnitUtil) VersionedPerson(org.datanucleus.samples.annotations.versioned.VersionedPerson) VersionedPerson(org.datanucleus.samples.annotations.versioned.VersionedPerson) Person(org.datanucleus.samples.annotations.models.company.Person) PersistenceException(javax.persistence.PersistenceException)

Example 2 with VersionedPerson

use of org.datanucleus.samples.annotations.versioned.VersionedPerson in project tests by datanucleus.

the class EntityManagerTest method testLockOptimisticForceIncrement.

/**
 * Test of lock type OPTIMISTIC_FORCE_INCREMENT
 */
public void testLockOptimisticForceIncrement() {
    try {
        // Persist an object
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            VersionedPerson p = new VersionedPerson(1, 1);
            em.persist(p);
            tx.commit();
            assertEquals(1, p.getVersion());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        emf.getCache().evictAll();
        // Retrieve the object and lock it with OPTIMISTIC_FORCE_INCREMENT
        em = getEM();
        tx = em.getTransaction();
        try {
            tx.begin();
            VersionedPerson p = em.find(VersionedPerson.class, 1);
            em.lock(p, LockModeType.OPTIMISTIC_FORCE_INCREMENT);
            tx.commit();
            assertEquals(2, p.getVersion());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(VersionedPerson.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) VersionedPerson(org.datanucleus.samples.annotations.versioned.VersionedPerson)

Example 3 with VersionedPerson

use of org.datanucleus.samples.annotations.versioned.VersionedPerson in project tests by datanucleus.

the class EntityManagerTest method testVersionedObject.

/**
 * Test of persistence/retrieval of object with version.
 */
public void testVersionedObject() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            VersionedPerson p = new VersionedPerson(1, 1);
            em.persist(p);
            tx.commit();
            assertEquals(1, p.getVersion());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        emf.getCache().evictAll();
        // Merge the same object, but transient
        em = getEM();
        tx = em.getTransaction();
        try {
            tx.begin();
            VersionedPerson p = new VersionedPerson(1, 1);
            p = em.merge(p);
            tx.commit();
            assertEquals(2, p.getVersion());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(VersionedEmployee.class);
        clean(VersionedPerson.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) VersionedPerson(org.datanucleus.samples.annotations.versioned.VersionedPerson)

Aggregations

EntityManager (javax.persistence.EntityManager)3 EntityTransaction (javax.persistence.EntityTransaction)3 VersionedPerson (org.datanucleus.samples.annotations.versioned.VersionedPerson)3 PersistenceException (javax.persistence.PersistenceException)1 PersistenceUnitUtil (javax.persistence.PersistenceUnitUtil)1 Person (org.datanucleus.samples.annotations.models.company.Person)1