present (sb for a job etc)
In the dynamic world of Spring Boot development, ensuring data integrity and tracking changes is critical. A powerful tool to accomplish this is the @Audited annotation. This article delves into the intricacies of this annotation, its purpose, steps to implement it, and how to utilize its functionality for effective entity auditing.
Understanding @Audited
The @Audited annotation in Spring Boot is used to audit entities, providing a detailed record of changes to data over time. This is valuable in situations where you need to track changes, user actions, or compliance requirements.
Implementation steps
1. dependencies: to include @Audited, you need to add the spring-data-envers dependency to your project. Make sure yours or reflect this addition.
<!-- Maven Dependency -->
<dependency>
<groupId></groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId></groupId>
<artifactId>hibernate-envers</artifactId>
</dependency>
The spring-boot-starter-data-jpa dependency contains the components required for data access using Spring Data JPA. However, if you specifically want to enable entity auditing in Spring Boot using the @Audited annotation, you also need to include the hibernate-envers dependency. This dependency adds support for Hibernate Envers, which are the tools responsible for entity version control and auditing.
2. Entity Configuration: Apply the @Audited annotation to the entity class you want to audit.
import ;
@Entity
@Audited
public class YourEntity {
// Your entity fields and methods
}
3. Configuration: Ensure that your or contains the required configuration for Hibernate Envers.
spring:
data:
jpa:
repositories:
enabled: true
auditing:
enabled: true
4. Audit table fields: The audit table generated by Hibernate Envers typically includes fields such as REV (revision number), REVTYPE (revision type), and AUDIT_TIMESTAMP (audit timestamp). Together, these fields store the history of changes made to the audited entity.
Spring Boot automatically creates audit tables (e.g., 'YourEntity_AUD') to store metadata.
Explore the fields in the audit table:
- REV: Revision number (incremental)
- REVTYPE: revision type (insert, update, delete)
- AUDITEDFIELD: Audited field value
- MODIFIEDBY: the user who made the change
- MODIFIEDDATE: Modify date and time
5. Retrieve audit data: Use the Spring Data JPA repository to query audit history.
import ;
import ;
import ;
public interface YourEntityAuditRepository extends RevisionRepository<YourEntity, Long, Integer> {
List<Revision<Integer, YourEntity>> findRevisionsById(Long entityId);
}
In this example:
- YourEntityAuditRepository extends RevisionRepository, a Spring Data JPA interface for handling revisions.
- The findRevisionsById method allows you to retrieve all revisions for the entity with the specified ID.
You can then use this repository to query the audit history in the service or controller:
import ;
import ;
import ;
@Service
public class AuditService {
private final YourEntityAuditRepository entityAuditRepository;
@Autowired
public AuditService(YourEntityAuditRepository entityAuditRepository) {
= entityAuditRepository;
}
public List<Revision<Integer, YourEntity>> getEntityRevisions(Long entityId) {
return (entityId);
}
}
Another example
Use Hibernate Envers to query the audit history of a specific entity with a given ID
List<YourEntity_AUD> revisions = (, entityld);
- auditReader: an AuditReader instance, provided by Hibernate Envers. It allows you to interact with an entity's audit history.
- findRevisions: method provided by Hibernate Envers to retrieve all revisions for a given entity with a specified ID.
- : The entity class whose audit history you want to retrieve.
- entityId: the specific ID of the entity whose revision you want to get.
- List<YourEntity_AUD>: the result is a list of audited entities (YourEntity_AUD), where each entry in the list represents a revision of the entity.
In Hibernate Envers, when you enable auditing for an entity, it generates a corresponding audit entity with the "_AUD" suffix (by default). This audit entity tracks all changes to the original entity over time.
Thus, this line of code is essentially querying the audit history of all revisions of an entity with a given ID and storing the results in a list of audited entities. This list can then be used to analyze or display the entity's changes across revisions.
Welcome to my public number: program ape DD. the first time to understand the cutting-edge industry news, share in-depth technical dry goods, access to high-quality learning resources