use of org.hibernate.Session in project hibernate-orm by hibernate.
the class ZonedDateTimeTest method saveZoneDateTimeEventWithStartDate.
private void saveZoneDateTimeEventWithStartDate(ZonedDateTime startdate) {
final ZonedDateTimeEvent event = new ZonedDateTimeEvent();
event.id = 1L;
event.startDate = startdate;
final Session s = openSession();
s.getTransaction().begin();
try {
s.save(event);
s.getTransaction().commit();
} catch (Exception e) {
if (s.getTransaction() != null && s.getTransaction().getStatus() == TransactionStatus.ACTIVE) {
s.getTransaction().rollback();
}
fail(e.getMessage());
} finally {
s.close();
}
}
use of org.hibernate.Session in project hibernate-orm by hibernate.
the class ZonedDateTimeTest method testRetrievingEntityByZoneDateTime.
@Test
public void testRetrievingEntityByZoneDateTime() {
final ZonedDateTime startDate = ZonedDateTime.of(1, 1, 1, 0, 0, 0, 0, ZoneOffset.ofHours(3));
saveZoneDateTimeEventWithStartDate(startDate);
final Session s = openSession();
try {
Query query = s.createQuery("from ZonedDateTimeEvent o where o.startDate = :date");
query.setParameter("date", startDate, ZonedDateTimeType.INSTANCE);
List<ZonedDateTimeEvent> list = query.list();
assertThat(list.size(), is(1));
} finally {
s.close();
}
}
use of org.hibernate.Session in project hibernate-orm by hibernate.
the class ZonedDateTimeTest method checkSavedZonedDateTimeIsEqual.
private void checkSavedZonedDateTimeIsEqual(ZonedDateTime startdate) {
final Session s = openSession();
try {
final ZonedDateTimeEvent zonedDateTimeEvent = s.get(ZonedDateTimeEvent.class, 1L);
assertThat(zonedDateTimeEvent.startDate.isEqual(startdate), is(true));
} finally {
s.close();
}
}
use of org.hibernate.Session in project hibernate-orm by hibernate.
the class ZonedDateTimeTest method compareSavedZonedDateTimeWith.
private void compareSavedZonedDateTimeWith(ZonedDateTime startdate) {
final Session s = openSession();
try {
final ZonedDateTimeEvent zonedDateTimeEvent = s.get(ZonedDateTimeEvent.class, 1L);
assertThat(ZonedDateTimeType.INSTANCE.getComparator().compare(zonedDateTimeEvent.startDate, startdate), is(0));
} finally {
s.close();
}
}
use of org.hibernate.Session in project hibernate-orm by hibernate.
the class SubselectFetchTest method testSubselectFetchCriteria.
@Test
public void testSubselectFetchCriteria() {
Session s = openSession();
Transaction t = s.beginTransaction();
Parent p = new Parent("foo");
p.getChildren().add(new Child("foo1"));
p.getChildren().add(new Child("foo2"));
Parent q = new Parent("bar");
q.getChildren().add(new Child("bar1"));
q.getChildren().add(new Child("bar2"));
q.getMoreChildren().addAll(p.getChildren());
s.persist(p);
s.persist(q);
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
sessionFactory().getStatistics().clear();
List parents = s.createCriteria(Parent.class).add(Property.forName("name").between("bar", "foo")).addOrder(Order.desc("name")).list();
p = (Parent) parents.get(0);
q = (Parent) parents.get(1);
assertFalse(Hibernate.isInitialized(p.getChildren()));
assertFalse(Hibernate.isInitialized(q.getChildren()));
assertEquals(p.getChildren().size(), 2);
assertTrue(Hibernate.isInitialized(p.getChildren().iterator().next()));
assertTrue(Hibernate.isInitialized(q.getChildren()));
assertEquals(q.getChildren().size(), 2);
assertTrue(Hibernate.isInitialized(q.getChildren().iterator().next()));
assertFalse(Hibernate.isInitialized(p.getMoreChildren()));
assertFalse(Hibernate.isInitialized(q.getMoreChildren()));
assertEquals(p.getMoreChildren().size(), 0);
assertTrue(Hibernate.isInitialized(q.getMoreChildren()));
assertEquals(q.getMoreChildren().size(), 2);
assertTrue(Hibernate.isInitialized(q.getMoreChildren().iterator().next()));
assertEquals(3, sessionFactory().getStatistics().getPrepareStatementCount());
Child c = (Child) p.getChildren().get(0);
c.getFriends().size();
s.delete(p);
s.delete(q);
t.commit();
s.close();
}
Aggregations