tommccann
tommccann

Reputation: 133

Design: What is the best approach to updating part of a database record?

This question concerns the best design approach rather than a particular technical technique. I have a database table that contains system user's details i.e: username, password, firstname, lastname (these attributes can only be changed by an administrator). The rest of the user record contains address, phone number, mobile number, etc (these attributes can only be changed by the user).

I therefore need 2 forms, an 'admin' form and a 'user' form, with a different set of editable fields on each, and appropriate role-based security to control usage.

Here are two approaches I could take:

Approach 1. Each form has it's own backing bean representing just the editable fields on the form. On 'Save', the backing bean calls a specific DAO method (e.g. UpdateUser_Admin or UpdateUser_User) to perform a partial update of the user record. This approach therefore involves 2 forms, a backing bean for each form, and 2 methods in the DAO class.

Approach 2. There are 2 forms as before, but each form re-creates the entire User object through the use of input fields for the editable fields and hidden fields for the non-editable fields. Both forms would use the same backing bean representing ALL of the User attributes. There would only need to be one method in the DAO - to update ALL fields in the User record.

The second approach seems to make sense to me, but I'm new to development and I'd be grateful if readers could advise if I'm missing something. I've already considered splitting the User table into 2 separate tables but that is not practical in this case. Thank you for your advice.

Upvotes: 1

Views: 115

Answers (1)

Jordi Castilla
Jordi Castilla

Reputation: 26961

  • As a rule: don't create DAO methods updating only a single field / attribute.
  • Don't split User entity into 2 tables.
  • DAO are created to manage entities, thus use one DAO for each class and code only a single update method for each DAO.
  • If you are not sure of having the complete entity follow this steps in the service layer

    • get data from form (at least id and field you want to update)
    • retrieve Object representing entity from database (with DAO::readById(id) for example)
    • update the field you need in the java Object
    • call DAO::update

      NOTE: I ommited populating DTO to DBO objects

Upvotes: 1

Related Questions