Work around for envers auditing for bulk update - bulkinsert

In the application which I am working on I use spring, hibernate and envers for auditing. envers works with calls like, hibernateTemplate.insert, hibernateTemplate.save, hibernateTemplate.saveOrUpdate. But it doesnt seem to work when i call hibernateTemplate.bulkUpdate.
I googled for solutions and found that envers doesnt support bulkUpdate.
A work around has been provided in the link below but i am not able to get it.
Envers Bulk insert/updates
It would be of help if someone can provide a workaround/sample for this.
Thanks

The documentation is correct. HQL and native SQL operations are not audited.
Since the performance of the bulk update will be affected by auditing, you may wish to change your design - for example, if you have a parent entity with related children, and you are performing a bulk update on the child records, you could update attributes of the parent record and then call saveOrUpdate after doing the bulk update.
Another option, is to manually perform the bulk update on the audit table(s) also, but its not going to be elegant. I managed to get around the issue in my case by changing the design as per the above.

Related

Transaction lock in sql

Two processes are running simultaneously on a table. One is updating the records. The other is reading the data. While updating it is locking the table.so I am unable to read the data. Help to to handle the same.
Thanks,
Bibhu
I am assuming you are using SQL Server. You can use the WITH NOLOCK hint in your select statement, so the read goes through. However, you should be aware that it is a dirty read. Since the update statement is changing the data and the changes may or may not have been committed, the result of the read (select) statement can be problematic.
Here is a link to a page with more details and simple examples
https://www.mssqltips.com/sqlservertip/2470/understanding-the-sql-server-nolock-hint/

Hibernate Envers SQL auditing

I was wondering if anyone had succeeded in auditing a native query (SQL) with Hibernate Envers? I know this is probably just wrong, but it would spare me a lot of refactoring time.
Cheers
Nick
I just want to leave my thoughts here so others might benefit when they choose to Envers. We tried Hibernate envers in one our recent project and it did not work out. Below are the reason
Hibernate Envers captures the Audit information only when the updates happen through Persistence Context.
We did not like one audit table for each entity. It was too much schema pollution.
We have lot of batch jobs and data synchronization scripts that updates data directly using sql queries. Any update that is happening outside the persistence context will not be captured in these Hibernate ENvers created Audit tables.
SO we went with Database trigger appraoach with just only one AUDIT table which will capture the table_name, column_name, primary_key, old_value and new_value. It worked for us.

NHibernate, Caching and custom SQL queries

We're using NHibernate with Memcache as the second level cache. Occasionally there is a need for more advanced queries or bulk query operations. From the book Nhibernate in Action they recommend the following:
"It’s our view that ORM isn’t suitable for mass-update (or mass-delete) operations. If
you have a use case like this, a different strategy is almost always better: call a stored
procedure in the database, or use direct SQL UPDATE and DELETE statements for that
particular use case."
My concern is that queries against the underlying database do not reflect in the cache (at least until cache expiry) and I was wondering if anyone has come up with any effective strategies for mixing and matching NHibernate with custom SQL statements?
Is there any way of getting say a bulk Update statement (executed with custom sql) to reflect in the second level cache? I am aware of being able to manually evict, but this removes the items from cache and thefore increases hits on the database.
Does the community have any solutions that have been found to be effective in dealing with this problem?
As far as I know there is no method to keep the 2nd level cache up to date with massupdates. But you can partially evict the cache as described in: http://www.nhforge.org/doc/nh/en/index.html#performance-sessioncache.

Using SQLQuery.uniqueResult() to perform native SQL INSERTs and UPDATEs

I am using Hibernate in my project and there is a certain scenario where I want to use the uniqueResult() method on the org.hibernate.SQLQuery class to perform native SQL INSERT and UPDATE operations.
I did try using the executeUpdate() method on the same class. But I get an error saying that they are used for HQL updates only.
Please advice if this is effective and reliable way of ensuring data being saved/updated in the database.
session.createSQLQuery() is for querying, not manipulation. If you want to do raw SQL insert, use session.connection() and straight JDBC code.
Not sure why you need this exactly but I'd also suggest to check 16.3. Custom SQL for create, update and delete.

Nhibernate Auditing

I have a question about auditing. Most auditing examples use one audit table to track changes. However, we need one audit table per "regular" table. In other words, tblCustomer would also have tblCustomer_History. I can't figure out how to use a listener, and on update populate the history table as well. Any ideas? I'd hate to fall back on SQL Server triggers.
If you don't want to build your own audit solution, maybe you should have a look at NHibernate Envers?
https://bitbucket.org/RogerKratz/nhibernate.envers