Search in sources :

Example 66 with Session

use of org.hibernate.Session in project hibernate-orm by hibernate.

the class SubselectFetchWithFormulaTransactSqlTest method before.

@Before
public void before() {
    Session session = openSession();
    session.getTransaction().begin();
    Name chris = new Name();
    chris.setId(1);
    chris.setName("chris");
    Value cat = new Value();
    cat.setId(1);
    cat.setName(chris);
    cat.setValue("cat");
    Value canary = new Value();
    canary.setId(2);
    canary.setName(chris);
    canary.setValue("canary");
    session.persist(chris);
    session.persist(cat);
    session.persist(canary);
    Name sam = new Name();
    sam.setId(2);
    sam.setName("sam");
    Value seal = new Value();
    seal.setId(3);
    seal.setName(sam);
    seal.setValue("seal");
    Value snake = new Value();
    snake.setId(4);
    snake.setName(sam);
    snake.setValue("snake");
    session.persist(sam);
    session.persist(seal);
    session.persist(snake);
    session.getTransaction().commit();
    session.close();
}
Also used : Session(org.hibernate.Session) Before(org.junit.Before)

Example 67 with Session

use of org.hibernate.Session in project hibernate-orm by hibernate.

the class MySQL57TimestampFspFunctionTest method testTimeStampFunctions.

@Test
public void testTimeStampFunctions() {
    // add an entity just so it can be queried.
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    s.persist(new Entity());
    tx.commit();
    s.close();
    s = openSession();
    tx = s.beginTransaction();
    // current_timestamp(), localtime(), and localtimestamp() are synonyms for now(),
    // which returns the time at which the statement began to execute.
    // the returned values for now(), current_timestamp(), localtime(), and
    // localtimestamp() should be the same.
    // sysdate() is the time at which the function itself is executed, so the
    // value returned for sysdate() should be different.
    Query q = s.createQuery("select now(), current_timestamp(), localtime(), localtimestamp(), sysdate() from MySQL57TimestampFspFunctionTest$Entity");
    Object[] oArray = (Object[]) q.uniqueResult();
    final Timestamp now = (Timestamp) oArray[0];
    assertEquals(now, oArray[1]);
    assertEquals(now, oArray[2]);
    assertEquals(now, oArray[3]);
    final Timestamp sysdate = (Timestamp) oArray[4];
    assertTrue(now.compareTo(sysdate) < 0);
    // all should have nanos > 0
    assertTrue(now.getNanos() > 0);
    assertTrue(sysdate.getNanos() > 0);
    tx.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Query(org.hibernate.Query) Timestamp(java.sql.Timestamp) Session(org.hibernate.Session) Test(org.junit.Test)

Example 68 with Session

use of org.hibernate.Session in project hibernate-orm by hibernate.

the class OffsetDateTimeTest method saveOffsetDateTimeEventWithStartDate.

private void saveOffsetDateTimeEventWithStartDate(OffsetDateTime startdate) {
    final OffsetDateTimeEvent event = new OffsetDateTimeEvent();
    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 69 with Session

use of org.hibernate.Session in project hibernate-orm by hibernate.

the class OffsetDateTimeTest method compareSavedOffsetDateTimeWith.

private void compareSavedOffsetDateTimeWith(OffsetDateTime startdate) {
    final Session s = openSession();
    try {
        final OffsetDateTimeEvent offsetDateEvent = s.get(OffsetDateTimeEvent.class, 1L);
        assertThat(OffsetDateTimeType.INSTANCE.getComparator().compare(offsetDateEvent.startDate, startdate), is(0));
    } finally {
        s.close();
    }
}
Also used : Session(org.hibernate.Session)

Example 70 with Session

use of org.hibernate.Session in project hibernate-orm by hibernate.

the class SubselectInJoinedTableTest method testSubselectInJoinedTable.

@Test
@TestForIssue(jiraKey = "HHH-10998")
public void testSubselectInJoinedTable() {
    OrderEntry orderEntry1 = new OrderEntry();
    orderEntry1.setOrderEntryId(1L);
    OrderEntry orderEntry2 = new OrderEntry();
    orderEntry2.setOrderEntryId(2L);
    Order order = new Order();
    order.setOrderId(3L);
    order.getOrderEntries().add(orderEntry1);
    order.getOrderEntries().add(orderEntry2);
    order.setFirstOrderEntry(orderEntry1);
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    s.persist(orderEntry1);
    s.persist(orderEntry2);
    s.persist(order);
    tx.commit();
    s.close();
    s = openSession();
    s.getTransaction().begin();
    order = (Order) s.get(Order.class, order.getOrderId());
    assertEquals(orderEntry1.getOrderEntryId(), order.getFirstOrderEntry().getOrderEntryId());
    assertEquals(2, order.getOrderEntries().size());
    assertEquals(orderEntry1.getOrderEntryId(), order.getOrderEntries().get(0).getOrderEntryId());
    assertEquals(orderEntry2.getOrderEntryId(), order.getOrderEntries().get(1).getOrderEntryId());
    s.getTransaction().commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

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