6

I am using JPA & Spring-Data JPA in a project. I have one table wherei Insert and then update the data. However i need the auditing information to save the state of all the objects i.e. I need state information of insert and all subsequent updates in another Table. I know I can use triggers in database to do this. However, Can this be done using JPA/Spring Data JPA?

Any help would be appreciated.

Abhishek
  • 1,031
  • 4
  • 16
  • 25

2 Answers2

7

JPA spec (section 3.5) defines lifecycle callback methods:

  • prePersist
  • postPersist
  • preRemove
  • postRemove
  • preUpdate
  • postUpdate
  • postLoad

which are probably the closest thing you will find in pure JPA. SpringData itself has further auditing capabilities which are at a slightly higher level of abstraction. It describes these in section 2.4 of the reference manual.

Pace
  • 41,875
  • 13
  • 113
  • 156
  • Although the JPA spec will allow you to do this, Spring Data Jpa has a very slick mechanism built in to do just this: http://static.springsource.org/spring-data/data-jpa/docs/current/reference/html/#jpa.auditing – bh5k Aug 22 '12 at 23:05
1

See,

http://en.wikibooks.org/wiki/Java_Persistence/Auditing_and_Security

http://en.wikibooks.org/wiki/Java_Persistence/Advanced_Topics#History

http://wiki.eclipse.org/EclipseLink/Examples/JPA/Auditing

http://wiki.eclipse.org/EclipseLink/Examples/JPA/History

James
  • 17,965
  • 11
  • 91
  • 146
  • I saw the above links and they specify the History Feature can be used in JPA but i saw links of only TOPLINK and ECLIPSELINK. Any idea if it can be used with Hibernate also? – Abhishek Jun 23 '11 at 15:09