use of com.vladmihalcea.sql.exception.SQLSelectCountMismatchException in project vladmihalcea.wordpress.com by vladmihalcea.
the class HibernateSQLStatementCountTest method test.
@Test
public void test() {
transactionTemplate.execute(new TransactionCallback<Void>() {
@Override
public Void doInTransaction(TransactionStatus transactionStatus) {
Company company = new Company();
company.setName("TV Company");
entityManager.persist(company);
Product product1 = new Product("tvCode");
product1.setName("TV");
product1.setCompany(company);
WarehouseProductInfo warehouseProductInfo1 = new WarehouseProductInfo();
warehouseProductInfo1.setQuantity(101);
product1.addWarehouse(warehouseProductInfo1);
Product product2 = new Product("cdCode");
product2.setName("CD");
product2.setCompany(company);
WarehouseProductInfo warehouseProductInfo2 = new WarehouseProductInfo();
warehouseProductInfo2.setQuantity(50);
product2.addWarehouse(warehouseProductInfo2);
entityManager.persist(product1);
entityManager.persist(product2);
entityManager.flush();
final JdbcTemplate otherDataSourceJdbcTemplate = new JdbcTemplate(otherDataSource);
List<Map<String, Object>> versions = otherDataSourceJdbcTemplate.queryForList(" select * from version ");
assertTrue(versions.isEmpty());
return null;
}
});
try {
SQLStatementCountValidator.reset();
warehouseProductInfoService.findAllWithNPlusOne();
SQLStatementCountValidator.assertSelectCount(1);
} catch (SQLSelectCountMismatchException e) {
assertEquals(3, e.getRecorded());
}
SQLStatementCountValidator.reset();
warehouseProductInfoService.findAllWithFetch();
SQLStatementCountValidator.assertSelectCount(1);
SQLStatementCountValidator.reset();
warehouseProductInfoService.newWarehouseProductInfo();
SQLStatementCountValidator.assertSelectCount(1);
SQLStatementCountValidator.assertInsertCount(2);
}
Aggregations