-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
using hibernate-envers add V3__7_revision_info_schema.sql add V4__7_customer_audit_schema.sql add V5__7_customer_address_audit_schema.sql resolves: #7
- Loading branch information
1 parent
93dfade
commit 71fb9ea
Showing
14 changed files
with
313 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/main/java/com/cheise_proj/auditing/AuditConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.cheise_proj.auditing; | ||
|
||
import jakarta.persistence.EntityManagerFactory; | ||
import org.hibernate.envers.AuditReader; | ||
import org.hibernate.envers.AuditReaderFactory; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class AuditConfiguration { | ||
|
||
private final EntityManagerFactory entityManagerFactory; | ||
|
||
AuditConfiguration(EntityManagerFactory entityManagerFactory) { | ||
this.entityManagerFactory = entityManagerFactory; | ||
} | ||
|
||
@Bean | ||
AuditReader auditReader() { | ||
return AuditReaderFactory.get(entityManagerFactory.createEntityManager()); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/main/java/com/cheise_proj/auditing/AuditRevisionEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.cheise_proj.auditing; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import jakarta.persistence.*; | ||
import lombok.*; | ||
import org.hibernate.envers.RevisionEntity; | ||
import org.hibernate.envers.RevisionNumber; | ||
import org.hibernate.envers.RevisionTimestamp; | ||
|
||
import java.util.*; | ||
|
||
@Entity | ||
@Table(name = "revision_info") | ||
@RevisionEntity(AuditRevisionListener.class) | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Getter | ||
@Setter | ||
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) | ||
@Builder | ||
class AuditRevisionEntity { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "revision_seq") | ||
@SequenceGenerator( | ||
name = "revision_seq", | ||
sequenceName = "seq_revision_id" | ||
) | ||
@RevisionNumber | ||
private int id; | ||
|
||
@Column(name = "revision_date") | ||
@Temporal(TemporalType.TIMESTAMP) | ||
@RevisionTimestamp | ||
private Date date; | ||
|
||
@Column(name = "user_name") | ||
private String userName; | ||
|
||
static List<Map<String, Object>> toRevisionResults(List<Object> results) { | ||
List<Map<String, Object>> revisions = new ArrayList<>(); | ||
|
||
for (Object result : results) { | ||
if (result instanceof Object[] resultArray) { | ||
Map<String, Object> revisionData = new HashMap<>(); | ||
revisionData.put("entity", resultArray[0]); | ||
revisionData.put("revision", resultArray[1]); | ||
revisionData.put("revisionType", resultArray[2]); | ||
if (resultArray.length > 3) { | ||
revisionData.put("changes", resultArray[3]); | ||
} | ||
revisions.add(revisionData); | ||
} | ||
else if (result instanceof Object resultMap) { | ||
Map<String, Object> revisionData = new HashMap<>(); | ||
revisionData.put("revision", resultMap); | ||
revisions.add(revisionData); | ||
} | ||
} | ||
return revisions; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/cheise_proj/auditing/AuditRevisionListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.cheise_proj.auditing; | ||
|
||
import org.hibernate.envers.RevisionListener; | ||
|
||
class AuditRevisionListener implements RevisionListener { | ||
|
||
@Override | ||
public void newRevision(Object revisionEntity) { | ||
String currentUser = "System"; | ||
AuditRevisionEntity audit = (AuditRevisionEntity) revisionEntity; | ||
audit.setUserName(currentUser); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/main/resources/db/migration/V3__7_revision_info_schema.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
CREATE SEQUENCE IF NOT EXISTS seq_revision_id START WITH 1 INCREMENT BY 50; | ||
|
||
CREATE TABLE revision_info | ||
( | ||
id INTEGER NOT NULL, | ||
revision_date TIMESTAMP WITHOUT TIME ZONE, | ||
user_name VARCHAR(255), | ||
CONSTRAINT pk_revision_info PRIMARY KEY (id) | ||
); |
25 changes: 25 additions & 0 deletions
25
src/main/resources/db/migration/V4__7_customer_audit_schema.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
CREATE TABLE customers_audit | ||
( | ||
rev INTEGER NOT NULL, | ||
revtype SMALLINT, | ||
id BIGINT NOT NULL, | ||
first_name VARCHAR(100), | ||
first_name_mod BOOLEAN, | ||
last_name VARCHAR(100), | ||
last_name_mod BOOLEAN, | ||
email_address VARCHAR(255), | ||
email_address_mod BOOLEAN, | ||
addresses_mod BOOLEAN, | ||
created_by VARCHAR(255), | ||
created_by_mod BOOLEAN, | ||
updated_by VARCHAR(255), | ||
updated_by_mod BOOLEAN, | ||
created_on TIMESTAMP WITHOUT TIME ZONE, | ||
created_on_mod BOOLEAN, | ||
updated_on TIMESTAMP WITHOUT TIME ZONE, | ||
updated_on_mod BOOLEAN, | ||
CONSTRAINT pk_customers_audit PRIMARY KEY (rev, id) | ||
); | ||
|
||
ALTER TABLE customers_audit | ||
ADD CONSTRAINT FK_CUSTOMERS_AUDIT_ON_REV FOREIGN KEY (rev) REFERENCES revision_info (id); |
16 changes: 16 additions & 0 deletions
16
src/main/resources/db/migration/V5__7_customer_address_audit_schema.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
CREATE TABLE customer_address_audit | ||
( | ||
rev INTEGER NOT NULL, | ||
revtype SMALLINT, | ||
id BIGINT NOT NULL, | ||
street_address VARCHAR(255), | ||
city VARCHAR(255), | ||
state_code VARCHAR(255), | ||
country VARCHAR(255), | ||
zip_code VARCHAR(255), | ||
customer_id BIGINT, | ||
CONSTRAINT pk_customer_address_audit PRIMARY KEY (rev, id) | ||
); | ||
|
||
ALTER TABLE customer_address_audit | ||
ADD CONSTRAINT FK_CUSTOMER_ADDRESS_AUDIT_ON_REV FOREIGN KEY (rev) REFERENCES revision_info (id); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.