为什么 Spring Data Envers Revision Type 返回 UNKNOWN?

Why Spring Data Envers Revision Type returned UNKOWN?

我想要实现的是在响应中打印出 UNKNOWN 的修订类型,但在数据库中存在 revtype。这是回复:

{
  "metadata": {
    "delegate": {
      "id": 1,
      "timestamp": 1594199577086,
      "revisionDate": "2020-07-08T09:12:57.086+0000"
    },
    "revisionNumber": 1,
    "revisionDate": "2020-07-08T16:12:57.086",
    "revisionInstant": "2020-07-08T09:12:57.086Z",
    "revisionType": "UNKNOWN",
    "requiredRevisionInstant": "2020-07-08T09:12:57.086Z",
    "requiredRevisionNumber": 1
  },
  "entity": {
    "id": 2,
    "roleCode": "ROLE001",
    "roleName": "Admin",
    "isInternal": false,
    "isDeleted": false,
    "createdDate": "2020-07-08T09:12:56.723+0000",
    "modifiedDate": "2020-07-08T09:12:56.723+0000",
    "createdBy": "someone",
    "modifiedBy": null,
    "roleDt": [
      {
        "id": 2,
        "moduleName": "SALES",
        "permission": "ALL"
      },
      {
        "id": 3,
        "moduleName": "Report",
        "permission": "ALL"
      },
      {
        "id": 4,
        "moduleName": "Dashboard",
        "permission": "ALL"
      }
    ]
  },
  "revisionNumber": 1,
  "requiredRevisionInstant": "2020-07-08T09:12:57.086Z",
  "requiredRevisionNumber": 1,
  "revisionInstant": "2020-07-08T09:12:57.086Z"
}

起初我使用 Spring Data Envers 开发,我打印了修订类型 INSERT/UPDATE/DELETE,当修订类型变为未知时,我不会重新使用。 这是我的模型: RoleHd.java

@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Audited
@EntityListeners(AuditingEntityListener.class)
@EqualsAndHashCode(of = "id")
@ToString(of = {"id"})
@Table(name= "msRoleHd")
public class RoleHd {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long id;

    @NotBlank
    public String roleCode;

    @NotBlank
    public String roleName;

    @NotNull
    public Boolean isInternal;

    public Boolean isDeleted = false;

    @CreatedDate
    private Date createdDate;
    @LastModifiedDate
    private Date modifiedDate;
    @CreatedBy
    private String createdBy;
    @LastModifiedBy
    private String modifiedBy;

    @OneToMany(mappedBy = "roleHd", cascade = CascadeType.ALL)
    public List<RoleDt> roleDt;

    @JsonIgnore
    @OneToMany(mappedBy = "roleHd", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    public List<UserPartnerRole> userPartnerRole;
}

RoleDt.java :

@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Audited
@EntityListeners(AuditingEntityListener.class)
@Table(name= "msRoleDt")
public class RoleDt {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long id;

    public String moduleName;

    public String permission;

    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "roleId")
    public RoleHd roleHd;

}

这是我保存数据的方式 RoleHdService.java :

public RoleHd save(InputRequest<RoleHd> request) {
    String currentUser = request.getLoggedInUser();
    RoleHd roleHd = request.getObject();

    if(roleHdRepository.findByroleCodeIgnoreCase(roleHd.getRoleCode()).isPresent()){
        throw new ResourceAlreadyExistException("Role Code "+ roleHd.getRoleCode() +" already exists!");
    }
    
    roleHd.setCreatedBy(currentUser);
    roleHd.getRoleDt().forEach(d -> d.setRoleHd(roleHd));

    return roleHdRepository.save(roleHd);
}

这是我从 envers 获取修订数据的方法:

public Revision<Integer, RoleHd> findByIdLatest(Long id) {

    return roleHdRepository.findLastChangeRevision(id).orElseThrow(() -> new ResourceNotFound("Role "+ id +" not found!"));
}

这是我的 application.java

@SpringBootApplication
@EnableJpaRepositories(repositoryFactoryBeanClass = EnversRevisionRepositoryFactoryBean.class)
@EnableJpaAuditing
public class B2bApiApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(B2bApiApplication.class, args);
    }

}

在 revisionType 打印未知之前我做错了什么?

找到答案了,那是 Spring Data Envers 的一个错误,我在这个问题中发现了这个错误: https://github.com/spring-projects/spring-data-envers/issues/215

我猜它在 2.2.5 以上的版本中解决了,我的项目使用的是 2.2.5 版本的 spring data envers 而我的 spring 启动版本是 2.2.5 然后我只是将我的 spring 引导项目版本更新为 2.3.1 并且其他依赖项在后面并且现在打印了 revisionType

但我发现它破坏了我整个模型中的 javax.validation 标签,我将为此提出另一个问题