0

I store to database entity LOG which is immutable (doesn't change) but other classes (entities) which it includes can change do I have to annotate this one with @Audited (I mean log)? This creates extra table of history which doesn't make any sense because LOG can not change so it is just creating duplicates. But when I don't add @Audited I can not search for it using AuditReader (AuditQuery) ... Could anyone help me here?

EXAMPLE:

LOG[id, weather_station_id, measurement ...]
WEATHER_STATION[id, name ...]
SENSOR[id, name, weather_station_id ...]

The name of sensor or weather station can change and I need to be able to get the original data when the LOG was created.

EDITED: I will try to describe my problem in more details. I am getting some measurements from sensors of weather station which has some sensors. I need to store these measurements into database to be able to read them later (I am calling this "log"). The measurement value can not change. The log class (entity) has reference (relation ship) with weatherStation class which has reference to multiple sensors. Some informations about weatherStation and sensors can change during time such as firmware version etc. And I need to be able to get the whole log class from database at the time of measurement to be able to check the firmware number when the measurement was made for example.

I would like to achieve something like Log getLogFromDb(Long id) which would return the log class including weatherStation information at the time of measurement and information about sensors at the time of measurement.

I was able to get similar behavior with Hibernate Envers when I marked WeatherStation, Sensor and LOG as audited. But as you can see the LOG table doesn't change at all so it make no sense to have log history table because it just includes always the same data.

I hope my problem is clearer now.

This is how I do it right now:

public Optional<Log> findVersionById(Integer id) {
    AuditReader auditReader = AuditReaderFactory.get(entityManager);
    AuditQuery query = auditReader.createQuery().forRevisionsOfEntity(Log.class, true, false);
    query.add(AuditEntity.revisionType().eq(RevisionType.ADD));
    query.add(AuditEntity.id().eq(id));
    Log log = (Log)query.getSingleResult();

    return Optional.of(log);
}
4

1 回答 1

0

您可以使用 HQL 来搜索已审计实体和普通实体。尝试调试以查看被审计实体的元模型类型的结构。您可以通过查看来访问元模型EntityManagerFactory.getMetamodel().getEntities()

于 2020-12-23T09:37:33.767 回答