Search in sources :

Example 76 with EntityTransaction

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

the class RelationshipsTest method testOneToManyWithOrphanRemovalAndDeleting.

/**
 * Test of 1-N List of entity elements and use of orphan removal flag when deleting the owner.
 */
public void testOneToManyWithOrphanRemovalAndDeleting() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            ListHolder holder1 = new ListHolder(1);
            holder1.getJoinListPC().add(new PCFKListElement(1, "First"));
            holder1.getJoinListPC().add(new PCFKListElement(2, "Second"));
            holder1.getJoinListPC().add(new PCFKListElement(3, "Third"));
            em.persist(holder1);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception thrown while creating data");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        // Check the contents of the datastore and trigger the delete
        em = getEM();
        tx = em.getTransaction();
        try {
            tx.begin();
            ListHolder holder1 = em.find(ListHolder.class, new Long(1));
            assertEquals("Number of list elements is wrong", 3, holder1.getJoinListPC().size());
            // Delete holder which should trigger the orphan removal on the elements
            em.remove(holder1);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception thrown while retrieving data");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        // Check the contents of the datastore
        em = getEM();
        tx = em.getTransaction();
        try {
            tx.begin();
            ListHolder holder1 = em.find(ListHolder.class, new Long(1));
            assertNull("Holder should have been deleted but wasn't", holder1);
            PCFKListElement el1 = em.find(PCFKListElement.class, new Long(1));
            assertNull("Element1 should have been deleted but wasn't", el1);
            PCFKListElement el2 = em.find(PCFKListElement.class, new Long(2));
            assertNull("Element2 should have been deleted but wasn't", el2);
            PCFKListElement el3 = em.find(PCFKListElement.class, new Long(3));
            assertNull("Element3 should have been deleted but wasn't", el3);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception thrown while retrieving data");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(ListHolder.class);
        clean(PCFKListElement.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) PCFKListElement(org.datanucleus.samples.annotations.one_many.collection.PCFKListElement) ListHolder(org.datanucleus.samples.annotations.one_many.collection.ListHolder)

Example 77 with EntityTransaction

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

the class RelationshipsTest method testOneToManyBidirMappedByDotNotation.

/**
 * Test of 1-N bidir with element having embedded PC, and 1-1 bidir with embedded PC, and using "mappedBy" with DOT notation.
 */
public void testOneToManyBidirMappedByDotNotation() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            Contact c1 = new Contact("John Doe");
            Document d1 = new Document("Doc1", c1);
            Document d2 = new Document("Doc2", c1);
            c1.setMainDocument(d1);
            d1.getDetails().setOwner(c1);
            c1.getDocuments().add(d2);
            d2.getDetails().setOwner(c1);
            em.persist(c1);
            em.persist(d1);
            em.persist(d2);
            Contact c2 = new Contact("Jane Dose");
            em.persist(c2);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception thrown while creating data");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        // Query the datastore and check the results
        em = getEM();
        tx = em.getTransaction();
        List<Document> documents = null;
        try {
            tx.begin();
            TypedQuery<Document> query = em.createQuery("SELECT d FROM Document d JOIN FETCH d.details.owner owner_1 WHERE d.name = 'Doc2'", Document.class);
            documents = query.getResultList();
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception thrown while retrieving data");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        assertEquals(1, documents.size());
        assertEquals("Doc2", documents.get(0).getName());
        assertEquals("John Doe", documents.get(0).getDetails().getOwner().getName());
        em = getEM();
        tx = em.getTransaction();
        try {
            tx.begin();
            // Null out the relations
            TypedQuery<Contact> query = em.createQuery("SELECT c FROM " + Contact.class.getName() + " c", Contact.class);
            List<Contact> contacts = query.getResultList();
            for (Contact c : contacts) {
                c.setMainDocument(null);
                c.getDocuments().clear();
            }
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception thrown while retrieving data");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(Contact.class);
        clean(Document.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Document(org.datanucleus.samples.annotations.one_many.bidir_3.Document) Contact(org.datanucleus.samples.annotations.one_many.bidir_3.Contact)

Example 78 with EntityTransaction

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

the class RelationshipsTest method testOneToManyJoinTableIndexedList.

/**
 * Test of 1-N JoinTable indexed list (JPA2).
 */
public void testOneToManyJoinTableIndexedList() {
    EntityManagerFactory emf2 = getEMF(1, "JPATest", null);
    try {
        EntityManager em = emf2.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            UserGroup grp = new UserGroup(1, "JDO Expert Group");
            GroupMember member1 = new GroupMember(1, "Craig Russell");
            GroupMember member2 = new GroupMember(2, "David Jordan");
            List<GroupMember> members = new ArrayList<>();
            members.add(member1);
            members.add(member2);
            grp.setMembers(members);
            em.persist(grp);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception thrown while creating UserGroup+GroupMembers");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        // Check the contents of the datastore
        em = emf2.createEntityManager();
        tx = em.getTransaction();
        try {
            tx.begin();
            List result = em.createQuery("SELECT Object(T) FROM " + UserGroup.class.getName() + " T").getResultList();
            assertEquals(1, result.size());
            UserGroup grp = (UserGroup) result.get(0);
            Collection<GroupMember> members = grp.getMembers();
            assertEquals("Number of GroupMembers in UserGroup is incorrect", 2, members.size());
            Iterator<GroupMember> iter = members.iterator();
            boolean hasMem1 = false;
            boolean hasMem2 = false;
            GroupMember mem1 = iter.next();
            GroupMember mem2 = iter.next();
            if (mem1.getName().equals("Craig Russell")) {
                hasMem1 = true;
            }
            if (mem2.getName().equals("David Jordan")) {
                hasMem2 = true;
            }
            assertTrue("First member of user group is not present (in right place)", hasMem1);
            assertTrue("Second member of user group is not present (in right place)", hasMem2);
            tx.rollback();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception thrown while retrieving UserGroup+GroupMembers");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(emf2, UserGroup.class);
        clean(emf2, GroupMember.class);
    }
    emf2.close();
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) GroupMember(org.jpox.samples.one_many.unidir_2.GroupMember) EntityManager(javax.persistence.EntityManager) EntityManagerFactory(javax.persistence.EntityManagerFactory) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) UserGroup(org.jpox.samples.one_many.unidir_2.UserGroup)

Example 79 with EntityTransaction

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

the class RelationshipsTest method testOneToManyFKList.

/**
 * Test of 1-N FK relation with ordered list
 */
public void testOneToManyFKList() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            Farm farm1 = new Farm("Giles Farm");
            Animal an1 = new Animal("Spot the Dog");
            farm1.getAnimals().add(an1);
            an1.setFarm(farm1);
            Animal an2 = new Animal("Rosa the Rhino");
            farm1.getAnimals().add(an2);
            an2.setFarm(farm1);
            em.persist(farm1);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception thrown while creating Farm and Animals");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        // Check the contents of the datastore
        em = getEM();
        tx = em.getTransaction();
        try {
            tx.begin();
            List result = em.createQuery("SELECT Object(T) FROM " + Farm.class.getName() + " T").getResultList();
            assertEquals(1, result.size());
            Farm farm = (Farm) result.get(0);
            List<Animal> animals = farm.getAnimals();
            assertEquals("Number of animals in Farm is incorrect", 2, animals.size());
            Animal an1 = animals.get(0);
            Animal an2 = animals.get(1);
            assertEquals("First Animal in list has incorrect name", "Rosa the Rhino", an1.getName());
            assertEquals("Second Animal in list has incorrect name", "Spot the Dog", an2.getName());
            tx.rollback();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception thrown while retrieving Farm and Animals");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(Farm.class);
        clean(Animal.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Animal(org.datanucleus.samples.annotations.one_many.bidir.Animal) Farm(org.datanucleus.samples.annotations.one_many.bidir.Farm) ArrayList(java.util.ArrayList) List(java.util.List)

Example 80 with EntityTransaction

use of javax.persistence.EntityTransaction 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

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