Reputation: 4738
I have a two tables chart and chartHistory which has similar table structure
chart_id NUMERIC(9,0)
chart_ref_id NUMERIC(9,0) NOT NULL
chart_property VARCHAR2(20) NOT NULL
chart_value VARCHAR2(100)
The entries in chart needs to be stored into the chartHistory table, when we get new property/value pairs for chart reference id.
I am planning to implement it using Hibernate in the following way
Get the list of values from CHART table using the chartReferenceId
List<Chart> chartList = chartDao.findChartByVisitRef(chartReferenceId);
( named query : findChartByRef = from Chart chart where chart.chartReferenceId=:chartReferenceId)
Use the retrieved list and build a chartHistoryList using the values from chartList and call the Hibernate method saveAll
chartHistoryDao.saveAll(chartHistoryList);
Once saved, remove the existing entries from the CHART table.
chartDao.deleteAll(chartList);
Build a newChartList for the new entried received, and do a hibernate saveAll
chartDao.saveAll(newChartList);
As you can see this executes four queries ie. for retrieving old records, saving them into the archive table, deleting the old entries and inserting the new values.
I would like to know whether there is a better way to implement this? Something of the sorts insert into .. (select from... ) would be more efficient I guess? Please advice.
Upvotes: 0
Views: 3350
Reputation: 328754
Try the envers/audit module of Hibernate which handles all this for you automatically.
The main problem with envers/audit right now is to find the documentation. Envers (the original module) is supposed to be merged with Hibernate code since 3.5 but the core documentation doesn't mention anything about versioning or auditing even though InfoQ insists "Hibernate 4.1 Released With Improved Auditing Support"
So try the outdated documentation linked from the obsolete envers page and hope for the best.
Disclaimer: I haven't used Hibernate or envers for years.
Upvotes: 3