Search in sources :

Example 1 with EmptyInterceptor

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

the class InterceptorTest method testPrepareStatementFaultIntercept.

public void testPrepareStatementFaultIntercept() {
    final Interceptor interceptor = new EmptyInterceptor() {

        @Override
        public String onPrepareStatement(String sql) {
            return null;
        }
    };
    Session s = openSession(interceptor);
    try {
        Transaction t = s.beginTransaction();
        User u = new User("Kinga", "Mroz");
        s.persist(u);
        t.commit();
    } catch (TransactionException e) {
        assertTrue(e.getCause() instanceof AssertionFailure);
    } finally {
        s.close();
    }
}
Also used : TransactionException(org.hibernate.TransactionException) AssertionFailure(org.hibernate.AssertionFailure) Transaction(org.hibernate.Transaction) EmptyInterceptor(org.hibernate.EmptyInterceptor) Interceptor(org.hibernate.Interceptor) EmptyInterceptor(org.hibernate.EmptyInterceptor) Session(org.hibernate.Session)

Example 2 with EmptyInterceptor

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

the class InterceptorTest method testComponentInterceptor.

@Test
public void testComponentInterceptor() {
    final int checkPerm = 500;
    final String checkComment = "generated from interceptor";
    Session s = openSession(new EmptyInterceptor() {

        public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
            if (state[0] == null) {
                Image.Details detail = new Image.Details();
                detail.setPerm1(checkPerm);
                detail.setComment(checkComment);
                state[0] = detail;
            }
            return true;
        }
    });
    s.beginTransaction();
    Image i = new Image();
    i.setName("compincomp");
    i = (Image) s.merge(i);
    assertNotNull(i.getDetails());
    assertEquals(checkPerm, i.getDetails().getPerm1());
    assertEquals(checkComment, i.getDetails().getComment());
    s.getTransaction().commit();
    s.close();
    s = openSession();
    s.beginTransaction();
    i = (Image) s.get(Image.class, i.getId());
    assertNotNull(i.getDetails());
    assertEquals(checkPerm, i.getDetails().getPerm1());
    assertEquals(checkComment, i.getDetails().getComment());
    s.delete(i);
    s.getTransaction().commit();
    s.close();
}
Also used : Serializable(java.io.Serializable) EmptyInterceptor(org.hibernate.EmptyInterceptor) Type(org.hibernate.type.Type) Session(org.hibernate.Session) Test(org.junit.Test)

Example 3 with EmptyInterceptor

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

the class BatchedManyToManyTest method testLoadingNonInverseSide.

@Test
public void testLoadingNonInverseSide() {
    prepareTestData();
    sessionFactory().getStatistics().clear();
    CollectionStatistics userGroupStats = sessionFactory().getStatistics().getCollectionStatistics(User.class.getName() + ".groups");
    CollectionStatistics groupUserStats = sessionFactory().getStatistics().getCollectionStatistics(Group.class.getName() + ".users");
    Interceptor testingInterceptor = new EmptyInterceptor() {

        @Override
        public String onPrepareStatement(String sql) {
            // ugh, this is the best way I could come up with to assert this.
            // unfortunately, this is highly dependent on the dialect and its
            // outer join fragment.  But at least this wil fail on the majority
            // of dialects...
            Assert.assertFalse("batch load of many-to-many should use inner join", sql.toLowerCase(Locale.ROOT).contains("left outer join"));
            return super.onPrepareStatement(sql);
        }
    };
    Session s = openSession(testingInterceptor);
    s.beginTransaction();
    List users = s.createQuery("from User u").list();
    User user = (User) users.get(0);
    assertTrue(Hibernate.isInitialized(user));
    assertTrue(Hibernate.isInitialized(user.getGroups()));
    user = (User) users.get(1);
    assertTrue(Hibernate.isInitialized(user));
    assertTrue(Hibernate.isInitialized(user.getGroups()));
    // should have been just one fetch (the batch fetch)
    assertEquals(1, userGroupStats.getFetchCount());
    // should have been just one fetch (the batch fetch)
    assertEquals(1, groupUserStats.getFetchCount());
    s.getTransaction().commit();
    s.close();
}
Also used : List(java.util.List) EmptyInterceptor(org.hibernate.EmptyInterceptor) Interceptor(org.hibernate.Interceptor) EmptyInterceptor(org.hibernate.EmptyInterceptor) CollectionStatistics(org.hibernate.stat.CollectionStatistics) Session(org.hibernate.Session) Test(org.junit.Test)

Example 4 with EmptyInterceptor

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

the class InterceptorTest method testPrepareStatementIntercept.

@Test
@TestForIssue(jiraKey = "HHH-6594")
public void testPrepareStatementIntercept() {
    final Queue<String> expectedSQLs = new LinkedList<String>();
    // Transaction 1
    expectedSQLs.add("insert");
    // Transaction 2
    expectedSQLs.add("select");
    expectedSQLs.add("select");
    // Transaction 3
    expectedSQLs.add("select");
    expectedSQLs.add("select");
    expectedSQLs.add("update");
    // Transaction 4
    expectedSQLs.add("select");
    expectedSQLs.add("delete");
    final Interceptor interceptor = new EmptyInterceptor() {

        @Override
        public String onPrepareStatement(String sql) {
            assertNotNull(sql);
            String expectedSql = expectedSQLs.poll().toLowerCase(Locale.ROOT);
            assertTrue("sql:\n " + sql.toLowerCase(Locale.ROOT) + "\n doesn't start with \n" + expectedSql + "\n", sql.toLowerCase(Locale.ROOT).startsWith(expectedSql));
            return sql;
        }
    };
    Session s = openSession(interceptor);
    Transaction t = s.beginTransaction();
    User u = new User("Lukasz", "Antoniak");
    s.persist(u);
    t.commit();
    s.close();
    s = openSession(interceptor);
    t = s.beginTransaction();
    s.get(User.class, "Lukasz");
    s.createQuery("from User u").list();
    t.commit();
    s.close();
    u.setPassword("Kinga");
    s = openSession(interceptor);
    t = s.beginTransaction();
    s.merge(u);
    t.commit();
    s.close();
    s = openSession(interceptor);
    t = s.beginTransaction();
    s.delete(u);
    t.commit();
    s.close();
    assertTrue(expectedSQLs.isEmpty());
}
Also used : Transaction(org.hibernate.Transaction) EmptyInterceptor(org.hibernate.EmptyInterceptor) Interceptor(org.hibernate.Interceptor) EmptyInterceptor(org.hibernate.EmptyInterceptor) LinkedList(java.util.LinkedList) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 5 with EmptyInterceptor

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

the class InterceptorTest method testPropertyIntercept2.

/**
	 * Test case from HHH-1921.  Here the interceptor resets the
	 * current-state to the same thing as the current db state; this
	 * causes EntityPersister.findDirty() to return no dirty properties.
	 */
@Test
@TestForIssue(jiraKey = "HHH-1921")
public void testPropertyIntercept2() {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    User u = new User("Josh", "test");
    s.persist(u);
    t.commit();
    s.close();
    s = openSession(new EmptyInterceptor() {

        public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) {
            currentState[0] = "test";
            return true;
        }
    });
    t = s.beginTransaction();
    u = (User) s.get(User.class, u.getName());
    u.setPassword("nottest");
    t.commit();
    s.close();
    s = openSession();
    t = s.beginTransaction();
    u = (User) s.get(User.class, "Josh");
    assertEquals("test", u.getPassword());
    s.delete(u);
    t.commit();
    s.close();
}
Also used : Serializable(java.io.Serializable) Transaction(org.hibernate.Transaction) EmptyInterceptor(org.hibernate.EmptyInterceptor) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

EmptyInterceptor (org.hibernate.EmptyInterceptor)5 Session (org.hibernate.Session)5 Test (org.junit.Test)4 Interceptor (org.hibernate.Interceptor)3 Transaction (org.hibernate.Transaction)3 Serializable (java.io.Serializable)2 TestForIssue (org.hibernate.testing.TestForIssue)2 LinkedList (java.util.LinkedList)1 List (java.util.List)1 AssertionFailure (org.hibernate.AssertionFailure)1 TransactionException (org.hibernate.TransactionException)1 CollectionStatistics (org.hibernate.stat.CollectionStatistics)1 Type (org.hibernate.type.Type)1