I have a set of pre-generated entity beans and they are mapped to tables with some columns not having a matching bean property. During the generation of the entity beans the final DB schema is unknown. I want to add a single property of the type
Map<String, Object> to load these extra columns from the tables and of course persist them back to the tables.
The entity beans are unannotated. Instead I generate a mapping XML when the final DB schema is already known. I read that if you omit the
class attribute in the
<entity> element then Hibernate will instantiate a regular Map, but I need a mixture of a java bean and a map.
The short answer is, you can’t have dynamic columns. If you want to store extra state, I’d recommend you to use a single JSON column and map that as
Map<String, Object> or something like that, but in general, all of this will lead to massive pain during programming.
Use proper Java types to model your data and don’t try to overengineer your model by introducing all sort of dynamicity.
If you really really want dynamic models, you are in for a lot of pain. You will have to code everything against
java.util.Map and rebuild the
SessionFactory based on new mapping information. Also, you will have to synthesize the mapping information somehow into the hbm.xml format of Hibernate: Hibernate ORM 6.2.2.Final User Guide
I know some people are masochistic enough to endure the pain of programming this way, so it is possible to do it.