Search in sources :

Example 61 with Session

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();
    }
}
Also used : Session(org.hibernate.Session)

Example 62 with Session

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();
    }
}
Also used : Query(org.hibernate.Query) ZonedDateTime(java.time.ZonedDateTime) Session(org.hibernate.Session) Test(org.junit.Test)

Example 63 with Session

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();
    }
}
Also used : Session(org.hibernate.Session)

Example 64 with Session

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();
    }
}
Also used : Session(org.hibernate.Session)

Example 65 with Session

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();
}
Also used : Transaction(org.hibernate.Transaction) List(java.util.List) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

Session (org.hibernate.Session)4457 Test (org.junit.Test)2517 Transaction (org.hibernate.Transaction)1327 List (java.util.List)576 ArrayList (java.util.ArrayList)475 Test (org.testng.annotations.Test)411 TestForIssue (org.hibernate.testing.TestForIssue)389 Query (org.hibernate.Query)345 HibernateException (org.hibernate.HibernateException)299 DAOException (com.tomasio.projects.trainning.exception.DAOException)186 Criteria (org.hibernate.Criteria)174 Date (java.util.Date)134 Iterator (java.util.Iterator)127 SkipForDialect (org.hibernate.testing.SkipForDialect)114 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)113 SQLException (java.sql.SQLException)108 Map (java.util.Map)105 SessionFactory (org.hibernate.SessionFactory)105 GradingFactorDaoException (com.remswork.project.alice.dao.exception.GradingFactorDaoException)104 GradingFactorException (com.remswork.project.alice.exception.GradingFactorException)104