{"id":37022772,"url":"https://github.com/lemick/hibernate-query-asserts","last_synced_at":"2026-01-14T02:43:24.397Z","repository":{"id":57726646,"uuid":"481723308","full_name":"Lemick/hibernate-query-asserts","owner":"Lemick","description":"Assert SQL statement count generated by Hibernate in Spring tests","archived":false,"fork":false,"pushed_at":"2025-02-08T20:45:29.000Z","size":77,"stargazers_count":15,"open_issues_count":3,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-21T07:49:14.778Z","etag":null,"topics":["hibernate","jpa","jpa-test","spring","spring-test","spring-test-web","sql","testing-tools"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Lemick.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-04-14T19:19:35.000Z","updated_at":"2025-06-11T07:27:37.000Z","dependencies_parsed_at":"2025-02-01T11:22:33.391Z","dependency_job_id":"687cf0ca-68ab-489c-991d-bf8df3431cb4","html_url":"https://github.com/Lemick/hibernate-query-asserts","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/Lemick/hibernate-query-asserts","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lemick%2Fhibernate-query-asserts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lemick%2Fhibernate-query-asserts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lemick%2Fhibernate-query-asserts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lemick%2Fhibernate-query-asserts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Lemick","download_url":"https://codeload.github.com/Lemick/hibernate-query-asserts/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lemick%2Fhibernate-query-asserts/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28408722,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T01:52:23.358Z","status":"online","status_checked_at":"2026-01-14T02:00:06.678Z","response_time":107,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hibernate","jpa","jpa-test","spring","spring-test","spring-test-web","sql","testing-tools"],"created_at":"2026-01-14T02:43:23.955Z","updated_at":"2026-01-14T02:43:24.381Z","avatar_url":"https://github.com/Lemick.png","language":"Java","readme":"# Hibernate SQL Query Assertions for Spring\n\nHibernate is a powerful ORM, but you need to have control over the executed SQL queries to avoid **huge performance problems** (N+1 selects, batch insert not working...) \n\nYou 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, it can assert L2C statistics too**.\n\nIt consists of just a Hibernate SQL inspector service and a Spring Test Listener that controls it (no proxy around the Datasource).\n\nThe assertion will work seamlessly whether you're testing Spring repositories or doing HTTP integration tests.\n\n## Examples\n\nA full-working demo of the examples below [is available here](https://github.com/Lemick/demo-hibernate-query-utils)\n\n*Tested versions*: Hibernate 5 \u0026 6 \n\n### Assert SQL statements declaratively\n\nYou 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 :\n\n```java\n@Test\n@Transactional\n@AssertHibernateSQLCount(inserts = 6)\nvoid create_two_blog_posts() {\n    BlogPost post_1 = new BlogPost(\"Blog post 1\");\n    post_1.addComment(new PostComment(\"Good article\"));\n    post_1.addComment(new PostComment(\"Very interesting\"));\n    blogPostRepository.save(post_1);\n\n    BlogPost post_2 = new BlogPost(\"Blog post 2\");\n    post_2.addComment(new PostComment(\"Nice\"));\n    post_2.addComment(new PostComment(\"So cool, thanks\"));\n    blogPostRepository.save(post_2);\n}\n```\nIf the actual count is different, an exception is thrown with the executed statements:\n```   \ncom.mickaelb.assertions.HibernateAssertCountException: \nExpected 5 INSERT but got 6:\n     =\u003e '/* insert com.lemick.demo.entity.BlogPost */ insert into blog_post (id, title) values (default, ?)'\n     =\u003e '/* insert com.lemick.demo.entity.PostComment */ insert into post_comment (id, blog_post_id, content) values (default, ?, ?)'\n     =\u003e '/* insert com.lemick.demo.entity.PostComment */ insert into post_comment (id, blog_post_id, content) values (default, ?, ?)'\n     =\u003e '/* insert com.lemick.demo.entity.BlogPost */ insert into blog_post (id, title) values (default, ?)'\n     =\u003e '/* insert com.lemick.demo.entity.PostComment */ insert into post_comment (id, blog_post_id, content) values (default, ?, ?)'\n     =\u003e '/* insert com.lemick.demo.entity.PostComment */ insert into post_comment (id, blog_post_id, content) values (default, ?, ?)'\n```\n\n### Assert SQL statements programmatically\n\nYou can also assert statements from several transactions in your test using the programmatic API:\n```java\n@Test\nvoid multiple_assertions_using_programmatic_api() {\n    QueryAssertions.assertInsertCount(2, () -\u003e {\n        BlogPost post_1 = new BlogPost(\"Blog post 1\");\n        post_1.addComment(new PostComment(\"Good article\"));\n        blogPostRepository.save(post_1);\n    });\n\n    QueryAssertions.assertSelectCount(1, () -\u003e blogPostRepository.findById(1L));\n\n    // Or even multiple asserts at once\n    QueryAssertions.assertStatementCount(Map.of(INSERT, 2, SELECT, 1), () -\u003e {\n        BlogPost post_1 = new BlogPost(\"Blog post 1\");\n        post_1.addComment(new PostComment(\"Good article\"));\n        blogPostRepository.save(post_1);\n\n        blogPostRepository.findById(1L);\n    });\n}\n```\n\n### Assert L2C statistics declaratively\n\nIt supports assertions on Hibernate level two cache statistics, useful for checking that your entities are cached correctly and that they will stay forever:\n```java\n@Test\n@AssertHibernateL2CCount(misses = 1, puts = 1, hits = 1)\nvoid create_one_post_and_read_it() {\n    doInTransaction(() -\u003e {\n        BlogPost post_1 = new BlogPost(\"Blog post 1\");\n        blogPostRepository.save(post_1);\n    });\n\n    doInTransaction(() -\u003e {\n        blogPostRepository.findById(1L); // 1 MISS + 1 PUT\n    });\n\n    doInTransaction(() -\u003e {\n        blogPostRepository.findById(1L); // 1 HIT\n    });\n}\n```\n## How to integrate\n1. Import the dependency\n    ```xml\n    \u003cdependency\u003e\n        \u003cgroupId\u003ecom.mickaelb\u003c/groupId\u003e\n        \u003cartifactId\u003ehibernate-query-asserts\u003c/artifactId\u003e\n        \u003cversion\u003e2.2.0\u003c/version\u003e\n    \u003c/dependency\u003e\n    ```\n2. Register the integration with Hibernate, you just need to add this key in your configuration (here for yml):\n\n        spring:\n          jpa:\n            properties:\n              hibernate.session_factory.statement_inspector: com.mickaelb.integration.hibernate.HibernateStatementInspector\n\n3. Register the Spring TestListener that will launch the SQL inspection if the annotation is present:\n\n    * By adding the listener on each of your integration test: \n\t```java\n   @SpringBootTest\n   @TestExecutionListeners(\n       listeners = HibernateAssertTestListener.class, \n       mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS\n   )\n   class MySpringIntegrationTest {\n      ...\n   }\n\t```\n\n    * **OR** by adding a **META-INF/spring.factories** file that contains the definition, that will register the listener for all your tests:\n\t```\n\torg.springframework.test.context.TestExecutionListener=com.mickaelb.integration.spring.HibernateAssertTestListener\n\t```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flemick%2Fhibernate-query-asserts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flemick%2Fhibernate-query-asserts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flemick%2Fhibernate-query-asserts/lists"}