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();
}
}
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();
}
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();
}
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());
}
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();
}
Aggregations