Skip to content

Commit a3f0c1d

Browse files
authored
Update README.md
1 parent c3b3fbd commit a3f0c1d

File tree

1 file changed

+23
-33
lines changed

1 file changed

+23
-33
lines changed

README.md

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,43 @@
11
# Hibernate SQL query count assertions for Spring
22

3-
Hibernate is a powerful ORM, but you need to have control over the executed SQL queries to avoid huge performance problems (N+1 selects, silent updates, batch insert not working, etc...)
3+
Hibernate is a powerful ORM, but you need to have control over the executed SQL queries to avoid **huge performance problems** (N+1 selects, silent updates, batch insert not working...)
44

5-
You can enable SQL query logging, this is a great help in dev, but not in production.
5+
You can enable SQL query logging, this is a great help in dev, but not in production. This tool helps you to count the **executed SQL queries by Hibernate in your integration tests**.
66

7-
This tool helps you to count the real executed queries by Hibernate in your integration tests.
7+
It consists of just an Hibernate SQL inspector service and a Spring Test Listener that controls it (no proxy around the Datasource).
88

9-
It consists of just an Hibernate SQL inspector service and a Spring Test Listener that controls it (no proxy around the Datasource)
9+
The assertion will work seamlessly whether you're testing Spring repositories or doing HTTP integration tests.
1010

1111
## Example
1212

13-
* You just have to add the @AssertSQLStatementCount annotation to your test and it will verify the SQL statements count at the end of the test:
13+
* You just have to add the @AssertHibernateSQLCount annotation to your test and it will verify the SQL statements (SELECT, UPDATE, INSERT, DELETE) count at the end of the test :
1414

1515

1616
@Test
1717
@Transactional
18-
@AssertSQLStatementCount(deletes = 1, inserts = 2)
19-
void remove_one_entity_and_create_two() {
20-
blogPostRepository.deleteById(1L);
21-
22-
BlogPost post_2 = new BlogPost("Post title 2");
18+
@AssertHibernateSQLCount(inserts = 6)
19+
void create_two_blog_posts() {
20+
BlogPost post_1 = new BlogPost("Blog post 1");
21+
post_1.addComment(new PostComment("Good article"));
22+
post_1.addComment(new PostComment("Very interesting"));
23+
blogPostRepository.save(post_1);
24+
25+
BlogPost post_2 = new BlogPost("Blog post 2");
26+
post_2.addComment(new PostComment("Nice"));
27+
post_2.addComment(new PostComment("So cool, thanks"));
2328
blogPostRepository.save(post_2);
24-
25-
BlogPost post_3 = new BlogPost("Post title 3");
26-
blogPostRepository.save(post_3);
2729
}
2830

29-
If the actual count is different, an exception is thrown:
31+
If the actual count is different, an exception is thrown with the executed statements:
3032

3133
com.lemick.assertions.HibernateStatementCountException:
32-
Expected 2 INSERT but was 1
33-
Expected 1 DELETE but was 0
34-
35-
* You can also use the static methods in your test method, but it's more complex because Hibernate [will try to delay the flush](https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/chapters/flushing/Flushing.html) of your entities states at the end of your application transaction, here we call flush manually:
36-
37-
@Test
38-
@Transactional
39-
void create_two_entities() {
40-
BlogPost post_1 = new BlogPost("Post title 1");
41-
blogPostRepository.save(post_1);
42-
entityManager.flush();
43-
assertInsertStatementCount(1);
44-
45-
BlogPost post_2 = new BlogPost("Post title 2");
46-
blogPostRepository.save(post_2);
47-
entityManager.flush();
48-
assertInsertStatementCount(2);
49-
}
34+
Expected 5 INSERT but got 6:
35+
=> '/* insert com.lemick.testdatasourceproxy.entity.BlogPost */ insert into blog_post (id, title) values (default, ?)'
36+
=> '/* insert com.lemick.testdatasourceproxy.entity.PostComment */ insert into post_comment (id, blog_post_id, content) values (default, ?, ?)'
37+
=> '/* insert com.lemick.testdatasourceproxy.entity.PostComment */ insert into post_comment (id, blog_post_id, content) values (default, ?, ?)'
38+
=> '/* insert com.lemick.testdatasourceproxy.entity.BlogPost */ insert into blog_post (id, title) values (default, ?)'
39+
=> '/* insert com.lemick.testdatasourceproxy.entity.PostComment */ insert into post_comment (id, blog_post_id, content) values (default, ?, ?)'
40+
=> '/* insert com.lemick.testdatasourceproxy.entity.PostComment */ insert into post_comment (id, blog_post_id, content) values (default, ?, ?)'
5041

5142
## How to integrate
5243
1. Register the integration with Hibernate, you just need to add this key in your configuration (here for yml):
@@ -72,4 +63,3 @@ It consists of just an Hibernate SQL inspector service and a Spring Test Listene
7263
* **OR** by adding a **META-INF/spring.factories** file that contains the definition, that will register the listener for all your tests:
7364

7465
org.springframework.test.context.TestExecutionListener=com.lemick.integration.spring.HibernateStatementCountTestListener
75-

0 commit comments

Comments
 (0)