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();
}
}
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);
});
}
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();
}
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;
}
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();
}
Aggregations