This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Question on checking and logging an enity before inserting  RSS feed

 
Ranch Hand
Posts: 609
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have a table  in my database, it has a price, what I want to do is if the price changes I want the old price to be logged into another table.  PRICE,  PRICE_HISTORY


Now another elelment is that each entity with a price will have a timestamp for when that price was created. So that the timestamp will also be checked.
So the rules will be.

A Price comes in. check the price is it different to price in PRICE Table then check incoming price timestamp, if the pricestamp is greater than the timestamp in the PRICE table
insert into PRICE table, insert into PRICE_HISTORY

If price is it different to price in PRICE Table but  the pricestamp is not  greater than the timestamp in the PRICE table
then insert into PRICE_HISTORY only


One way to do this is my coding that logic into the java itself. Which would be easy.
What I would like to know, is, is there a way to do this on the database. So that when I insert data into the table PRICE, JPA will handle the logic that I detailed above.

Thank You Tony
 
Bartender
Posts: 19988
95
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you should be able to do that using a database trigger.

However, if you're using Hibernate JPA, there's something called "Envers" that allows you to annotate entities for change logging automatically.
 
Tony Evans
Ranch Hand
Posts: 609
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much will look at envers tomorrow
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!