• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Question on checking and logging an enity before inserting

 
Ranch Hand
Posts: 632
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: 21002
128
Android Eclipse IDE Tomcat Server Redhat Java 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: 632
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much will look at envers tomorrow
 
30 seconds to difuse a loaf of bread ... here, use this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!