Search in sources :

Example 1 with CompoundSingleTarget

use of org.datanucleus.samples.annotations.compoundidentity.CompoundSingleTarget in project tests by datanucleus.

the class CompoundIdentityTest method testOneToOneUniSingleAnnotations.

/**
 * Basic test of 1-1 uni relation using compound identity relation and JPA annotations.
 */
public void testOneToOneUniSingleAnnotations() {
    // Swap to "JPATest" EMF
    EntityManagerFactory emf = getEMF(1, "JPATest", null);
    try {
        CompoundSingleTarget[] targets = new CompoundSingleTarget[6];
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            CompoundHolder holder1 = new CompoundHolder("First Holder");
            CompoundHolder holder2 = new CompoundHolder("Second Holder");
            CompoundHolder holder3 = new CompoundHolder("Third Holder");
            targets[0] = new CompoundSingleTarget(holder3, 1.0);
            targets[1] = new CompoundSingleTarget(holder3, 2.0);
            targets[2] = new CompoundSingleTarget(holder2, 3.0);
            targets[3] = new CompoundSingleTarget(holder2, 4.0);
            targets[4] = new CompoundSingleTarget(holder1, 5.0);
            targets[5] = new CompoundSingleTarget(holder1, 6.0);
            for (int i = 0; i < 6; i++) {
                em.persist(targets[i]);
            }
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception during test", e);
            fail(e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        em = emf.createEntityManager();
        tx = em.getTransaction();
        try {
            tx.begin();
            List result = em.createQuery("SELECT Object(T) FROM " + CompoundHolder.class.getName() + " T WHERE T.name = 'First Holder'").getResultList();
            CompoundHolder holder1 = (CompoundHolder) result.get(0);
            result = em.createQuery("SELECT Object(T) FROM " + CompoundHolder.class.getName() + " T WHERE T.name = 'Second Holder'").getResultList();
            CompoundHolder holder2 = (CompoundHolder) result.get(0);
            result = em.createQuery("SELECT Object(T) FROM " + CompoundHolder.class.getName() + " T WHERE T.name = 'Third Holder'").getResultList();
            CompoundHolder holder3 = (CompoundHolder) result.get(0);
            assertEquals("Name of holder was incorrect", "First Holder", holder1.getName());
            assertEquals("Name of holder was incorrect", "Second Holder", holder2.getName());
            assertEquals("Name of holder was incorrect", "Third Holder", holder3.getName());
            for (int i = 0; i < targets.length; i++) {
                result = em.createQuery("SELECT Object(T) FROM " + CompoundSingleTarget.class.getName() + " T WHERE T.value = " + (float) ((i + 1) * 1.0)).getResultList();
                CompoundSingleTarget target = (CompoundSingleTarget) result.get(0);
                assertEquals(i + 1, target.getValue(), 0);
                if (i == 0 || i == 1) {
                    assertEquals("Name of holder of target is incorrect", "Third Holder", target.getHolder().getName());
                }
                if (i == 2 || i == 3) {
                    assertEquals("Name of holder of target is incorrect", "Second Holder", target.getHolder().getName());
                }
                if (i == 4 || i == 5) {
                    assertEquals("Name of holder of target is incorrect", "First Holder", target.getHolder().getName());
                }
            }
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception during test", e);
            fail(e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(emf, CompoundSingleTarget.class);
        clean(emf, CompoundHolder.class);
        emf.close();
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) CompoundSingleTarget(org.datanucleus.samples.annotations.compoundidentity.CompoundSingleTarget) EntityManagerFactory(javax.persistence.EntityManagerFactory) List(java.util.List) CompoundHolder(org.datanucleus.samples.annotations.compoundidentity.CompoundHolder)

Aggregations

List (java.util.List)1 EntityManager (javax.persistence.EntityManager)1 EntityManagerFactory (javax.persistence.EntityManagerFactory)1 EntityTransaction (javax.persistence.EntityTransaction)1 CompoundHolder (org.datanucleus.samples.annotations.compoundidentity.CompoundHolder)1 CompoundSingleTarget (org.datanucleus.samples.annotations.compoundidentity.CompoundSingleTarget)1