我当前正在使用Spring Boot 2.1.1.RELEASE,Hibernate Envers 5.3.7.Final。
当我为我的User类查询AuditEntity时,它将抛出javax.persistence.EntityNotFoundException,表明我的用户记录不存在。但是,它确实存在于数据库中。有人知道为什么吗?
用户实体
@Entity
@EntityListeners(AuditingEntityListener.class)
@Audited
@Table(name = "[user]")
@Getter
@Setter
@NoArgsConstructor
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(nullable = false, unique = true)
@Length(max = 20)
@Unique
private String username;
@Column(nullable = false)
private String password;
@Column
private String email;
@Column(nullable = false)
private String name;
@Audited(targetAuditMode = NOT_AUDITED)
@ManyToOne(fetch = FetchType.LAZY)
@CreatedBy
private User createByUser;
@CreatedDate
private LocalDateTime createDatetime;
@Audited(targetAuditMode = NOT_AUDITED)
@ManyToOne(fetch = FetchType.LAZY)
@LastModifiedBy
private User updateByUser;
@LastModifiedDate
private LocalDateTime updateDatetime;
@Column
private Boolean active = true;
@Column
private Boolean deleted = false;
}
查询审计实体时,属性createByUser updateByUser抛出异常javax.persistence.EntityNotFoundException:无法找到ID为2的data.entity.User。但是,在数据库中,我有此记录。
AuditReader auditReader = AuditReaderFactory.get(entityManager);
AuditQuery query = auditReader.createQuery().forRevisionsOfEntity(User.class, true, true);
query.add(AuditEntity.id().eq(id));
return query.getResultList();
编辑于2018-12-05
问题似乎是此id 2记录的问题,我从后端而不是通过前端插入此记录,这会在_aud表中生成一条记录。不确定该审核逻辑如何工作。
答案 0 :(得分:0)
我发现这确实是由于_aud表中的记录缺失所致。
不是将测试记录直接插入后端。我决定在Spring Boot的Application类中创建一个例程。
@SpringBootApplication(
exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class})
@EntityScan(basePackageClasses = {WebApplication.class})
@EnableAsync
@EnableCaching
@EnableTransactionManagement
@EnableConfigurationProperties
public class WebApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}
@Bean
public CommandLineRunner setUpInitialData(
EntityRepository1 entityRepository1,
EntityRepository2 entityRepository2) {
return args -> {
// do all data insertion here...
};
}
}
此表或_aud表还必须包含该实体的相应条目。