Following is my problem. Prerequisite: Spring boot application1(SB1) with Database1. Spring boot application2(SB2) with Database2.
Both the application write javers audit data to Database3.
SB1 audits data for entity E1.
SB2 audits data for entity E2.
QueryBuilder from SB1 cannot read the audit data for E2 correctly.
It picks only NewObject type changes related to E2.
QueryBuilder from SB1 can read audit data for E1 perfectly. QueryBuilder from SB2 can read audit data for E2 perfectly.
If SB1 can make a REST call to SB2 to get the 'changes' from the QueryBuilder instance in SB2 it works perfectly.
QueryBuilder from SB1 should be able to read the audit data for E2 correctly since the audited data is assumed to be agnostic to JaversRepository instance.
Following code is used in both the Spring boot application to get the audited records.
@RequestMapping("/javers")
public String audit() {
QueryBuilder jqlQuery = QueryBuilder.anyDomainObject();
Changes changes = javers.findChanges(jqlQuery.withNewObjectChanges().build());
List<ChangesByCommit> changesByCommit = changes.groupByCommit();
return javers.getJsonConverter().toJson(changesByCommit);
}
Following entity, repository, application.properties and 'jql changes output' was used in Spring boot application1 (SB1)
`
package com.test.javers.entitya;
import java.io.Serializable;
import java.sql.Timestamp;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "EntityA")
public class EntityA implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "APPLICATION_ID")
private String applicationId;
@Column(name = "APP_NAME")
private String appName;
@Column(name = "CREATE_DATE")
private Timestamp createDate;
@Column(name = "DESCRIPTION")
private String description;
@Column(name = "IS_AUDIT_ENABLED")
private Boolean isAuditEnabled;
public EntityA() {
}
public String getApplicationId() {
return applicationId;
}
public void setApplicationId(String applicationId) {
this.applicationId = applicationId;
}
public String getAppName() {
return appName;
}
public void setAppName(String appName) {
this.appName = appName;
}
public Timestamp getCreateDate() {
return createDate;
}
public void setCreateDate(Timestamp createDate) {
this.createDate = createDate;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Boolean getIsAuditEnabled() {
return isAuditEnabled;
}
public void setIsAuditEnabled(Boolean isAuditEnabled) {
this.isAuditEnabled = isAuditEnabled;
}
}
--------------------------------------------------------------------
package com.test.javers.repository;
import org.javers.spring.annotation.JaversSpringDataAuditable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.test.javers.entitya.EntityA;
@Repository
@JaversSpringDataAuditable
public interface EntityARepository extends JpaRepository<EntityA, String>{
}
--------------------------------------------------------------------------
spring.datasource.url=jdbc:postgresql://localhost/sandbox1
spring.datasource.username=postgres
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.password=postgres
server.port=8010
#Audit datasource configuration
spring.audit-datasource.url=jdbc:postgresql://localhost/sandboxaudit
spring.audit-datasource.username=postgres
spring.audit-datasource.password=postgres
spring.audit-datasource.driver-class-name=org.postgresql.Driver
-------------------------------------------------------------------------
[ { "changes": [ { "changeType": "NewObject", "globalId": { "entity":
"com.test.javers.entityb.EntityB", "cdoId": "IEj5tvWc850834675" },
"commitMetadata": { "author": "EntityBUser", "properties": [ { "key":
"applicationID", "value": "ViralSEQ" } ], "commitDate": "2019-06-
25T19:16:02.946", "commitDateInstant": "2019-06-25T13:46:02.946Z", "id":
1.00 } } ], "commitMetadata": { "author": "EntityBUser", "properties": [ {
"key": "applicationID", "value": "ViralSEQ" } ], "commitDate": "2019-06-
25T19:16:02.946", "commitDateInstant": "2019-06-25T13:46:02.946Z", "id":
1.00 } } ]
`
Following is the entity and repository used for Spring Boot Application 2 (SB2)
`
package com.test.javers.entityb;
import java.io.Serializable;
import java.sql.Timestamp;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "EntityB")
public class EntityB implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "APPLICATION_ID")
private String applicationId;
@Column(name = "APP_NAME")
private String appName;
@Column(name = "CREATE_DATE")
private Timestamp createDate;
@Column(name = "DESCRIPTION")
private String description;
@Column(name = "IS_AUDIT_ENABLED")
private Boolean isAuditEnabled;
public EntityB() {
}
public String getApplicationId() {
return applicationId;
}
public void setApplicationId(String applicationId) {
this.applicationId = applicationId;
}
public String getAppName() {
return appName;
}
public void setAppName(String appName) {
this.appName = appName;
}
public Timestamp getCreateDate() {
return createDate;
}
public void setCreateDate(Timestamp createDate) {
this.createDate = createDate;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Boolean getIsAuditEnabled() {
return isAuditEnabled;
}
public void setIsAuditEnabled(Boolean isAuditEnabled) {
this.isAuditEnabled = isAuditEnabled;
}
}
package com.test.javers.repository;
import org.javers.spring.annotation.JaversSpringDataAuditable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.test.javers.entityb.EntityB;
@Repository
@JaversSpringDataAuditable
public interface EntityBRepository extends JpaRepository<EntityB, String>{
}
spring.datasource.url=jdbc:postgresql://localhost/sandbox2
spring.datasource.username=postgres
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.password=postgres
server.port=8020
#Audit datasource configuration
spring.audit-datasource.url=jdbc:postgresql://localhost/sandboxaudit
spring.audit-datasource.username=postgres
spring.audit-datasource.password=postgres
spring.audit-datasource.driver-class-name=org.postgresql.Driver
------------------------------------------------------------------
[ { "changes": [ { "changeType": "ValueChange", "globalId": { "entity":
"com.test.javers.entityb.EntityB", "cdoId": "IEj5tvWc850834675" },
"commitMetadata": { "author": "EntityBUser", "properties": [ { "key":
"applicationID", "value": "ViralSEQ" } ], "commitDate": "2019-06-
25T19:17:07.676", "commitDateInstant": "2019-06-25T13:47:07.676Z", "id":
2.01 }, "property": "description", "propertyChangeType":
"PROPERTY_VALUE_CHANGED", "left": "EntityB", "right": "WjMVxZBX" } ],
"commitMetadata": { "author": "EntityBUser", "properties": [ { "key":
"applicationID", "value": "ViralSEQ" } ], "commitDate": "2019-06-
25T19:17:07.676", "commitDateInstant": "2019-06-25T13:47:07.676Z", "id":
2.01 } }, { "changes": [ { "changeType": "ValueChange", "globalId": {
"entity": "com.test.javers.entityb.EntityB", "cdoId": "IEj5tvWc850834675" },
"commitMetadata": { "author": "EntityBUser", "properties": [ { "key":
"applicationID", "value": "ViralSEQ" } ], "commitDate": "2019-06-
25T19:16:02.946", "commitDateInstant": "2019-06-25T13:46:02.946Z", "id":
1.00 }, "property": "applicationId", "propertyChangeType":
"PROPERTY_VALUE_CHANGED", "left": null, "right": "IEj5tvWc850834675" }, {
"changeType": "ValueChange", "globalId": { "entity":
"com.test.javers.entityb.EntityB", "cdoId": "IEj5tvWc850834675" },
"commitMetadata": { "author": "EntityBUser", "properties": [ { "key":
"applicationID", "value": "ViralSEQ" } ], "commitDate": "2019-06-
25T19:16:02.946", "commitDateInstant": "2019-06-25T13:46:02.946Z", "id":
1.00 }, "property": "description", "propertyChangeType":
"PROPERTY_VALUE_CHANGED", "left": null, "right": "EntityB" }, {
"changeType": "NewObject", "globalId": { "entity":
"com.test.javers.entityb.EntityB", "cdoId": "IEj5tvWc850834675" },
"commitMetadata": { "author": "EntityBUser", "properties": [ { "key":
"applicationID", "value": "ViralSEQ" } ], "commitDate": "2019-06-
25T19:16:02.946", "commitDateInstant": "2019-06-25T13:46:02.946Z", "id":
1.00 }
} ], "commitMetadata": { "author": "EntityBUser", "properties": [ { "key":
"applicationID", "value": "ViralSEQ" } ], "commitDate": "2019-06-
25T19:16:02.946", "commitDateInstant": "2019-06-25T13:46:02.946Z", "id":
1.00 }
} ]
`
Notice that the "NewObject" is appearing in both the output but rest of the 'changeTypes' are absent from SB1 even though the audit database is the same.