Search in sources :

Example 1 with Processor

use of org.datanucleus.samples.annotations.embedded.Processor in project tests by datanucleus.

the class EmbeddedTest method testOneToManyEmbeddedElements.

/**
 * Test of 1-N relation with embedded elements.
 */
public void testOneToManyEmbeddedElements() {
    if (!storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_EMBEDDED_COLLECTION)) {
        return;
    }
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            Processor proc = new Processor(1, "RISC");
            Job job1 = new Job("Cron backup", 1);
            proc.addJob(job1);
            Job job2 = new Job("Jenkins", 2);
            proc.addJob(job2);
            em.persist(proc);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception thrown creating data", e);
            fail("Exception thrown while creating data (see log for details) : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        // Check the contents of the datastore
        em = getEM();
        tx = em.getTransaction();
        try {
            tx.begin();
            Processor proc = em.find(Processor.class, 1);
            assertNotNull(proc);
            List<Job> jobs = proc.getJobs();
            assertNotNull(jobs);
            assertEquals(2, jobs.size());
            tx.rollback();
        } catch (Exception e) {
            LOG.error("Exception thrown retrieving data", e);
            fail("Exception thrown while retrieving data " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(Processor.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Processor(org.datanucleus.samples.annotations.embedded.Processor) Job(org.datanucleus.samples.annotations.embedded.Job)

Example 2 with Processor

use of org.datanucleus.samples.annotations.embedded.Processor in project tests by datanucleus.

the class JPQLQueryTest method testQueryOfEmbeddedElement.

/**
 * Test for query of embedded collection elements.
 */
public void testQueryOfEmbeddedElement() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            Processor proc1 = new Processor(1, "quad-core");
            Job j1 = new Job("dn1", 1);
            Job j2 = new Job("dn2", 2);
            proc1.addJob(j1);
            proc1.addJob(j2);
            Processor proc2 = new Processor(2, "dual-core");
            Job j3 = new Job("dn3", 1);
            proc2.addJob(j3);
            em.persist(proc1);
            em.persist(proc2);
            em.flush();
            Query q = em.createQuery("SELECT p FROM Processor p JOIN p.jobs j WHERE j.name='dn1'");
            List<Processor> results = q.getResultList();
            assertEquals(1, results.size());
            Processor pr1 = results.get(0);
            assertEquals(2, pr1.getNumberOfJobs());
            assertEquals("quad-core", pr1.getType());
            tx.rollback();
        } catch (PersistenceException e) {
            LOG.error("Exception in test", e);
            fail("Exception in test : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(Processor.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Processor(org.datanucleus.samples.annotations.embedded.Processor) JPAQuery(org.datanucleus.api.jpa.JPAQuery) Query(javax.persistence.Query) TypedQuery(javax.persistence.TypedQuery) PersistenceException(javax.persistence.PersistenceException) Job(org.datanucleus.samples.annotations.embedded.Job)

Aggregations

EntityManager (javax.persistence.EntityManager)2 EntityTransaction (javax.persistence.EntityTransaction)2 Job (org.datanucleus.samples.annotations.embedded.Job)2 Processor (org.datanucleus.samples.annotations.embedded.Processor)2 PersistenceException (javax.persistence.PersistenceException)1 Query (javax.persistence.Query)1 TypedQuery (javax.persistence.TypedQuery)1 JPAQuery (org.datanucleus.api.jpa.JPAQuery)1