我有一个自定义的Hibernate事件监听器。如果一个实体是脏的,则代码会尝试查找该实体的旧审核条目。问题在于,这会导致堆栈溢出错误。
这是我的听众:
@Component
public class MyCustomEventListener implements FlushEntityEventListener {
@Autowired
private AuditService service;
@Override
public void onFlushEntity(FlushEntityEvent event) throws HibernateException {
final Object entity = event.getEntity();
boolean isDirty = isDirty(event);
if (isDirty) {
service.auditEntity(entity);
}
...
}
...
}
我的服务:
@Service
public class AuditServiceImpl implements AuditService
public void auditEntity(BaseEntity entity) {
try {
String tableName = getTableName(entity);
Optional<AuditRecord> result = auditRepository.findFirstByTableNameAndRecordIdOrderByIdDesc(tableName, entity.getId());
...
}
...
}
}
在调用存储库查找器期间,它会以某种方式再次触发eventListener。