Search in sources :

Example 1 with CustomUUIDHolder

use of org.datanucleus.samples.annotations.valuegenerator.CustomUUIDHolder in project tests by datanucleus.

the class ValueGeneratorTest method testUUID.

public void testUUID() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        String uid = null;
        try {
            tx.begin();
            CustomUUIDHolder holder = new CustomUUIDHolder();
            holder.setNameField("First holder");
            em.persist(holder);
            tx.commit();
            uid = holder.getUid();
            assertNotNull(uid);
            try {
                UUID.fromString(uid);
            } catch (IllegalArgumentException iae) {
                fail("The generated uid was not a real UUID : " + iae.getMessage());
            }
        } catch (Exception e) {
            LOG.error(">> Exception thrown during persist when using ValueGenerator", e);
            fail("Failure on persist with ValueGenerator : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        if (emf.getCache() != null) {
            emf.getCache().evictAll();
        }
        em = getEM();
        tx = em.getTransaction();
        try {
            tx.begin();
            CustomUUIDHolder h1 = em.find(CustomUUIDHolder.class, uid);
            assertNotNull(h1);
            assertEquals("First holder", h1.getName());
            assertEquals(uid, h1.getUid());
            tx.commit();
        } catch (Exception e) {
            LOG.error(">> Exception thrown during retrieve when using ValueGenerator", e);
            fail("Failure on retrieve with ValueGenerator : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(CustomUUIDHolder.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) CustomUUIDHolder(org.datanucleus.samples.annotations.valuegenerator.CustomUUIDHolder)

Aggregations

EntityManager (javax.persistence.EntityManager)1 EntityTransaction (javax.persistence.EntityTransaction)1 CustomUUIDHolder (org.datanucleus.samples.annotations.valuegenerator.CustomUUIDHolder)1