2 Replies Latest reply on Jan 13, 2008 5:33 PM by gduq

    Representing many kinds of entities in DB with two tables us

    gduq

      We have an application for submitting application forms... LOTS of forms. The forms have different fields (obviously) and different validation requirements for the fields.

      We are trying to avoid having to create a new table for each form. So we thought we could do this -> have two tables in the DB 'FORMS' and 'FORM_FIELD_VALUES' 'FORM_FIELD_VALUES' having the form, filed name and field value for any form. Problem solved! right? well so we thought anyway until we tried to figure out how to create the model classes and bind the views to them.

      I had a couple of ideas of how to do this but they all seam flawed:

      First Idea:
      Since I like the validation annotations it seems like I will need to have one model class for each type of form with the fields and their respective validation annotations. And then have the controller convert this into a form object with its collection of formFieldValues before saving it. The problem is that to use the validation annotations on the model the classes need to be '@Entitiy's and therefore need to map directly to tables...so again we end up with one table per form (albeit empty tables)

      Second Idea:
      Use a single groovy class for the Form model with a "fieldValues" hash populated with keys based on what kind of form it is. Use groovy to do method-missing style metta programming magic to present this as a bean setters and getters don't really exist but they work anyway if the key is in the hash. Down side is doing this seems like it would be complex and I would be able to use the lovely validation annotations.


      Any advise on a better way to do this would be hugely appreciated.