Search in sources :

Example 1 with Person

use of org.datanucleus.samples.annotations.models.company.Person in project tests by datanucleus.

the class EntityManagerFactoryTest method testPersistenceUnitUtilGetIdentifier.

/**
 * Test for emf.getPersistenceUnitUtil.getIdentifier() method
 */
public void testPersistenceUnitUtilGetIdentifier() {
    try {
        PersistenceUnitUtil util = emf.getPersistenceUnitUtil();
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            Person p = new Person(101, "Fred", "Flintstone", "fred.flintstone@jpox.com");
            p.setGlobalNum("First");
            em.persist(p);
            assertTrue(util.getIdentifier(p) instanceof Person.PK);
            VersionedPerson vp = new VersionedPerson(1, 1);
            em.persist(vp);
            Object vpId = util.getIdentifier(vp);
            assertTrue(vpId instanceof Long && ((Long) vpId) == 1);
            tx.rollback();
        } catch (Exception e) {
            LOG.error(">> Exception on persist before serialisation", e);
            fail("Exception on persist : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
    // No cleanup needed
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) PersistenceUnitUtil(javax.persistence.PersistenceUnitUtil) VersionedPerson(org.datanucleus.samples.annotations.versioned.VersionedPerson) VersionedPerson(org.datanucleus.samples.annotations.versioned.VersionedPerson) Person(org.datanucleus.samples.annotations.models.company.Person) PersistenceException(javax.persistence.PersistenceException)

Example 2 with Person

use of org.datanucleus.samples.annotations.models.company.Person in project tests by datanucleus.

the class EntityManagerTest method testPersistThenDetach.

/**
 * Test of EntityManager.persist() and detach().
 */
public void testPersistThenDetach() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            Person p = new Person(101, "Fred", "Flintstone", "fred.flintstone@jpox.com");
            p.setGlobalNum("First");
            em.persist(p);
            em.detach(p);
            tx.commit();
        } 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)

Example 3 with Person

use of org.datanucleus.samples.annotations.models.company.Person 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 4 with Person

use of org.datanucleus.samples.annotations.models.company.Person 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 5 with Person

use of org.datanucleus.samples.annotations.models.company.Person 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)

Aggregations

Person (org.datanucleus.samples.annotations.models.company.Person)102 EntityTransaction (javax.persistence.EntityTransaction)101 EntityManager (javax.persistence.EntityManager)95 List (java.util.List)47 Query (javax.persistence.Query)46 ArrayList (java.util.ArrayList)35 TypedQuery (javax.persistence.TypedQuery)34 PersistenceException (javax.persistence.PersistenceException)18 JPAQuery (org.datanucleus.api.jpa.JPAQuery)17 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)16 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)15 NoResultException (javax.persistence.NoResultException)13 Path (javax.persistence.criteria.Path)11 VersionedPerson (org.datanucleus.samples.annotations.versioned.VersionedPerson)11 NonUniqueResultException (javax.persistence.NonUniqueResultException)9 Predicate (javax.persistence.criteria.Predicate)8 Employee (org.datanucleus.samples.annotations.models.company.Employee)8 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)7 DatastoreAdapter (org.datanucleus.store.rdbms.adapter.DatastoreAdapter)7 Connection (java.sql.Connection)6