我使用 Hibernate Envers 创建了一个表审计日志,并且我使用 Spring Data Envers 作为我的库,当我保存/更新/删除它时,它成功地将日志保存在我的 autid_log 表中,但是当我想检索日志数据时,我得到了无限错误循环,我该如何正确执行此操作?这是我的代码:
这是我的控制器:
@GetMapping("/getPartnerRelationshipLog/{partnerId}")
public ResponseEntity<?> getPartnerRelationshipLog(@PathVariable Long partnerId) {
// Long id = partner.getId();
Revisions<Integer,Partner> history = partnerService.findRelationLog(partnerId);
return ResponseEntity.ok(history);
}
这是我的 Partner.java 模型:包 com.example.envers.auditing.Model;
@Data
@Entity
@Audited
@EntityListeners(AuditingEntityListener.class)
@Table(name = "msPartner")
public class Partner {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long id;
public String partnerCode;
public String partnerName;
@CreatedDate
private Date createDate;
@LastModifiedDate
private Date lastModifiedDate;
@CreatedBy
private String createdBy;
@LastModifiedBy
private String modifiedBy;
@OneToMany(mappedBy = "partner", cascade = CascadeType.ALL)
public List<PartnerShipment> partnerShipment;
}
这是我的 PartnerShipment.java :
@Data
@Entity
@Audited
@EntityListeners(AuditingEntityListener.class)
@Table(name = "msPartnerShipment")
public class PartnerShipment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long id;
public String partnerShipmentCode;
public String partnerShipmentAddress;
@JsonIgnore
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "partnerId")
// @NotAudited
public Partner partner;
@CreatedDate
private Date createDate;
@LastModifiedDate
private Date lastModifiedDate;
@CreatedBy
private String createdBy;
@LastModifiedBy
private String modifiedBy;
}
这是我的服务:
public Revisions<Integer,Partner> findRelationLog(Long id) {
Revisions<Integer,Partner> partner = partnerRepository.findRevisions(id);
return partner;
}
这是我的存储库:
@Repository
public interface PartnerRepository extends RevisionRepository<Partner, Long, Integer>, JpaRepository<Partner, Long > {
}
这是我的 Application.java
@SpringBootApplication
@EnableJpaRepositories(repositoryFactoryBeanClass = EnversRevisionRepositoryFactoryBean.class)
@EnableJpaAuditing
public class AuditingApplication {
public static void main(String[] args) {
SpringApplication.run(AuditingApplication.class, args);
}
}
当我得到 id = 1 的数据时,我得到了类似循环的无限错误,从 java.lang.StackOverflowError: null 开始,我在终端中看到的只有这个:
java.lang.StackOverflowError: null
at com.example.envers.auditing.Model.PartnerShipment.hashCode(PartnerShipment.java:33) ~[classes/:na]
at java.util.AbstractList.hashCode(AbstractList.java:541) ~[na:1.8.0_241]
at org.hibernate.envers.internal.entities.mapper.relation.lazy.proxy.CollectionProxy.hashCode(CollectionProxy.java:131) ~[hibernate-envers-5.4.17.Final.jar:5.4.17.Final]
at com.example.envers.auditing.Model.Partner.hashCode(Partner.java:31) ~[classes/:na]
at com.example.envers.auditing.Model.PartnerShipment.hashCode(PartnerShipment.java:33) ~[classes/:na]
at java.util.AbstractList.hashCode(AbstractList.java:541) ~[na:1.8.0_241]
at org.hibernate.envers.internal.entities.mapper.relation.lazy.proxy.CollectionProxy.hashCode(CollectionProxy.java:131) ~[hibernate-envers-5.4.17.Final.jar:5.4.17.Final]
at com.example.envers.auditing.Model.Partner.hashCode(Partner.java:31) ~[classes/:na]
at com.example.envers.auditing.Model.PartnerShipment.hashCode(PartnerShipment.java:33) ~[classes/:na]
at java.util.AbstractList.hashCode(AbstractList.java:541) ~[na:1.8.0_241]
at org.hibernate.envers.internal.entities.mapper.relation.lazy.proxy.CollectionProxy.hashCode(CollectionProxy.java:131) ~[hibernate-envers-5.4.17.Final.jar:5.4.17.Final]
at com.example.envers.auditing.Model.Partner.hashCode(Partner.java:31) ~[classes/:na]
at com.example.envers.auditing.Model.PartnerShipment.hashCode(PartnerShipment.java:33) ~[classes/:na]
at java.util.AbstractList.hashCode(AbstractList.java:541) ~[na:1.8.0_241]
at org.hibernate.envers.internal.entities.mapper.relation.lazy.proxy.CollectionProxy.hashCode(CollectionProxy.java:131) ~[hibernate-envers-5.4.17.Final.jar:5.4.17.Final]
at com.example.envers.auditing.Model.Partner.hashCode(Partner.java:31) ~[classes/:na]
at com.example.envers.auditing.Model.PartnerShipment.hashCode(PartnerShipment.java:33) ~[classes/:na]
at java.util.AbstractList.hashCode(AbstractList.java:541) ~[na:1.8.0_241]
at org.hibernate.envers.internal.entities.mapper.relation.lazy.proxy.CollectionProxy.hashCode(CollectionProxy.java:131) ~[hibernate-envers-5.4.17.Final.jar:5.4.17.Final]
at com.example.envers.auditing.Model.Partner.hashCode(Partner.java:31) ~[classes/:na]
at com.example.envers.auditing.Model.PartnerShipment.hashCode(PartnerShipment.java:33) ~[classes/:na]
at java.util.AbstractList.hashCode(AbstractList.java:541) ~[na:1.8.0_241]
at org.hibernate.envers.internal.entities.mapper.relation.lazy.proxy.CollectionProxy.hashCode(CollectionProxy.java:131) ~[hibernate-envers-5.4.17.Final.jar:5.4.17.Final]
为了避免这个错误,我错过了什么?