I have followed Balusc’s 1st method to create dynamic form from fields defined in database.
I can get field names and values of posted fields.
But I am confused about how to save values into database.
- Should I precreate a table to hold values after creating form and
save values there manually (by forming SQL query manually)? - Should I convert name/value pairs to JSON objects
and save? - Should I create a simple table with id,name,value field and
save name/value pairs here (Like EAV Scheme)?
Or is there any way for persisting posted values into database?
Regards
It look like that you’re trying to work bottom-up instead of top-down.
The dynamic form in the linked answer is intented to be reused among all existing tables without the need to manually create separate JSF CRUD forms on “hardcoded” Facelets files for every single table. You should already have a generic model available which contains information about all available columns in the particular DB table (which is
Fieldin the linked answer). This information can be extracted dynamically and generically via JPA metadata information (how to do that in turn depends on the JPA provider used) or just via good ‘ol JDBCResultSetMetaDataclass once during application’s startup.If you really need to work bottom-up, then it gets trickier. Creating tables/columns during runtime is namely a very bad design (unless you intend to develop some kind of DB management tool like PhpMyAdmin or so, of course). Without the need to create tables/columns runtime, you should basically have 3 tables:
Then you should link them together by FK relationships.