Maor
Maor

Reputation: 118

Calculate values after hibernate retrieves data from DB

We're using hibernate for school, and we're implementing loans. We have a Loan class which is persistent, and depending on the information on the loan (amount of payments, total due etc.) the app should calculate the entire payment quotes for the specific loan.

So we have a Quote class with the information regarding that quote and the Loan class has a list of all its quotes (composition between the classes).

Since the quote information is calculable, I think it shouldn't be on the DB.

My question specifically is , how do I get Hibernate to calculate and populate the list of quotes when it brings up a Loan from the DB.

Clarification: Loan class has a method for calculating and populating it's list of quotes, I can't figure out the way to make it run when hibernate retrieves it from the DB.

Update: Since we have a data layer (where the Loan class is) and a business layer (where you actually work on the data, and in turn calls the DAO Layer), I have added a call to the method in the business layer right before I return the object from the DB. Is this best practice? End update

Thanks,

Upvotes: 3

Views: 73

Answers (1)

onof
onof

Reputation: 17367

  1. Just because some data is calculable it doesn't imply that it shouldn't stored in the database. In fact redundancy is often used to improve the performances of a system. Besides, denormalized data are very useful for reports, stats and for business intelligence tools.
  2. Having put the calculation of the Quote in the business layer could be a good choice but it depends on the context of your application. It is the Quote data used only to change the state of the system or it is rather used also in reports? Do you expect much data in the database? Will you need a stat of all Quotes of all loans? Is it your application service oriented? Has it got an external report engine? If any of these questions has yes as answer, you should consider store the computed data in the database

Upvotes: 1

Related Questions