Example usage for org.hibernate Session doWork

List of usage examples for org.hibernate Session doWork

Introduction

In this page you can find the example usage for org.hibernate Session doWork.

Prototype

void doWork(Work work) throws HibernateException;

Source Link

Document

Controller for allowing users to perform JDBC related work using the Connection managed by this Session.

Usage

From source file:org.jessma.dao.hbn.HibernateSessionMgr.java

License:Apache License

/** ?{@link AbstractSessionMgr#loadDefalutTransIsoLevel()} */
@Override/*from w w w.  jav a 2s  .  c om*/
protected void loadDefalutTransIsoLevel() {
    try {
        Session session = getSession();
        session.doWork(new Work() {
            @Override
            public void execute(Connection connection) throws SQLException {
                int level = connection.getTransactionIsolation();
                defaultTransIsoLevel = TransIsoLevel.fromInt(level);
            }
        });
    } finally {
        closeSession();
    }
}

From source file:org.jessma.dao.hbn.HibernateSessionMgr.java

License:Apache License

/** ?{@link SessionMgr#setSessionTransIsoLevel(TransIsoLevel)} */
@Override/*from  www  . j av  a  2  s  .c om*/
public void setSessionTransIsoLevel(final TransIsoLevel level) {
    Session session = getSession();
    session.doWork(new Work() {
        @Override
        public void execute(Connection connection) throws SQLException {
            connection.setTransactionIsolation(level.toInt());
        }
    });
}

From source file:org.joda.time.contrib.hibernate.TestPersistentDateTime.java

License:Apache License

public void testSimpleStore() throws SQLException {
    SessionFactory factory = getSessionFactory();

    Session session = factory.openSession();

    for (int i = 0; i < writeReadTimes.length; i++) {
        DateTime writeReadTime = writeReadTimes[i];

        Event event = new Event();
        event.setId(i);/*w  ww .j  a  v a  2  s .c  o  m*/
        event.setDateTime(writeReadTime);

        session.save(event);
    }

    session.flush();
    session.doWork(new Work() {
        public void execute(Connection connection) throws SQLException {
            connection.commit();
        }
    });
    session.close();

    for (int i = 0; i < writeReadTimes.length; i++) {
        DateTime writeReadTime = writeReadTimes[i];

        session = factory.openSession();
        Event eventReread = (Event) session.get(Event.class, new Integer(i));

        assertNotNull("get failed - event#'" + i + "'not found", eventReread);
        assertNotNull("get failed - returned null", eventReread.getDateTime());

        // we loose the timezone, so we have to normalize both to offset=0
        assertEquals("get failed - returned different time",
                writeReadTime.toDateTime(DateTimeZone.forOffsetHours(0)),
                eventReread.getDateTime().toDateTime(DateTimeZone.forOffsetHours(0)));

        session.close();
    }
}

From source file:org.joda.time.contrib.hibernate.TestPersistentDateTime.java

License:Apache License

public void testStoreWithTimezone() throws SQLException {
    SessionFactory factory = getSessionFactory();

    Session session = factory.openSession();

    for (int i = 0; i < writeReadTimes.length; i++) {
        DateTime writeReadTime = writeReadTimes[i];

        EventTZ event = new EventTZ();
        event.setId(i);//from   ww w .ja va2s.  c o  m
        event.setDateTime(writeReadTime);

        session.save(event);
    }

    session.flush();
    session.doWork(new Work() {
        public void execute(Connection connection) throws SQLException {
            connection.commit();
        }
    });
    session.close();

    for (int i = 0; i < writeReadTimes.length; i++) {
        DateTime writeReadTime = writeReadTimes[i];

        session = factory.openSession();
        EventTZ eventReread = (EventTZ) session.get(EventTZ.class, new Integer(i));

        assertNotNull("get failed - event#'" + i + "'not found", eventReread);
        assertNotNull("get failed - returned null", eventReread.getDateTime());

        assertEquals("get failed - returned different time", writeReadTime, eventReread.getDateTime());
    }

    session.close();
}

From source file:org.joda.time.contrib.hibernate.TestPersistentDateTimeAsBigInt.java

License:Apache License

public void testSimpleStore() throws SQLException {
    SessionFactory factory = getSessionFactory();

    Session session = factory.openSession();

    for (int i = 0; i < writeReadTimes.length; i++) {
        DateTime writeReadTime = writeReadTimes[i];

        ThingWithDateTime thing = new ThingWithDateTime();
        thing.setId(i);// w w w  .j  a va  2s .  co m
        thing.setDateTime(writeReadTime);

        session.save(thing);
    }

    session.flush();
    session.doWork(new Work() {
        public void execute(Connection connection) throws SQLException {
            connection.commit();
        }
    });
    session.close();

    for (int i = 0; i < writeReadTimes.length; i++) {
        DateTime writeReadTime = writeReadTimes[i];

        session = factory.openSession();
        ThingWithDateTime thingReread = (ThingWithDateTime) session.get(ThingWithDateTime.class,
                new Integer(i));

        assertNotNull("get failed - thing#'" + i + "'not found", thingReread);
        assertNotNull("get failed - returned null", thingReread.getDateTime());

        DateTime reReadTime = thingReread.getDateTime();
        if (writeReadTime.getMillis() != reReadTime.getMillis()) {
            fail("get failed - returned different date. expected " + writeReadTime + " was "
                    + thingReread.getDateTime());
        }
    }

    session.close();
}

From source file:org.joda.time.contrib.hibernate.TestPersistentDuration.java

License:Apache License

public void testSimpleStore() throws SQLException, IOException {
    Session session = getSessionFactory().openSession();

    for (int i = 0; i < durations.length; i++) {
        SomethingThatLasts thing = new SomethingThatLasts();
        thing.setId(i);/*  w ww  . ja  v  a  2 s  .  c  o  m*/
        thing.setName("test_" + i);
        thing.setTheDuration(durations[i]);
        session.save(thing);
    }

    session.flush();
    session.doWork(new Work() {
        public void execute(Connection connection) throws SQLException {
            connection.commit();
        }
    });
    session.close();

    for (int i = 0; i < durations.length; i++) {
        session = getSessionFactory().openSession();
        SomethingThatLasts lastingThing = (SomethingThatLasts) session.get(SomethingThatLasts.class,
                new Long(i));

        Assert.assertNotNull(lastingThing);
        Assert.assertEquals(i, lastingThing.getId());
        Assert.assertEquals("test_" + i, lastingThing.getName());
        Assert.assertEquals(durations[i], lastingThing.getTheDuration());

        session.close();
    }

    // printSqlQueryResults("SELECT * FROM lasting");
}

From source file:org.joda.time.contrib.hibernate.TestPersistentInstant.java

License:Apache License

public void testSimpleStore() throws SQLException {
    SessionFactory factory = getSessionFactory();

    Session session = factory.openSession();

    for (int i = 0; i < writeReadTimes.length; i++) {
        Instant writeReadTime = writeReadTimes[i];

        ThingWithInstant thing = new ThingWithInstant();
        thing.setId(i);/*from  w ww .j  ava2s  . co  m*/
        thing.setInstant(writeReadTime);

        session.save(thing);
    }

    session.flush();
    session.doWork(new Work() {
        public void execute(Connection connection) throws SQLException {
            connection.commit();
        }
    });
    session.close();

    for (int i = 0; i < writeReadTimes.length; i++) {
        Instant writeReadTime = writeReadTimes[i];

        session = factory.openSession();
        ThingWithInstant thingReread = (ThingWithInstant) session.get(ThingWithInstant.class, new Integer(i));

        assertNotNull("get failed - thing#'" + i + "'not found", thingReread);
        assertNotNull("get failed - returned null", thingReread.getInstant());

        Instant reReadTime = thingReread.getInstant();
        if (writeReadTime.getMillis() != reReadTime.getMillis()) {
            fail("get failed - returned different date. expected " + writeReadTime + " was "
                    + thingReread.getInstant());
        }
    }

    session.close();
}

From source file:org.joda.time.contrib.hibernate.TestPersistentLocalDate.java

License:Apache License

public void testSimpleStore() throws SQLException {
    SessionFactory factory = getSessionFactory();

    Session session = factory.openSession();

    for (int i = 0; i < writeReadTimes.length; i++) {
        LocalDate writeReadTime = writeReadTimes[i];

        Event event = new Event();
        event.setId(i);//from  w w  w .  j ava  2 s  . c  o  m
        event.setLocalDate(writeReadTime);

        session.save(event);
    }

    session.flush();
    session.doWork(new Work() {
        public void execute(Connection connection) throws SQLException {
            connection.commit();
        }
    });
    session.close();

    for (int i = 0; i < writeReadTimes.length; i++) {
        LocalDate writeReadTime = writeReadTimes[i];

        session = factory.openSession();
        Event eventReread = (Event) session.get(Event.class, new Integer(i));

        assertNotNull("get failed - event#'" + i + "'not found", eventReread);
        assertNotNull("get failed - returned null", eventReread.getLocalDate());

        // we loose the timezone, so we have to normalize both to offset=0
        assertEquals("get failed - returned different time", writeReadTime, eventReread.getLocalDate());

        session.close();
    }
}

From source file:org.joda.time.contrib.hibernate.TestPersistentLocalDateTime.java

License:Apache License

public void testSimpleStore() throws SQLException {
    SessionFactory factory = getSessionFactory();

    Session session = factory.openSession();

    for (int i = 0; i < writeReadTimes.length; i++) {
        LocalDateTime writeReadTime = writeReadTimes[i];

        Event event = new Event();
        event.setId(i);/*from  ww  w  .j ava  2s.  co  m*/
        event.setLocalDateTime(writeReadTime);

        session.save(event);
    }

    session.flush();
    session.doWork(new Work() {
        public void execute(Connection connection) throws SQLException {
            connection.commit();
        }
    });
    session.close();

    for (int i = 0; i < writeReadTimes.length; i++) {
        LocalDateTime writeReadTime = writeReadTimes[i];

        session = factory.openSession();
        Event eventReread = (Event) session.get(Event.class, new Integer(i));

        assertNotNull("get failed - event#'" + i + "'not found", eventReread);
        assertNotNull("get failed - returned null", eventReread.getLocalDateTime());

        // we loose the timezone, so we have to normalize both to offset=0
        assertEquals("get failed - returned different time", writeReadTime, eventReread.getLocalDateTime());

        session.close();
    }
}

From source file:org.joda.time.contrib.hibernate.TestPersistentLocalTime.java

License:Apache License

public void testSimpleStore() throws SQLException {
    SessionFactory factory = getSessionFactory();

    Session session = factory.openSession();

    for (int i = 0; i < writeReadTimes.length; i++) {
        LocalTime writeReadTime = writeReadTimes[i];

        Event event = new Event();
        event.setId(i);/*from w w  w .ja v a 2 s  .c  o m*/
        event.setLocalTime(writeReadTime);
        event.setLocalTime2(writeReadTime);
        event.setLocalTime3(writeReadTime);
        event.setLocalTime4(writeReadTime);

        session.save(event);
    }

    session.flush();
    session.doWork(new Work() {
        public void execute(Connection connection) throws SQLException {
            connection.commit();
        }
    });
    session.close();

    for (int i = 0; i < writeReadTimes.length; i++) {
        LocalTime writeReadTime = writeReadTimes[i];

        session = factory.openSession();
        Event eventReread = (Event) session.get(Event.class, new Integer(i));

        assertNotNull("get failed - event#'" + i + "'not found", eventReread);
        assertNotNull("get failed - returned null", eventReread.getLocalTime());
        assertNotNull("get failed - returned null", eventReread.getLocalTime2());
        assertNotNull("get failed - returned null", eventReread.getLocalTime3());
        assertNotNull("get failed - returned null", eventReread.getLocalTime4());

        // we might loose the millis, depends on database
        assertEquals("get failed - returned different time (TIME)", writeReadTime.getMillisOfDay() / 1000,
                eventReread.getLocalTime().getMillisOfDay() / 1000);

        assertEquals("get failed - returned different time (INT)", writeReadTime.getMillisOfDay(),
                eventReread.getLocalTime2().getMillisOfDay());

        assertEquals("get failed - returned different time (STRING)", writeReadTime.getMillisOfDay(),
                eventReread.getLocalTime3().getMillisOfDay());

        assertEquals("get failed - returned different time (TIMESTAMP)", writeReadTime.getMillisOfDay(),
                eventReread.getLocalTime4().getMillisOfDay());

        session.close();
    }
}