我试图实现的是在响应中打印出 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 的修订类型,当修订类型变为 UNKNOWN 时我不依赖。这是我的模型: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);
}
}
在修订类型打印未知之前我做错了什么?