Search in sources :

Example 1 with JodaSample3

use of org.datanucleus.samples.types.jodatime.JodaSample3 in project tests by datanucleus.

the class JodaTimeTest method testQueryLocalTime.

/**
 * Test for LocalTime persistence and retrieval.
 */
public void testQueryLocalTime() {
    try {
        // Create some data we can use for access
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        LocalTime localTime1 = new LocalTime(15, 9, 26);
        LocalTime localTime2 = new LocalTime(7, 9, 26);
        try {
            tx.begin();
            JodaSample3 s1 = new JodaSample3(1, localTime1, localTime1);
            em.persist(s1);
            JodaSample3 s2 = new JodaSample3(2, localTime2, localTime2);
            em.persist(s2);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Error persisting LocalTime sample", e);
            fail("Error persisting LocalTime sample");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        // Retrieve the data
        em = emf.createEntityManager();
        tx = em.getTransaction();
        try {
            tx.begin();
            Query q = em.createQuery("SELECT s FROM " + JodaSample3.class.getName() + " s WHERE s.localTime1 < :timeParam");
            LocalTime timeParam = new LocalTime(11, 9, 26);
            q.setParameter("timeParam", timeParam);
            List<JodaSample3> results = q.getResultList();
            assertEquals("Number of results is wrong", 1, results.size());
            JodaSample3 s = results.get(0);
            LocalTime lt1 = s.getLocalTime1();
            assertNotNull("Retrieved LocalTime was null!", lt1);
            assertEquals("Hour was wrong", 7, lt1.getHourOfDay());
            assertEquals("Minute was wrong", 9, lt1.getMinuteOfHour());
            assertEquals("Second was wrong", 26, lt1.getSecondOfMinute());
            tx.commit();
        } catch (Exception e) {
            LOG.error("Error retrieving LocalTime data", e);
            fail("Error retrieving LocalTime data : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(JodaSample3.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) LocalTime(org.joda.time.LocalTime) Query(javax.persistence.Query) JodaSample3(org.datanucleus.samples.types.jodatime.JodaSample3)

Example 2 with JodaSample3

use of org.datanucleus.samples.types.jodatime.JodaSample3 in project tests by datanucleus.

the class JodaTimeTest method testLocalTime.

/**
 * Test for LocalTime persistence and retrieval.
 */
public void testLocalTime() {
    try {
        // Create some data we can use for access
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        LocalTime localTime1 = new LocalTime(15, 9, 26);
        LocalTime localTime2 = new LocalTime(7, 9, 26);
        try {
            tx.begin();
            JodaSample3 s = new JodaSample3(1, localTime1, localTime2);
            JodaSample3 s2 = new JodaSample3(2, null, null);
            em.persist(s);
            em.persist(s2);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Error persisting LocalTime sample", e);
            fail("Error persisting LocalTime sample");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        // Retrieve the data
        em = emf.createEntityManager();
        tx = em.getTransaction();
        try {
            tx.begin();
            JodaSample3 s = em.find(JodaSample3.class, 1);
            LocalTime lt1 = s.getLocalTime1();
            assertNotNull("Retrieved LocalTime was null!", lt1);
            assertEquals("Hour was wrong", 15, lt1.getHourOfDay());
            assertEquals("Minute was wrong", 9, lt1.getMinuteOfHour());
            assertEquals("Second was wrong", 26, lt1.getSecondOfMinute());
            LocalTime lt2 = s.getLocalTime2();
            assertNotNull("Retrieved LocalTime was null!", lt2);
            assertEquals("Hour was wrong", 7, lt2.getHourOfDay());
            assertEquals("Minute was wrong", 9, lt2.getMinuteOfHour());
            assertEquals("Second was wrong", 26, lt2.getSecondOfMinute());
            JodaSample3 s2 = em.find(JodaSample3.class, 2);
            assertNull(s2.getLocalTime1());
            assertNull(s2.getLocalTime2());
            tx.commit();
        } catch (Exception e) {
            LOG.error("Error retrieving LocalTime data", e);
            fail("Error retrieving LocalTime data : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(JodaSample3.class);
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) LocalTime(org.joda.time.LocalTime) JodaSample3(org.datanucleus.samples.types.jodatime.JodaSample3)

Aggregations

EntityManager (javax.persistence.EntityManager)2 EntityTransaction (javax.persistence.EntityTransaction)2 JodaSample3 (org.datanucleus.samples.types.jodatime.JodaSample3)2 LocalTime (org.joda.time.LocalTime)2 Query (javax.persistence.Query)1