{"id":3471,"date":"2025-12-24T10:01:32","date_gmt":"2025-12-24T15:01:32","guid":{"rendered":"https:\/\/www.mymiller.name\/wordpress\/?p=3471"},"modified":"2025-12-24T10:01:32","modified_gmt":"2025-12-24T15:01:32","slug":"spring-jpa-auditing","status":"publish","type":"post","link":"https:\/\/www.mymiller.name\/wordpress\/spring_databases\/spring-jpa-auditing\/","title":{"rendered":"Spring JPA Auditing"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Simplifying Entity Auditing with Spring Data JPA Annotations<\/h2>\n\n\n\n<p>Keeping track of who created and modified your entities is crucial for various purposes, including audit trails, security, and data lineage. Spring Data JPA offers a convenient and efficient way to achieve this through dedicated annotations: <code>@CreatedBy<\/code>, <code>@LastModifiedBy<\/code>, <code>@CreatedDate<\/code>, and <code>@LastModifiedDate<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Leveraging Annotations<\/h3>\n\n\n\n<p>Let&#8217;s consider a simple <code>Product<\/code> entity:<\/p>\n\n\n\n<p>Java<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Entity\npublic class Product {\n\n    @Id\n    @GeneratedValue(strategy = GenerationType.IDENTITY)\n    private Long id;\n\n    private String name;\n\n    @CreatedBy\n    private String createdBy;\n\n    @LastModifiedBy\n    private String lastModifiedBy;\n\n    @CreatedDate\n    private Date createdDate;\n\n    @LastModifiedDate\n    private Date lastModifiedDate;\n\n    \/\/ Getters and Setters\n}\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>@CreatedBy and @LastModifiedBy:<\/strong> These annotations mark fields that will hold the username or identifier of the user who created and last modified the entity, respectively.<\/li>\n\n\n\n<li><strong>@CreatedDate and @LastModifiedDate:<\/strong> These annotations mark fields that will automatically store the creation and last modification timestamps. Spring Data JPA automatically injects the current user and timestamp during entity persistence and updates.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Maven and Gradle Dependencies<\/h3>\n\n\n\n<p>To utilize these annotations, you need the <code>spring-data-jpa<\/code> dependency:<\/p>\n\n\n\n<p><strong>Maven:<\/strong><\/p>\n\n\n\n<p>XML<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-data-jpa&lt;\/artifactId&gt;\n&lt;\/dependency&gt;\n<\/code><\/pre>\n\n\n\n<p><strong>Gradle:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>implementation 'org.springframework.boot:spring-data-jpa'\n<\/code><\/pre>\n\n\n\n<p><strong>Optional: Enabling Auditing<\/strong><\/p>\n\n\n\n<p>Spring Data JPA auditing is enabled by default, but if explicitly desired, add the following configuration class:<\/p>\n\n\n\n<p>Java<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Configuration\n@EnableJpaAuditing\npublic class AuditingConfig {\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">How it Works<\/h3>\n\n\n\n<p>Behind the scenes, Spring Data JPA utilizes an <code>AuditingEntityListener<\/code> that intercepts entity lifecycle events like save and update. It retrieves the current user information (typically from the security context) and the current timestamp to populate the annotated fields before persisting the entity.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Additional Notes<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The annotations support various field types like <code>String<\/code>, <code>Long<\/code>, and <code>Date<\/code>. Consider using specific user identifier types instead of usernames for better data integrity.<\/li>\n\n\n\n<li>You can customize the behavior of these annotations by implementing an <code>AuditorAware<\/code> interface and providing a custom logic to determine the user information.<\/li>\n<\/ul>\n\n\n\n<p>In conclusion, these annotations provide a straightforward approach to maintain audit trails and user-related information within your Spring Data JPA entities. By leveraging them effectively, you can simplify data management and enhance the traceability of your application.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Simplifying Entity Auditing with Spring Data JPA Annotations Keeping track of who created and modified your entities is crucial for various purposes, including audit trails, security, and data lineage. Spring Data JPA offers a convenient and efficient way to achieve this through dedicated annotations: @CreatedBy, @LastModifiedBy, @CreatedDate, and @LastModifiedDate. Leveraging Annotations Let&#8217;s consider a simple [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3474,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[435],"tags":[319],"series":[397],"class_list":["post-3471","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-spring_databases","tag-spring","series-spring"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/02\/audit-4190944_640.jpg?fit=640%2C377&ssl=1","jetpack-related-posts":[{"id":3530,"url":"https:\/\/www.mymiller.name\/wordpress\/spring_databases\/spring-jpa-auditing-track-data-changes\/","url_meta":{"origin":3471,"position":0},"title":"Spring JPA Auditing: Track Data Changes","author":"Jeffery Miller","date":"April 20, 2026","format":false,"excerpt":"In the dynamic world of software development, understanding the complete history of your data is crucial. Who made a change? When did it occur? Who viewed the data? Spring JPA Auditing, combined with custom solutions, offers a comprehensive way to answer these questions, acting as a time machine for your\u2026","rel":"","context":"In &quot;Spring Databases&quot;","block_context":{"text":"Spring Databases","link":"https:\/\/www.mymiller.name\/wordpress\/category\/spring_databases\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/image.png?fit=1200%2C686&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/image.png?fit=1200%2C686&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/image.png?fit=1200%2C686&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/image.png?fit=1200%2C686&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/image.png?fit=1200%2C686&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":3502,"url":"https:\/\/www.mymiller.name\/wordpress\/spring_databases\/spring-data-jpa-for-dummies-persisting-data-like-a-pro\/","url_meta":{"origin":3471,"position":1},"title":"Spring Data JPA for Dummies: Persisting Data Like a Pro","author":"Jeffery Miller","date":"December 24, 2025","format":false,"excerpt":"Ever feel bogged down by writing tons of code just to interact with your database? If you're a Java developer working with relational databases, Spring Data JPA is your new best friend. This blog post will give you a beginner-friendly introduction to Spring Data JPA, showing you how to save\u2026","rel":"","context":"In &quot;Spring Databases&quot;","block_context":{"text":"Spring Databases","link":"https:\/\/www.mymiller.name\/wordpress\/category\/spring_databases\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/binary-2904980_1280.jpg?fit=1200%2C674&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/binary-2904980_1280.jpg?fit=1200%2C674&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/binary-2904980_1280.jpg?fit=1200%2C674&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/binary-2904980_1280.jpg?fit=1200%2C674&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/binary-2904980_1280.jpg?fit=1200%2C674&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":3539,"url":"https:\/\/www.mymiller.name\/wordpress\/spring_databases\/spring-data-cassandra-simplifying-java-development-with-apache-cassandra\/","url_meta":{"origin":3471,"position":2},"title":"Spring Data Cassandra: Simplifying Java Development with Apache Cassandra","author":"Jeffery Miller","date":"September 22, 2025","format":false,"excerpt":"Apache Cassandra is a powerful NoSQL database known for its scalability and high availability. Spring Data Cassandra seamlessly integrates Spring\u2019s familiar programming model with Cassandra, boosting developer productivity. Why Spring Data Cassandra? Simplified Configuration: Spring Boot auto-configuration minimizes manual setup. Object-Relational Mapping (ORM): Easily map Java objects to Cassandra tables.\u2026","rel":"","context":"In &quot;Spring Databases&quot;","block_context":{"text":"Spring Databases","link":"https:\/\/www.mymiller.name\/wordpress\/category\/spring_databases\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/network-3396348_1280.jpg?fit=1200%2C720&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/network-3396348_1280.jpg?fit=1200%2C720&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/network-3396348_1280.jpg?fit=1200%2C720&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/network-3396348_1280.jpg?fit=1200%2C720&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/network-3396348_1280.jpg?fit=1200%2C720&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":3820,"url":"https:\/\/www.mymiller.name\/wordpress\/spring_rest\/effortless-api-creation-generating-crud-endpoints-with-spring-data-rest\/","url_meta":{"origin":3471,"position":3},"title":"Effortless API Creation: Generating CRUD Endpoints with Spring Data REST","author":"Jeffery Miller","date":"December 24, 2025","format":false,"excerpt":"Building RESTful APIs for your data can often feel like a repetitive task. Defining endpoints, handling HTTP methods (GET, POST, PUT, DELETE), serializing and deserializing data \u2013 it all adds up. But what if you could significantly reduce this boilerplate and focus on your core domain logic? Enter Spring Data\u2026","rel":"","context":"In &quot;Spring Rest&quot;","block_context":{"text":"Spring Rest","link":"https:\/\/www.mymiller.name\/wordpress\/category\/spring_rest\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/05\/ai-generated-8041774_640.jpg?fit=640%2C479&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/05\/ai-generated-8041774_640.jpg?fit=640%2C479&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/05\/ai-generated-8041774_640.jpg?fit=640%2C479&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":3692,"url":"https:\/\/www.mymiller.name\/wordpress\/spring_aop\/tracking-method-access-in-spring-aop-security-and-jpa\/","url_meta":{"origin":3471,"position":4},"title":"Tracking Method Access in Spring: AOP, Security, and JPA","author":"Jeffery Miller","date":"November 20, 2025","format":false,"excerpt":"In the world of Spring applications, understanding how your methods are accessed can be crucial for various reasons like monitoring usage patterns, auditing security, or simply gathering insights into your application\u2019s behavior. Let\u2019s explore a powerful approach to track method access using Spring AOP (Aspect-Oriented Programming), Spring Security, and Spring\u2026","rel":"","context":"In &quot;Spring AOP&quot;","block_context":{"text":"Spring AOP","link":"https:\/\/www.mymiller.name\/wordpress\/category\/spring_aop\/"},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/09\/money-3523131_1280-jpg.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/09\/money-3523131_1280-jpg.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/09\/money-3523131_1280-jpg.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/09\/money-3523131_1280-jpg.avif 2x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/09\/money-3523131_1280-jpg.avif 3x"},"classes":[]},{"id":3925,"url":"https:\/\/www.mymiller.name\/wordpress\/spring_databases\/spring-data-jpa-java-records-the-ultimate-duo-for-clean-fast-query-projections\/","url_meta":{"origin":3471,"position":5},"title":"Spring Data JPA &#038; Java Records: The Ultimate Duo for Clean, Fast Query Projections","author":"Jeffery Miller","date":"November 20, 2025","format":false,"excerpt":"Architecting Efficient Data Access with Immutability As Spring developers, we spend a significant amount of time optimizing the path between the database and the client. One of the most common performance pitfalls is the over-fetching of data\u2014loading entire, complex JPA entities when all we need is a small subset of\u2026","rel":"","context":"In &quot;Spring Databases&quot;","block_context":{"text":"Spring Databases","link":"https:\/\/www.mymiller.name\/wordpress\/category\/spring_databases\/"},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/11\/record-shop-9180482_1280.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/11\/record-shop-9180482_1280.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/11\/record-shop-9180482_1280.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/11\/record-shop-9180482_1280.avif 2x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2025\/11\/record-shop-9180482_1280.avif 3x"},"classes":[]}],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/3471","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/comments?post=3471"}],"version-history":[{"count":1,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/3471\/revisions"}],"predecessor-version":[{"id":3472,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/3471\/revisions\/3472"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media\/3474"}],"wp:attachment":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media?parent=3471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/categories?post=3471"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/tags?post=3471"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/series?post=3471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}