Basically we have a spring boot application that requires that the user can define his/her own set of fields and that these fields should be persisted in their own class/tab
Hibernate maps entities to tables, and the metadata is built during bootstrap. So, you can't modify it on the fly while the application is running.
However, as long as you keep on adding new tables without modifying the existing structure, you can address this issue at the architecture level:
Or, just use a NoSQL database like MongoDB with Hibernate OGM since your requirements do not fit very well into a relational database anyway.
But, if you already use a RDBMS, then it's simpler to just use JSON instead of switching to a NoSQL database just for that reason.
Firstly, the problem is how to add entity class in hot-deploy. We could do it by some tools of swapping (spring-boot-devtools, or maven copy resource). Secondly, to architect the different models of an entity, could use JPA Inheritance(https://en.wikibooks.org/wiki/Java_Persistence/Inheritance) or JPA row mapper.
But, I see it is more easily to persistence JSON object as text in the database, and left the consumer(front or another service) of the service to parse it.
Another way to do it, is to try load class in runtime by its class path. I will try to persistence Json object as text and its type in ddbb. Then in application.properties create the mapping of its type and classpath (the class java of the data), then do something like:
static{
Map<String, String> typeClassPathMap = ......// from properties
File file = new File("c:\\class-path\\");
// Convert File to a URL
URL url = file.toURL();
// file:/c:/myclasses/
URL[] urls = new URL[]{url};
ClassLoader cl = new URLClassLoader(urls);
}
Class<?> loadClass(String type){
Class clazz = urlClassLoader.loadClass(typeClassPathMap.get(type));
}
It will read class in runtime and is configurable by properties. After that, for each type of data, we define su type and class path in the property file. When data come from ddbb, we use its type to get the java class, and parse it to object. When we need to create new type of data, we left the class in class-path, and configure it in properties.