0

I am about to start implementing the following relationship:

Entity one-to-many Configuration

But the Configuration will evolve over time, including the creation of future Configurations, meaning the following fields in the configuration table:

entityId
fromDate
toDate
configurationValue1
configurationValue2
...

fromDate and the toDate store the dates when a particular version of the information is active.

The final entry in the configuration table would have its fromDate, and a very distant toDate. Adding a new Configuration before the toDate would update the current latest configuration to have an toDate the day before the new configuration's fromDate, and the new Configuration's toDate would be the value in the future.

All this seems like a good case for a well-defined and tested DAO able to retrieve all Configurations for an Entity, the current Configuration for an Entity, and to update future Configurations (ensuring no overlap of dates of course), but before I go about implementing it, is there anything within Hibernate that would help, for example, code that could allow me to do this? :

myEntity.getCurrentConfiguration() //perhaps a @Where that uses now() to find the correct Configuration?

I am aware of Hibernate Envers but I don't think that this is necessarily the right use case for it. Envers seems to manage audit logs, transparently keeping a history of changes made to an entity, whereas I want to be able to manage the current and future values of an entity, with the current value being based upon now() (I do however need to be able to keep all old entries).

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Rich
  • 15,602
  • 15
  • 79
  • 126
  • You could utilize Hibernate formulas or database views for this; see [this answer](http://stackoverflow.com/a/32027714/4754790). However, both would mean that joins between `Entity` and `Configuration` are performed whenever you read `Entity`s, so you might want to just execute additional query to get the current configuration when you need it, without mapping it in the `Entity` at all. – Dragan Bozanovic Feb 23 '17 at 12:25

1 Answers1

0

That it absolutely correct, Hibernate Envers does not fit the requirements as you've outlined.

However, I would instead suggest you separate the concerns with the date business logic that you described from the normal CRUD persistence operations your DAO class would typically expose.

This way, if you decide later on that you want to store that configuration data the same way, but rather than in a JPA persistence data store but something else, you only touch the persistence code based on a commonly exposed interface and your date manipulation logic remains untouched.

Naros
  • 19,928
  • 3
  • 41
  • 71
  • Thanks for validating this - the approach you have suggested is exactly the approach I had already chosen: a Util class which is able to manipulate a TreeSet of existing entities which must implement a particular method. By using an interface I can use the technique anywhere, and Hibernate will pick up the modifications. – Rich Feb 27 '17 at 09:43