Search in sources :

Example 1 with DSIDHolder

use of org.datanucleus.samples.annotations.datastoreidentity.DSIDHolder in project tests by datanucleus.

the class DatastoreIdentityTest method testBasic.

/**
 * Basic test.
 */
public void testBasic() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        Object id = null;
        try {
            tx.begin();
            DSIDHolder holder = new DSIDHolder("First Holder");
            em.persist(holder);
            em.flush();
            id = NucleusJPAHelper.getObjectId(holder);
            tx.commit();
        } catch (Exception e) {
            LOG.error(e);
            e.printStackTrace();
            fail(e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        emf.getCache().evictAll();
        em = getEM();
        tx = em.getTransaction();
        try {
            tx.begin();
            Object key = IdentityUtils.getTargetKeyForDatastoreIdentity(id);
            DSIDHolder holder = em.find(DSIDHolder.class, key);
            assertNotNull(holder);
            assertEquals("First Holder", holder.getName());
            tx.commit();
        } catch (Exception e) {
            LOG.error(e);
            e.printStackTrace();
            fail(e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(DSIDHolder.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) DSIDHolder(org.datanucleus.samples.annotations.datastoreidentity.DSIDHolder)

Aggregations

EntityManager (javax.persistence.EntityManager)1 EntityTransaction (javax.persistence.EntityTransaction)1 DSIDHolder (org.datanucleus.samples.annotations.datastoreidentity.DSIDHolder)1