Search in sources :

Example 96 with EntityTransaction

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

the class EntityManagerTest method testPersist.

/**
 * Test of EntityManager.persist()
 */
public void testPersist() {
    try {
        EntityManager em = getEM();
        if ("cassandra".equals(storeMgr.getStoreManagerKey())) {
            em.setProperty("datanucleus.cassandra.enforceUniquenessInApplication", "true");
        } else if ("hbase".equals(storeMgr.getStoreManagerKey())) {
            em.setProperty("datanucleus.hbase.enforceUniquenessInApplication", "true");
        }
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            Person p = new Person(101, "Fred", "Flintstone", "fred.flintstone@jpox.com");
            p.setGlobalNum("First");
            em.persist(p);
            tx.commit();
            try {
                tx.begin();
                Person p2 = new Person(101, "Fred", "Flintstone", "fred.flintstone@jpox.com");
                p2.setGlobalNum("First");
                em.persist(p2);
                tx.commit();
                fail("Allowed to persist same object twice!");
            } catch (EntityExistsException eee) {
            // Expected since the object had been persisted earlier
            } catch (PersistenceException eee) {
            // Expected since the object had been persisted earlier
            } finally {
                if (tx.isActive()) {
                    tx.rollback();
                }
            }
            try {
                tx.begin();
                Person p2 = new Person(101, "Fred", "Flintstone", "fred.flintstone@jpox.com");
                p2.setGlobalNum("First");
                em.persist(p2);
                tx.commit();
                fail("Allowed to persist object with same id twice");
            } catch (EntityExistsException eee) {
            // Either this or PersistenceException expected
            } catch (PersistenceException pe) {
            // Either this or EntityExistsException expected
            }
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        em.close();
    } finally {
        clean(Person.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) PersistenceException(javax.persistence.PersistenceException) VersionedPerson(org.datanucleus.samples.annotations.versioned.VersionedPerson) Person(org.datanucleus.samples.annotations.models.company.Person) EntityExistsException(javax.persistence.EntityExistsException)

Example 97 with EntityTransaction

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

the class EntityManagerTest method testDetachAccessUndetachedField.

/**
 * Test of detaching and access of an undetached field.
 */
public void testDetachAccessUndetachedField() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        Person p = null;
        Person.PK pk = null;
        try {
            tx.begin();
            p = new Person(101, "Billy", "Nomates", "billy.nomates@nowhere.com");
            p.getPhoneNumbers().put("Joey", new org.datanucleus.samples.annotations.models.company.PhoneNumber("Joey", "+44 123456789"));
            em.persist(p);
            tx.commit();
            pk = p.getPK();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        em.close();
        // Retrieve it
        em = getEM();
        tx = em.getTransaction();
        try {
            tx.begin();
            p = em.find(Person.class, pk);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        em.close();
        em = getEM();
        try {
            p.getPhoneNumbers();
        } catch (Throwable thr) {
            assertTrue(thr instanceof IllegalAccessException);
        }
    } finally {
        clean(Person.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) VersionedPerson(org.datanucleus.samples.annotations.versioned.VersionedPerson) Person(org.datanucleus.samples.annotations.models.company.Person)

Example 98 with EntityTransaction

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

the class EntityManagerTest method testMerge.

/**
 * Test of EntityManager.merge()
 */
public void testMerge() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            // Persist an object
            tx.begin();
            Person p1 = new Person(101, "Fred", "Flintstone", "fred.flintstone@jpox.com");
            em.persist(p1);
            tx.commit();
            // Merge the detached object
            tx.begin();
            em.merge(p1);
            tx.commit();
            // Merge a new object
            tx.begin();
            Person p2 = new Person(102, "Barney", "Rubble", "barney.rubble@jpox.com");
            em.merge(p2);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception using merge " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        em.close();
    } finally {
        clean(Person.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) VersionedPerson(org.datanucleus.samples.annotations.versioned.VersionedPerson) Person(org.datanucleus.samples.annotations.models.company.Person) EntityExistsException(javax.persistence.EntityExistsException) PersistenceException(javax.persistence.PersistenceException) EntityNotFoundException(javax.persistence.EntityNotFoundException)

Example 99 with EntityTransaction

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

the class EntityManagerTest method testFind.

/**
 * Test of EntityManager.find()
 */
public void testFind() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            Person p1 = new Person(101, "Fred", "Flintstone", "fred.flintstone@jpox.com");
            em.persist(p1);
            Account acct1 = new Account();
            acct1.setUsername("fredf");
            em.persist(acct1);
            em.flush();
            Person.PK pk = p1.getPK();
            long acctId = acct1.getId();
            tx.commit();
            tx.begin();
            // Find using IdClass instance
            Person person = em.find(Person.class, pk);
            assertEquals(p1.getFirstName(), person.getFirstName());
            // Find using key value (but of slightly different type that needs conversion - DN extension)
            Account acct = em.find(Account.class, Integer.valueOf("" + acctId));
            assertEquals(acct1.getUsername(), acct.getUsername());
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(Account.class);
        clean(Person.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) Account(org.datanucleus.samples.annotations.models.company.Account) EntityManager(javax.persistence.EntityManager) VersionedPerson(org.datanucleus.samples.annotations.versioned.VersionedPerson) Person(org.datanucleus.samples.annotations.models.company.Person)

Example 100 with EntityTransaction

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

the class EntityManagerTest method testTransactionBeginTwice.

/**
 * Test of calling tx.begin() twice in a row.
 */
public void testTransactionBeginTwice() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            try {
                tx.begin();
                fail("Expected IllegalStateException on tx.begin() twice but got nothing");
            } catch (IllegalStateException ise) {
            // Expected
            } catch (Exception e) {
                fail("Expected IllegalStateException but got " + e);
            }
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        em.close();
    } finally {
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) EntityExistsException(javax.persistence.EntityExistsException) PersistenceException(javax.persistence.PersistenceException) EntityNotFoundException(javax.persistence.EntityNotFoundException)

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