Let us say I've got a pre-defined listing of values (RW, FW, 4W) representing drive kind of an automobile:

RW - Rear Wheel

FW - Front Wheet

4W - Four Wheel

Now, I wish to have a value in the above 3 values being an input from the user after which store it inside a database.

Up to my understanding, I'm able to perform this with the aid of the following techniques:

- Hard-code the values in the UI to ensure that the UI shows a drop-lower getting just the above 3 values. Then store that value within the String vehicleType area from the Vehicle vehicle object after which store it within the DB as String.

  • Cons:

    i). No validation from the value at object level

    ii). No validation from the value at DB level.

    iii). Though the requirement for adding a brand new value towards the list is rare, but nonetheless user can't give a new value at runtime

    - Pros:

    i). No necessity of join at DB to retrieve the vehicle object

OR

  • Create a separate table VEHICLE_TYPE within the DB getting all of the 3 values and link it using the VEHICLE table via. foreign key. After which populate the drop-lower at UI in the VEHICLE_TYPE table. Keep value within the vehicle object as String

    - Cons:

    i). No validation at object level

    ii). Require a join at DB to retrieve a vehicle object

    - Pros:

    i). validation from the value at DB level (by foreign key)

    ii). User can also add a brand new value towards the list at runtime

OR

  • Create a separate table VEHICLE_TYPE within the DB getting all of the 3 values but DON'T link it using the VEHICLE table via. foreign key. After which populate the drop-lower at UI in the VEHICLE_TYPE table. Keep value within the vehicle object as well as in the DB as String

    - Cons:

    i). No validation at object level

    ii). No validation at DB level

    - Pros:

    i). No join needed at DB level

    ii). User can also add new value towards the list

OR

  • Create a separate table VEHICLE_TYPE within the DB getting all of the 3 values and link it using the VEHICLE table via. foreign key. After which populate the drop-lower at UI in the VEHICLE_TYPE table. Make an enum VehicleType in java after which give a area VehicleType vehicleType within the Vehicle class. Store something in the VehicleType enum within the vehicleType area in line with the input from the user.

    -Cons:

    i). Will need to update their email list at two places: VehicleType enum and also the VEHICLE_TYPE table. Could cause inconsistency.

    ii). User can't give a new value towards the list (he is able to give a value within the table but can't alter the enum)

    - Pros:

    i). validation at UI level

    ii). validation at object level

    iii). validation at DB level

Question: Can there be other through which we are able to carry out the above task which does not have any of these disadvantages?

Sure. Your next one having a modification:

Create a separate table VEHICLE_TYPE within the DB getting all of the 3 values and link it using the VEHICLE table via. foreign key. After which populate the drop-lower at UI in the VEHICLE_TYPE table. Keep value within the vehicle object as String. When calling vehicle.setVehicleType(), verify the value designated applies by checking the potential values in the DB. Whether it's invalid, throw an InvalidArgumentException or perhaps a subclass.

Now you must validation within the object. As well as, I do not consider needing to perform a enroll in a disadvantage. You cannot do much else without joining tables. That's how you get many tables.