To do this generically, be generic about it.
Instead of having subclasses of MenuItem
just use a single class MenuItem
and have a HashMap
within it. The HashMap
then maps from keys to values for all the properties of that MenuItem
. You can then store them in the database using a one-to-many relationship linking the MenuItem
table with a MenuItemProperties
table.
Trying to do it the way you were doing falls down quickly, because you can't actually usefully do anything with MenuItem
without knowing what type it is.
Note also that you are directly inserting values into your query string. While that's safe enough for integer values it's still bad practice and it is very dangerous is you ever have String
values.