1

I have table suspect in database ,using hibernate reverse engineering i have created a bean of it. I need to make a form to update this suspect.

What i did is :

i. fetched suspect row (based on id using hibernate)

ii. set in an suspect named object.

iii. passed it as spring form command object.

It shows the data from table in the form successfully.

There are some fields in the table that i set hard coded through some logic. These fields doesn't exist in form tags.So when i save(using update function in hibernate) this form. it sets those columns values as null.

What by now i have felt is there may be two solutions :

  1. create hidden fields in the update form for all such fields or
  2. hard code them again at the time of updating.

which is better and if there is any other way to do it?

alexbt
  • 16,415
  • 6
  • 78
  • 87
Anuja
  • 33
  • 6

1 Answers1

0

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.

Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • glad to help, please don't hesitate to ask if you need clarification or further info – Jordi Castilla Mar 22 '16 at 08:23
  • Thanks for reply but values are not fixed i cant use default values. 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. but I am curious to know what if i have to keep these values safe. – Anuja Mar 22 '16 at 08:28
  • 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 – Jordi Castilla Mar 22 '16 at 08:31
  • I can hardcode based on at what url request is coming not in database. – Anuja Mar 22 '16 at 08:32
  • then you calculate values with some constants, you don't hardcode it ;) – Jordi Castilla Mar 22 '16 at 08:33
  • what I mean, `int prize = 100` is harcoding it, but `final int VAT = 18; int finalPrize = prize * VAT / 100` is calculate using constants. Constants are is really usual and useful :) – Jordi Castilla Mar 22 '16 at 08:35