is any other way to do it?
The way fits you more, is using default values in your database or in hibernate like in this question:
If you don't want database default value, but simply a default value
in your Java code, just initialize your variable like that
Will clean the code and make logic clearer:
private Integer myColumn = 100;
Anyway, let answer your questions :)
Which is better?
Depending of the app needs.
- create hidden fields in the update form for all such fields
If the logic to obtain this fields has a high computation cost, you can use hidden fields to save this computational cost, but this does not seem to be your problem as long as you harcoded this data.
- hard code them again at the time of updating.
Hardcoding never is an option outside academical programming, but if the updated fields can make this fixed values to be changed, your best option is to generate again this fields prior to update.
UPDATE:
I also want to avoid hard coded values. with hidden fields safety is only concern. Though in my current case security is not a big issue and i can follow second option.
Then yes, second option is better to you.
To clarify, i.e. this will be hardcoding:
int prize = 100;
But if you ask prize in the form and you have a constant field like:
// CONSTANT
private final Integer VAT = 18;
// ATTRIBUTE (filled by user in form)
int prize;
When you update the entity, you can recalculate each time, for example, prize with taxes (not tested, be careful with floating point error dividing )
// calculated field / attribute
double finalPrize = prize * VAT / 100;
That's using constants, not hardcoding and constants are our friends ;)
UPDATE: SECURITY
but I am curious to know what if i have to keep these values safe
About security there are many ways, easy one? Keeping the no-updatable values in the server side will do the job, if them are necessary in the client side, you can double validate them (javascript
/hibernate
) and also (if necessary) encrypt to hide content.