This is a Spring Boot application demonstrating comprehensive entity auditing using both JaVers and Spring Data JPA Auditing. It provides a simple REST API for managing Post entities and allows retrieving their detailed change history.
- CRUD Operations: Standard RESTful endpoints for Creating, Reading, Updating, and Deleting 
Postentities. - JaVers Auditing: Automatic, fine-grained auditing of changes to 
Postentities (creations, updates, deletions) managed by JaVers.- Captures snapshots of entity state at each change.
 - Tracks changes to specific fields (
title,content). 
 - Spring Data JPA Auditing: Standard auditing for creation/modification metadata (
createdBy,creationDate,lastModifiedBy,lastModifiedDate) using Spring Data JPA's built-in features. - Audit History API: An endpoint (
GET /posts/{id}/histories) to retrieve the complete version history of a specificPost, showing changes, responsible user, and state at each version. - User Tracking: Integrates with Spring Security to automatically capture the username of the authenticated user performing the changes for both JaVers commits and JPA audit fields.
 - Custom Commit Metadata: Demonstrates adding custom metadata (e.g., 
tenant) to JaVers commits usingCommitPropertiesProvider. - DTO Pattern: Uses Data Transfer Objects (
PostDto,HistoryDto) for clean API contracts. - MapStruct Mapping: Leverages MapStruct for efficient and type-safe mapping between Entities and DTOs.
 - Spring Security Integration: Basic security setup with in-memory users for authentication.
 - Pagination: Supports paginated retrieval of 
Postentities (GET /posts/). - Clear Separation: Demonstrates how JaVers and Spring Data JPA Auditing can coexist, with JPA auditing fields ignored by JaVers (
@DiffIgnore). 
- Java (JDK 17+)
 - Spring Boot 3.x
 - Spring Data JPA
 - Spring Security
 - JaVers (Core, Spring Boot Starter Data JPA)
 - PostgreSQL (or any compatible SQL database)
 - MapStruct
 - Lombok
 - Maven / Gradle
 
- 
Clone the Repository:
git clone <your-repository-url> cd <repository-directory>
 - 
Database Setup:
- Ensure you have a PostgreSQL database running.
 - Create a database named 
mydatabase. - Create a user 
myuserwith passwordsecretand grant necessary permissions on themydatabase. - Alternatively, update the database connection details in 
src/main/resources/application.properties:spring.datasource.url=jdbc:postgresql://<your_db_host>:<your_db_port>/<your_db_name> spring.datasource.username=<your_db_user> spring.datasource.password=<your_db_password>
 - JaVers requires its own tables to store audit data. These tables (
jv_global_id,jv_commit,jv_commit_property,jv_snapshot) will be created or updated automatically on application startup ifspring.jpa.hibernate.ddl-autois set toupdateorcreate. 
 - 
Build the Application:
- Using Maven:
mvn clean install
 - Using Gradle:
./gradlew clean build
 
 - Using Maven:
 - 
Run the Application:
- Using Maven:
mvn spring-boot:run
 - Using Gradle:
./gradlew bootRun
 - Or run the JAR file:
java -jar target/javers-auditing-*.jar 
 - Using Maven:
 
The application will start on http://localhost:8080 (or the configured port).
The primary endpoints are under /posts:
POST /posts: Create a new post. Requires authentication.- Body: 
PostDto({"title": "...", "content": "..."}) 
- Body: 
 GET /posts/{id}: Get a specific post by ID. Requires authentication.PUT /posts/{id}: Update an existing post. Requires authentication.- Body: 
PostDto({"title": "...", "content": "..."}) 
- Body: 
 DELETE /posts/{id}: Delete a post by ID. Requires authentication.GET /posts/: Get a paginated list of posts. Requires authentication.- Supports standard Spring Data pagination parameters (e.g., 
?page=0&size=10&sort=title,asc). 
- Supports standard Spring Data pagination parameters (e.g., 
 GET /posts/{id}/histories: Get the audit history for a specific post. Requires authentication.- Returns a list of 
HistoryDto. 
- Returns a list of 
 
Authentication:
The application uses basic authentication or form login configured via Spring Security. Default users (defined in SecurityConfig):
- Username: 
Youssef, Password:1234 - Username: 
Admin, Password:Admin 
Use these credentials when prompted by your HTTP client (like curl, Postman, or your browser).
- JaVers: The 
PostRepois annotated with@JaversSpringDataAuditable, enabling JaVers to automatically interceptsave()anddeleteById()calls to create audit snapshots. TheJaversAuthorProviderbean fetches the current username from Spring Security for theauthorfield in JaVers commits. TheCommitPropertiesProvideradds extra context (like"tenant":"tenant-1") to each commit. - Spring Data JPA: The 
Postentity uses@EntityListeners(AuditingEntityListener.class)and fields annotated with@CreatedBy,@CreatedDate,@LastModifiedBy,@LastModifiedDate. TheSecurityAuditorAwarebean provides the username for the@CreatedByand@LastModifiedByfields. These fields are marked with@DiffIgnoreso JaVers doesn't track their changes directly, avoiding redundancy. - History Retrieval: The 
HistoryControlleruses theJaversHistoryServiceImplwhich queries the JaVers repository (javers.findSnapshots) to get the historicalCdoSnapshotobjects for a given entity instance and maps them toHistoryDto. 
List of Key Features:
- CRUD Operations: Full Create, Read, Update, Delete for 
Postentities via REST API. - JaVers Auditing: Automatic snapshotting of 
Postentity changes (Create, Update, Delete). - Spring Data JPA Auditing: Tracks creation/modification user and timestamp (
createdBy,creationDate, etc.). - Combined Auditing Strategy: Demonstrates using both JaVers (for detailed field changes) and JPA Auditing (for metadata) effectively.
 - Audit History API: Dedicated endpoint to retrieve the full version history of any 
Post. - User Attribution: Automatically records the authenticated user performing changes in both audit logs (JaVers author, JPA createdBy/lastModifiedBy).
 - Custom JaVers Metadata: Ability to add custom key-value pairs (like 
tenant) to JaVers audit commits. - DTO Pattern & MapStruct: Clean API contracts and efficient entity-DTO mapping.
 - Spring Security Integration: Basic authentication protecting API endpoints.
 - Pagination Support: Allows fetching lists of posts in pages.
 - Configuration Driven: Database and basic settings managed via 
application.properties. - Clear Code Structure: Well-organized packages for controllers, services, entities, repositories, mappers, and configuration.