Search in sources :

Example 91 with GregorianCalendar

use of java.util.GregorianCalendar in project hibernate-orm by hibernate.

the class QueryTest method testTemporalTypeBinding.

@Test
public void testTemporalTypeBinding() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    try {
        Query query = em.createQuery("select w from " + Wallet.class.getName() + " w where w.marketEntrance = :me");
        Parameter parameter = query.getParameter("me", Date.class);
        assertEquals(parameter.getParameterType(), Date.class);
        query.setParameter("me", new Date());
        query.setParameter("me", new Date(), TemporalType.DATE);
        query.setParameter("me", new GregorianCalendar(), TemporalType.DATE);
        em.getTransaction().commit();
    } catch (Exception e) {
        if (em.getTransaction() != null && em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        throw e;
    } finally {
        em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) Wallet(org.hibernate.jpa.test.Wallet) GregorianCalendar(java.util.GregorianCalendar) Parameter(javax.persistence.Parameter) Date(java.util.Date) NoResultException(javax.persistence.NoResultException) PersistenceException(javax.persistence.PersistenceException) Test(org.junit.Test)

Example 92 with GregorianCalendar

use of java.util.GregorianCalendar in project hibernate-orm by hibernate.

the class EntityTest method testTemporalType.

@Test
@SkipForDialect(value = Oracle10gDialect.class, comment = "oracle12c returns time in getDate.  For now, skip.")
public void testTemporalType() throws Exception {
    Flight airFrance = doInHibernate(this::sessionFactory, session -> {
        Flight _airFrance = new Flight();
        _airFrance.setId(Long.valueOf(747));
        _airFrance.setName("Paris-Amsterdam");
        _airFrance.setDuration(Long.valueOf(10));
        _airFrance.setDepartureDate(Date.from(LocalDate.of(2005, 06, 21).atStartOfDay(ZoneId.systemDefault()).toInstant()));
        _airFrance.setAlternativeDepartureDate(new GregorianCalendar(2006, 02, 03, 10, 00));
        _airFrance.getAlternativeDepartureDate().setTimeZone(TimeZone.getTimeZone("GMT"));
        _airFrance.setBuyDate(new java.sql.Timestamp(122367443));
        _airFrance.setFactor(25);
        session.persist(_airFrance);
        return _airFrance;
    });
    doInHibernate(this::sessionFactory, session -> {
        Query q = session.createQuery("from Flight f where f.departureDate = :departureDate");
        q.setParameter("departureDate", airFrance.getDepartureDate(), StandardBasicTypes.DATE);
        Flight copyAirFrance = (Flight) q.uniqueResult();
        assertNotNull(copyAirFrance);
        assertEquals(Date.from(LocalDate.of(2005, 06, 21).atStartOfDay(ZoneId.systemDefault()).toInstant()), copyAirFrance.getDepartureDate());
        assertEquals(df.format(airFrance.getBuyDate()), df.format(copyAirFrance.getBuyDate()));
        session.delete(copyAirFrance);
    });
}
Also used : Query(org.hibernate.query.Query) GregorianCalendar(java.util.GregorianCalendar) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test)

Example 93 with GregorianCalendar

use of java.util.GregorianCalendar in project hibernate-orm by hibernate.

the class QueryAndSQLTest method testSQLQueryWithManyToOne.

@Test
public void testSQLQueryWithManyToOne() {
    cleanupCache();
    Night n = new Night();
    Calendar c = new GregorianCalendar();
    c.set(2000, 2, 2);
    Date now = c.getTime();
    c.add(Calendar.MONTH, -1);
    Date aMonthAgo = c.getTime();
    c.add(Calendar.MONTH, 2);
    Date inAMonth = c.getTime();
    n.setDate(now);
    n.setDuration(9999);
    Area a = new Area();
    a.setName("Paris");
    n.setArea(a);
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    s.persist(a);
    s.persist(n);
    tx.commit();
    s.close();
    s = openSession();
    tx = s.beginTransaction();
    Statistics stats = sessionFactory().getStatistics();
    stats.setStatisticsEnabled(true);
    stats.clear();
    Query q = s.getNamedQuery("night&areaCached");
    q.setCacheable(true);
    List result = q.list();
    assertEquals(1, result.size());
    assertEquals(1, stats.getQueryCachePutCount());
    q.setCacheable(true);
    q.list();
    assertEquals(1, stats.getQueryCacheHitCount());
    Night n2 = (Night) ((Object[]) result.get(0))[0];
    assertEquals(n2.getDuration(), n.getDuration());
    s.delete(n2.getArea());
    s.delete(n2);
    tx.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) SQLQuery(org.hibernate.SQLQuery) Query(org.hibernate.Query) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) GregorianCalendar(java.util.GregorianCalendar) List(java.util.List) Statistics(org.hibernate.stat.Statistics) Date(java.util.Date) Session(org.hibernate.Session) Test(org.junit.Test)

Example 94 with GregorianCalendar

use of java.util.GregorianCalendar in project hibernate-orm by hibernate.

the class Customer method generateNewOrder.

public Order generateNewOrder(BigDecimal total) {
    Order order = new Order(this);
    order.setOrderDate(new GregorianCalendar());
    order.setTotal(total);
    return order;
}
Also used : GregorianCalendar(java.util.GregorianCalendar)

Example 95 with GregorianCalendar

use of java.util.GregorianCalendar in project hibernate-orm by hibernate.

the class SQLFunctionsInterSystemsTest method testInterSystemsFunctions.

public void testInterSystemsFunctions() throws Exception {
    Calendar cal = new GregorianCalendar();
    cal.set(1977, 6, 3, 0, 0, 0);
    java.sql.Timestamp testvalue = new java.sql.Timestamp(cal.getTimeInMillis());
    testvalue.setNanos(0);
    Calendar cal3 = new GregorianCalendar();
    cal3.set(1976, 2, 3, 0, 0, 0);
    java.sql.Timestamp testvalue3 = new java.sql.Timestamp(cal3.getTimeInMillis());
    testvalue3.setNanos(0);
    final Session s = openSession();
    s.beginTransaction();
    try {
        s.doWork(new Work() {

            @Override
            public void execute(Connection connection) throws SQLException {
                Statement stmt = ((SessionImplementor) s).getJdbcCoordinator().getStatementPreparer().createStatement();
                ((SessionImplementor) s).getJdbcCoordinator().getResultSetReturn().executeUpdate(stmt, "DROP FUNCTION spLock FROM TestInterSystemsFunctionsClass");
            }
        });
    } catch (Exception ex) {
        System.out.println("as we expected stored procedure sp does not exist when we drop it");
    }
    s.getTransaction().commit();
    s.beginTransaction();
    s.doWork(new Work() {

        @Override
        public void execute(Connection connection) throws SQLException {
            Statement stmt = ((SessionImplementor) s).getJdbcCoordinator().getStatementPreparer().createStatement();
            String create_function = "CREATE FUNCTION SQLUser.TestInterSystemsFunctionsClass_spLock\n" + "     ( INOUT pHandle %SQLProcContext, \n" + "       ROWID INTEGER \n" + " )\n" + " FOR User.TestInterSystemsFunctionsClass " + "    PROCEDURE\n" + "    RETURNS INTEGER\n" + "    LANGUAGE OBJECTSCRIPT\n" + "    {\n" + "        q 0\n" + "     }";
            ((SessionImplementor) s).getJdbcCoordinator().getResultSetReturn().executeUpdate(stmt, create_function);
        }
    });
    s.getTransaction().commit();
    s.beginTransaction();
    TestInterSystemsFunctionsClass object = new TestInterSystemsFunctionsClass(Long.valueOf(10));
    object.setDateText("1977-07-03");
    object.setDate1(testvalue);
    object.setDate3(testvalue3);
    s.save(object);
    s.getTransaction().commit();
    s.close();
    Session s2 = openSession();
    s2.beginTransaction();
    TestInterSystemsFunctionsClass test = s2.get(TestInterSystemsFunctionsClass.class, 10L);
    assertTrue(test.getDate1().equals(testvalue));
    test = (TestInterSystemsFunctionsClass) s2.byId(TestInterSystemsFunctionsClass.class).with(LockOptions.NONE).load(10L);
    assertTrue(test.getDate1().equals(testvalue));
    Date value = (Date) s2.createQuery("select nvl(o.date,o.dateText) from TestInterSystemsFunctionsClass as o").list().get(0);
    assertTrue(value.equals(testvalue));
    Object nv = s2.createQuery("select nullif(o.dateText,o.dateText) from TestInterSystemsFunctionsClass as o").list().get(0);
    assertTrue(nv == null);
    String dateText = (String) s2.createQuery("select nvl(o.dateText,o.date) from TestInterSystemsFunctionsClass as o").list().get(0);
    assertTrue(dateText.equals("1977-07-03"));
    value = (Date) s2.createQuery("select ifnull(o.date,o.date1) from TestInterSystemsFunctionsClass as o").list().get(0);
    assertTrue(value.equals(testvalue));
    value = (Date) s2.createQuery("select ifnull(o.date3,o.date,o.date1) from TestInterSystemsFunctionsClass as o").list().get(0);
    assertTrue(value.equals(testvalue));
    Integer pos = (Integer) s2.createQuery("select position('07', o.dateText) from TestInterSystemsFunctionsClass as o").list().get(0);
    assertTrue(pos.intValue() == 6);
    String st = (String) s2.createQuery("select convert(o.date1, SQL_TIME) from TestInterSystemsFunctionsClass as o").list().get(0);
    assertTrue(st.equals("00:00:00"));
    java.sql.Time tm = (java.sql.Time) s2.createQuery("select cast(o.date1, time) from TestInterSystemsFunctionsClass as o").list().get(0);
    assertTrue(tm.toString().equals("00:00:00"));
    Double diff = (Double) s2.createQuery("select timestampdiff(SQL_TSI_FRAC_SECOND, o.date3, o.date1) from TestInterSystemsFunctionsClass as o").list().get(0);
    assertTrue(diff.doubleValue() != 0.0);
    diff = (Double) s2.createQuery("select timestampdiff(SQL_TSI_MONTH, o.date3, o.date1) from TestInterSystemsFunctionsClass as o").list().get(0);
    assertTrue(diff.doubleValue() == 16.0);
    diff = (Double) s2.createQuery("select timestampdiff(SQL_TSI_WEEK, o.date3, o.date1) from TestInterSystemsFunctionsClass as o").list().get(0);
    assertTrue(diff.doubleValue() >= 16 * 4);
    diff = (Double) s2.createQuery("select timestampdiff(SQL_TSI_YEAR, o.date3, o.date1) from TestInterSystemsFunctionsClass as o").list().get(0);
    assertTrue(diff.doubleValue() == 1.0);
    s2.getTransaction().commit();
    s2.close();
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) GregorianCalendar(java.util.GregorianCalendar) Connection(java.sql.Connection) SQLException(java.sql.SQLException) Date(java.util.Date) Work(org.hibernate.jdbc.Work) Session(org.hibernate.Session)

Aggregations

GregorianCalendar (java.util.GregorianCalendar)1278 Calendar (java.util.Calendar)603 Date (java.util.Date)377 Test (org.junit.Test)247 SimpleDateFormat (java.text.SimpleDateFormat)104 Timestamp (java.sql.Timestamp)54 ScheduleExpression (javax.ejb.ScheduleExpression)51 ArrayList (java.util.ArrayList)48 EJBCronTrigger (org.apache.openejb.core.timer.EJBCronTrigger)42 HashMap (java.util.HashMap)41 ParseException (java.text.ParseException)39 TimeZone (java.util.TimeZone)38 SimpleTimeZone (java.util.SimpleTimeZone)35 BigDecimal (java.math.BigDecimal)32 Date (java.sql.Date)30 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)28 SQLException (java.sql.SQLException)20 DateFormat (java.text.DateFormat)18 IOException (java.io.IOException)16 Map (java.util.Map)16