来自一个数据库的QueryBuilder无法从另一个数据库读取由其他JaversRepository创建的审核数据

时间:2019-06-24 10:06:16

标签: javers

以下是我的问题。 先决条件: 具有Database1的Spring Boot Application1(SB1)。 带有Database2的Spring Boot Application2(SB2)。

两个应用程序都将javers审核数据写入Database3。

SB1审核实体E1的数据。

SB2审核实体E2的数据。

SB1的QueryBuilder无法正确读取E2的审核数据。

它仅选择与E2相关的NewObject类型更改。

SB1的

QueryBuilder可以完美读取E1的审核数据。 SB2的QueryBuilder可以完美读取E2的审核数据。

如果SB1可以对SB2进行REST调用以从SB2中的QueryBuilder实例获取“更改”,那么它就可以完美地工作。

SB1的QueryBuilder应该能够正确读取E2的审核数据,因为被审核的数据被认为与JaversRepository实例无关。

两个代码均在Spring Boot应用程序中使用以下代码来获取审核的记录。

@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);
}

在Spring Boot Application1(SB1)中使用了以下实体,存储库,application.properties和“ jql更改输出”

`
    包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 } } ]

`

以下是用于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 } 
} ]

`

请注意,尽管审核数据库相同,但SB1中都没有在输出中同时出现“ NewObject”,但其余的“ changeTypes”却不存在。

1 个答案:

答案 0 :(得分:0)

为此,它可以运行以下Java应用程序: 触发查询应具有所有类 来自类路径中的jv_snapshot。在上述情况下,必须确保EntityA和EntityB在类路径中。然后,您将获得正确的输出。 这意味着您不能在没有所有jv_snapshot的类路径中包含所有类的“更改”中运行javers查询,否则它将仅返回“ NewObject”更改类型。