1

I'm looking to create a reusable audit table, that will log sql operations that occurred against "data sensitive" areas of the db.

The implementation is easy, in essence, I want to log the user's info who initiated the query, a timestamp and the query with any param values.

For the task on hand, the createNativeQuery() is called from a javax.persistence.EntityManager instance. This method returns a javax.persistence.Query.

I've done some googling and reading through http://docs.oracle.com/javaee/6/api/javax/persistence/Query.html and haven't found an easy way to do what I'm looking to do.

Is there any easy way to get the "generated" version of the query? Or do I need to log the query string separate, and create my own way to iterating through all of the parameters to log things in the desired manner?

James Oravec
  • 19,579
  • 27
  • 94
  • 160
  • Are you using Hibernate? – K.Nicholas Mar 02 '16 at 16:49
  • Grepped the project and there are references to hibernate although they try to keep things in JPA. – James Oravec Mar 02 '16 at 16:59
  • 1
    Well, this says no? [JPA: how do you get/print the JPQL query string...](http://stackoverflow.com/questions/7712440/jpa-how-do-you-get-print-the-jpql-query-string-behind-a-typed-query-after-par) – K.Nicholas Mar 02 '16 at 17:19
  • Seems it'll have to be the long way. I'll leave this open in case someone helps with showing a clean way of doing the long way. Might even post the a answer myself if no one beats me to it ;) – James Oravec Mar 02 '16 at 17:24
  • I saw something that would print the native SQL query, but it seems that you're starting with that. – K.Nicholas Mar 02 '16 at 17:26
  • Yeah, and the value put in is just a string, so just refactor and put into a string variable and then pass that in to both the query as well as the log feature. For the parameters, there is a method call that gets all of them as a set, then its a matter of iterating over them... these could be logged that way, I'm currently trying to think if there is any better way for this, especially for reporting purposes. – James Oravec Mar 02 '16 at 19:05

0 Answers0