Search in sources :

Example 81 with EntityTransaction

use of javax.persistence.EntityTransaction in project tests by datanucleus.

the class ArrayTest method testArrayOfLongViaJoin.

public void testArrayOfLongViaJoin() {
    try {
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            ArrayHolder h1 = new ArrayHolder(1);
            long[] longs = new long[] { 123, 6789, 25000 };
            h1.setLongArray(longs);
            em.persist(h1);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        emf.getCache().evictAll();
        em = emf.createEntityManager();
        tx = em.getTransaction();
        try {
            tx.begin();
            ArrayHolder h1 = em.find(ArrayHolder.class, 1);
            assertNotNull(h1);
            long[] longs = h1.getLongArray();
            assertNotNull(longs);
            assertEquals(3, longs.length);
            assertEquals(123, longs[0]);
            assertEquals(6789, longs[1]);
            assertEquals(25000, longs[2]);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(ArrayHolder.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) ArrayHolder(org.datanucleus.samples.annotations.array.ArrayHolder)

Example 82 with EntityTransaction

use of javax.persistence.EntityTransaction in project tests by datanucleus.

the class ArrayTest method testArrayOfPersistablesViaJoin.

public void testArrayOfPersistablesViaJoin() {
    try {
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            ArrayHolder h1 = new ArrayHolder(1);
            Permission p1 = new Permission(101, "Admin");
            Permission p2 = new Permission(102, "Developer");
            Permission[] perms = new Permission[] { p1, p2 };
            h1.setPermissions(perms);
            em.persist(h1);
            em.persist(p1);
            em.persist(p2);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        emf.getCache().evictAll();
        em = emf.createEntityManager();
        tx = em.getTransaction();
        try {
            tx.begin();
            ArrayHolder h1 = em.find(ArrayHolder.class, 1);
            assertNotNull(h1);
            Permission[] perms = h1.getPermissions();
            assertNotNull(perms);
            assertEquals(2, perms.length);
            assertEquals(101, perms[0].getId());
            assertEquals(102, perms[1].getId());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(ArrayHolder.class);
        clean(Permission.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Permission(org.datanucleus.samples.annotations.array.Permission) ArrayHolder(org.datanucleus.samples.annotations.array.ArrayHolder)

Example 83 with EntityTransaction

use of javax.persistence.EntityTransaction in project tests by datanucleus.

the class CallbackTest method testCallbackNotOverridden.

public void testCallbackNotOverridden() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            CallbackBase.invoked.clear();
            tx.begin();
            CallbackSub1Sub1 d = new CallbackSub1Sub1();
            d.setName("dpt1");
            d.setId("1");
            em.persist(d);
            em.flush();
            assertEquals(1, CallbackBase.invoked.size());
            assertEquals(CallbackSub1Sub1.class.getName(), (CallbackBase.invoked.get(0)).getName());
            tx.rollback();
        } finally {
            CallbackBase.invoked.clear();
            CallbackSub1Listener.invoked.clear();
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(CallbackSub1Sub1.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) CallbackSub1Sub1(org.datanucleus.samples.annotations.callbacks.CallbackSub1Sub1)

Example 84 with EntityTransaction

use of javax.persistence.EntityTransaction in project tests by datanucleus.

the class CallbackTest method testExpectionInCallbackNotEaten.

public void testExpectionInCallbackNotEaten() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            CallbackSub2 d = new CallbackSub2();
            d.setName("dpt1");
            d.setId("1");
            em.persist(d);
            em.flush();
            fail("Expected ArithmeticException");
        } catch (ArithmeticException ex) {
        // expected
        } finally {
            CallbackBase.invoked.clear();
            CallbackSub1Listener.invoked.clear();
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(CallbackSub2.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) CallbackSub2(org.datanucleus.samples.annotations.callbacks.CallbackSub2)

Example 85 with EntityTransaction

use of javax.persistence.EntityTransaction in project tests by datanucleus.

the class CompoundIdentityTest method testOneToOneUniSingleXML.

/**
 * Basic test of 1-1 uni relation using compound identity relation and JPA XML.
 */
public void testOneToOneUniSingleXML() {
    // Swap to "JPATest" EMF
    EntityManagerFactory emf = getEMF(1, "JPATest", null);
    try {
        org.jpox.samples.compoundidentity.CompoundSingleTarget[] targets = new org.jpox.samples.compoundidentity.CompoundSingleTarget[6];
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            org.jpox.samples.compoundidentity.CompoundHolder holder1 = new org.jpox.samples.compoundidentity.CompoundHolder("First Holder");
            org.jpox.samples.compoundidentity.CompoundHolder holder2 = new org.jpox.samples.compoundidentity.CompoundHolder("Second Holder");
            org.jpox.samples.compoundidentity.CompoundHolder holder3 = new org.jpox.samples.compoundidentity.CompoundHolder("Third Holder");
            targets[0] = new org.jpox.samples.compoundidentity.CompoundSingleTarget(holder3, 1.0);
            targets[1] = new org.jpox.samples.compoundidentity.CompoundSingleTarget(holder3, 2.0);
            targets[2] = new org.jpox.samples.compoundidentity.CompoundSingleTarget(holder2, 3.0);
            targets[3] = new org.jpox.samples.compoundidentity.CompoundSingleTarget(holder2, 4.0);
            targets[4] = new org.jpox.samples.compoundidentity.CompoundSingleTarget(holder1, 5.0);
            targets[5] = new org.jpox.samples.compoundidentity.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 " + org.jpox.samples.compoundidentity.CompoundHolder.class.getName() + " T WHERE T.name = 'First Holder'").getResultList();
            org.jpox.samples.compoundidentity.CompoundHolder holder1 = (org.jpox.samples.compoundidentity.CompoundHolder) result.get(0);
            result = em.createQuery("SELECT Object(T) FROM " + org.jpox.samples.compoundidentity.CompoundHolder.class.getName() + " T WHERE T.name = 'Second Holder'").getResultList();
            org.jpox.samples.compoundidentity.CompoundHolder holder2 = (org.jpox.samples.compoundidentity.CompoundHolder) result.get(0);
            result = em.createQuery("SELECT Object(T) FROM " + org.jpox.samples.compoundidentity.CompoundHolder.class.getName() + " T WHERE T.name = 'Third Holder'").getResultList();
            org.jpox.samples.compoundidentity.CompoundHolder holder3 = (org.jpox.samples.compoundidentity.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 " + org.jpox.samples.compoundidentity.CompoundSingleTarget.class.getName() + " T WHERE T.value = " + (float) ((i + 1) * 1.0)).getResultList();
                org.jpox.samples.compoundidentity.CompoundSingleTarget target = (org.jpox.samples.compoundidentity.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, org.jpox.samples.compoundidentity.CompoundSingleTarget.class);
        clean(emf, org.jpox.samples.compoundidentity.CompoundHolder.class);
        emf.close();
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) CompoundSingleTarget(org.datanucleus.samples.annotations.compoundidentity.CompoundSingleTarget) EntityManager(javax.persistence.EntityManager) EntityManagerFactory(javax.persistence.EntityManagerFactory) List(java.util.List) CompoundHolder(org.datanucleus.samples.annotations.compoundidentity.CompoundHolder)

Aggregations

EntityTransaction (javax.persistence.EntityTransaction)540 EntityManager (javax.persistence.EntityManager)447 Query (javax.persistence.Query)143 Person (org.datanucleus.samples.annotations.models.company.Person)101 List (java.util.List)90 TypedQuery (javax.persistence.TypedQuery)82 NoResultException (javax.persistence.NoResultException)77 ArrayList (java.util.ArrayList)69 NotFoundException (org.opencastproject.util.NotFoundException)61 PersistenceException (javax.persistence.PersistenceException)59 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)55 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)50 JPAQuery (org.datanucleus.api.jpa.JPAQuery)32 EntityManagerFactory (javax.persistence.EntityManagerFactory)29 Predicate (javax.persistence.criteria.Predicate)26 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)21 Connection (java.sql.Connection)20 RollbackException (javax.persistence.RollbackException)20 HashSet (java.util.HashSet)19 Employee (org.datanucleus.samples.annotations.models.company.Employee)19