Search in sources :

Example 1 with AbstractWork

use of org.hibernate.jdbc.AbstractWork in project hibernate-orm by hibernate.

the class FooBarTest method testCache.

@Test
public void testCache() throws Exception {
    Session s = openSession();
    s.beginTransaction();
    Immutable im = new Immutable();
    s.save(im);
    s.getTransaction().commit();
    s.close();
    s = openSession();
    s.beginTransaction();
    s.load(im, im.getId());
    s.getTransaction().commit();
    s.close();
    final Session s2 = openSession();
    s2.beginTransaction();
    s2.load(im, im.getId());
    assertEquals("cached object identity", im, s2.createQuery("from Immutable im where im = ?").setParameter(0, im, s2.getTypeHelper().entity(Immutable.class)).uniqueResult());
    s2.doWork(new AbstractWork() {

        @Override
        public void execute(Connection connection) throws SQLException {
            Statement st = connection.createStatement();
            st.executeUpdate("delete from immut");
        }
    });
    s2.getTransaction().commit();
    s2.close();
}
Also used : SQLException(java.sql.SQLException) AbstractWork(org.hibernate.jdbc.AbstractWork) Statement(java.sql.Statement) Connection(java.sql.Connection) Session(org.hibernate.Session) Test(org.junit.Test)

Example 2 with AbstractWork

use of org.hibernate.jdbc.AbstractWork in project hibernate-orm by hibernate.

the class ParentChildTest method testLoadAfterNonExists.

@Test
public void testLoadAfterNonExists() throws HibernateException, SQLException {
    Session session = openSession();
    if ((getDialect() instanceof MySQLDialect) || (getDialect() instanceof IngresDialect)) {
        session.doWork(new AbstractWork() {

            @Override
            public void execute(Connection connection) throws SQLException {
                connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
            }
        });
    }
    session.getTransaction().begin();
    // First, prime the fixture session to think the entity does not exist
    try {
        session.load(Simple.class, new Long(-1));
        fail();
    } catch (ObjectNotFoundException onfe) {
        if (getDialect() instanceof TeradataDialect) {
            session.getTransaction().rollback();
            session.getTransaction().begin();
        }
    // this is correct
    }
    // Next, lets create that entity "under the covers"
    Session anotherSession = sessionFactory().openSession();
    anotherSession.beginTransaction();
    Simple myNewSimple = new Simple(Long.valueOf(-1));
    myNewSimple.setName("My under the radar Simple entity");
    myNewSimple.setAddress("SessionCacheTest.testLoadAfterNonExists");
    myNewSimple.setCount(1);
    myNewSimple.setDate(new Date());
    myNewSimple.setPay(Float.valueOf(100000000));
    anotherSession.save(myNewSimple);
    anotherSession.getTransaction().commit();
    anotherSession.close();
    // Now, lets make sure the original session can see the created row...
    session.clear();
    try {
        Simple dummy = (Simple) session.get(Simple.class, Long.valueOf(-1));
        assertNotNull("Unable to locate entity Simple with id = -1", dummy);
        session.delete(dummy);
    } catch (ObjectNotFoundException onfe) {
        fail("Unable to locate entity Simple with id = -1");
    }
    session.getTransaction().commit();
    session.close();
}
Also used : MySQLDialect(org.hibernate.dialect.MySQLDialect) SQLException(java.sql.SQLException) AbstractWork(org.hibernate.jdbc.AbstractWork) IngresDialect(org.hibernate.dialect.IngresDialect) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) Connection(java.sql.Connection) TeradataDialect(org.hibernate.dialect.TeradataDialect) Date(java.util.Date) Session(org.hibernate.Session) Test(org.junit.Test)

Example 3 with AbstractWork

use of org.hibernate.jdbc.AbstractWork in project hibernate-orm by hibernate.

the class MasterDetailTest method testCachedCollectionRefresh.

@Test
public void testCachedCollectionRefresh() throws Exception {
    if (isSerializableIsolationEnforced()) {
        SkipLog.reportSkip("SERIALIZABLE isolation", "cached collection refreshing");
        return;
    }
    Session s = openSession();
    s.beginTransaction();
    Category c = new Category();
    List list = new ArrayList();
    c.setSubcategories(list);
    list.add(new Category());
    c.setName("root");
    Serializable id = s.save(c);
    s.getTransaction().commit();
    s.close();
    s = openSession();
    s.beginTransaction();
    c = (Category) s.load(Category.class, id);
    //force load and cache
    c.getSubcategories().size();
    s.getTransaction().commit();
    s.close();
    s = openSession();
    if ((getDialect() instanceof MySQLDialect)) {
        s.doWork(new AbstractWork() {

            @Override
            public void execute(Connection connection) throws SQLException {
                connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
            }
        });
    }
    s.beginTransaction();
    c = (Category) s.load(Category.class, id);
    //force load
    c.getSubcategories().size();
    Session ss = openSession();
    ss.beginTransaction();
    Category c2 = (Category) ss.load(Category.class, id);
    ss.delete(c2.getSubcategories().get(0));
    c2.getSubcategories().clear();
    ss.getTransaction().commit();
    ss.close();
    s.refresh(c);
    assertTrue(c.getSubcategories().size() == 0);
    ss = openSession();
    ss.beginTransaction();
    c2 = (Category) ss.load(Category.class, id);
    c2.getSubcategories().add(new Category());
    c2.getSubcategories().add(new Category());
    ss.getTransaction().commit();
    ss.close();
    s.refresh(c);
    assertEquals(2, c.getSubcategories().size());
    s.getTransaction().commit();
    s.close();
    s = openSession();
    s.beginTransaction();
    c = (Category) s.load(Category.class, id);
    assertEquals(2, c.getSubcategories().size());
    s.delete(c);
    s.getTransaction().commit();
    s.close();
}
Also used : MySQLDialect(org.hibernate.dialect.MySQLDialect) Serializable(java.io.Serializable) SQLException(java.sql.SQLException) AbstractWork(org.hibernate.jdbc.AbstractWork) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) List(java.util.List) Session(org.hibernate.Session) Test(org.junit.Test)

Example 4 with AbstractWork

use of org.hibernate.jdbc.AbstractWork in project hibernate-orm by hibernate.

the class FooBarTest method testRefresh.

@SkipForDialect(value = AbstractHANADialect.class, comment = "HANA currently requires specifying table name by 'FOR UPDATE of t1.c1' if there are more than one tables/views/subqueries in the FROM clause")
@Test
public void testRefresh() throws Exception {
    final Session s = openSession();
    s.beginTransaction();
    Foo foo = new Foo();
    s.save(foo);
    s.flush();
    s.doWork(new AbstractWork() {

        @Override
        public void execute(Connection connection) throws SQLException {
            final String sql = "update " + getDialect().openQuote() + "foos" + getDialect().closeQuote() + " set long_ = -3";
            Statement st = connection.createStatement();
            st.executeUpdate(sql);
        }
    });
    s.refresh(foo);
    assertEquals(Long.valueOf(-3l), foo.getLong());
    assertEquals(LockMode.READ, s.getCurrentLockMode(foo));
    s.refresh(foo, LockMode.UPGRADE);
    if (getDialect().supportsOuterJoinForUpdate()) {
        assertEquals(LockMode.UPGRADE, s.getCurrentLockMode(foo));
    }
    s.delete(foo);
    s.getTransaction().commit();
    s.close();
}
Also used : SQLException(java.sql.SQLException) AbstractWork(org.hibernate.jdbc.AbstractWork) Statement(java.sql.Statement) Connection(java.sql.Connection) Session(org.hibernate.Session) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test)

Aggregations

Connection (java.sql.Connection)4 SQLException (java.sql.SQLException)4 Session (org.hibernate.Session)4 AbstractWork (org.hibernate.jdbc.AbstractWork)4 Test (org.junit.Test)4 Statement (java.sql.Statement)2 MySQLDialect (org.hibernate.dialect.MySQLDialect)2 Serializable (java.io.Serializable)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1 ObjectNotFoundException (org.hibernate.ObjectNotFoundException)1 IngresDialect (org.hibernate.dialect.IngresDialect)1 TeradataDialect (org.hibernate.dialect.TeradataDialect)1 SkipForDialect (org.hibernate.testing.SkipForDialect)1