{"id":18938002,"url":"https://github.com/davidarchanjo/spring-boot-jpa-auditing","last_synced_at":"2025-07-23T05:37:34.417Z","repository":{"id":119392160,"uuid":"385636618","full_name":"davidarchanjo/spring-boot-jpa-auditing","owner":"davidarchanjo","description":"Sample Spring Boot project for implementing JPA database auditing","archived":false,"fork":false,"pushed_at":"2021-08-29T17:29:47.000Z","size":1439,"stargazers_count":5,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-15T20:46:26.054Z","etag":null,"topics":["database","java","spring-boot","spring-jpa"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/davidarchanjo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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,"zenodo":null}},"created_at":"2021-07-13T14:41:21.000Z","updated_at":"2024-07-11T23:51:34.000Z","dependencies_parsed_at":null,"dependency_job_id":"5ce6bf35-ac8c-4936-b3d0-229fc91d92a7","html_url":"https://github.com/davidarchanjo/spring-boot-jpa-auditing","commit_stats":null,"previous_names":[],"tags_count":0,"template":true,"template_full_name":null,"purl":"pkg:github/davidarchanjo/spring-boot-jpa-auditing","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidarchanjo%2Fspring-boot-jpa-auditing","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidarchanjo%2Fspring-boot-jpa-auditing/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidarchanjo%2Fspring-boot-jpa-auditing/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidarchanjo%2Fspring-boot-jpa-auditing/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/davidarchanjo","download_url":"https://codeload.github.com/davidarchanjo/spring-boot-jpa-auditing/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidarchanjo%2Fspring-boot-jpa-auditing/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266625186,"owners_count":23958304,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-23T02:00:09.312Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"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":["database","java","spring-boot","spring-jpa"],"created_at":"2024-11-08T12:13:10.693Z","updated_at":"2025-07-23T05:37:34.408Z","avatar_url":"https://github.com/davidarchanjo.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Spring Boot + JPA Database Auditing\n![background](./assets/banner.jpg)\n\n## Overview\n[Spring Data JPA](https://spring.io/projects/spring-data-jpa) provides some set of convenient and useful annotations for database auditing. It provides a way to keep a track of **who did the change** and **when it was done**.\nThe auditing functionality is provided out of the box by the usage of the following four annotations:\n- `@CreatedBy`\n- `@CreatedDate`\n- `@LastModifiedBy`\n- `@LastModifiedDate`\n\nThis repository is intended to be a reference for implementing database auditing with a focus on the settings and code needed to make it work in any SpringBoot application backed by any database (as long as it is supported by Spring JPA itself). Some development best practices were not taken into account in favor of objectivity and simplicity. .\n\n\n## Prerequisites\n- Maven 3+\n- Java 8+ \n\n\n## Tech Stack\n- [Spring Boot 2.3.1.RELEASE](https://spring.io/blog/2020/06/12/spring-boot-2-3-1-available-now)\n- [Spring Web MVC](https://docs.spring.io/spring-framework/docs/current/reference/html/web.html)\n- [Spring Data JPA](https://spring.io/projects/spring-data-jpa)\n- [H2 Database](https://www.h2database.com/html/main.html)\n- [Project Lombok](https://projectlombok.org/)\n\n\n## Booting Up the Application\n````bash\n$ mvn spring-boot:run\n````\nThe application will be accepting request at http://localhost:8080/users, and the H2 Database Console's login page will available at http://localhost:8080/h2-console according to the screenshot below:\n![h2_login_page](./assets/h2_login_page.jpg)\n\n\n## Testing\n\nWith the application up and running, lets test the **JPA Auditing** capabilities. I'm using [Postman](https://www.postman.com/) for demonstration and I made available the [postman_collection](./postman_collection.json) I used so you can follow along easily. Check [here](https://learning.postman.com/docs/getting-started/importing-and-exporting-data/#importing-data-into-postman) for more information on how import a collection into Postman if needed.\n\n- Lets create a user:\n  ![create_user](./assets/create_user.jpg)\n  ![h2_create_user](./assets/h2_create_user.jpg)\n  As for the snapshots, a new user was created successfully on the database and we can check from the [H2 Console](http://localhost:8080/h2-console) the fields `CREATED_BY`, `CREATED_DATE`, `LAST_MODIFIED_BY`, `LAST_MODIFIED_DATE` were populated dynamically by JPA with auditing data. From those fields it can be verified who was responsible for creating the record and when this action happened.\n  \n  \n- Now, lets simulate a user profile update by changing the `lastName` and `email` values from the previously created user and see what happens:\n  ![update_user](./assets/update_user.jpg)\n  ![h2_update_user](./assets/h2_update_user.jpg)\n  As outlined from the [H2 Console](http://localhost:8080/h2-console), the `LAST_MODIFIED_DATE` field value has been updated, thus reflecting that the record has been modified since its creation by user `davidarchanjo`. \n\n\n## Notes\nFor simplicity's sake, in the [JpaAuditingConfiguration](./src/main/java/br/com/darchanjo/examples/jpa/auditing/config/JpaAuditingConfiguration.java) configuration bean I'm hard-coding the user `davidarchanjo`, as the \"fake\" logged user, who turns to be used as the responsible for inserting and updating data into the database. If [Spring Security](https://spring.io/projects/spring-security) is being used, we could get the real logged user's name by calling: \n```\nSecurityContextHolder.getContext().getAuthentication().getName()\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavidarchanjo%2Fspring-boot-jpa-auditing","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdavidarchanjo%2Fspring-boot-jpa-auditing","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavidarchanjo%2Fspring-boot-jpa-auditing/lists"}