6

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/table through JPA/Hibernate at runtime. These classes will be generated dynamically through bytebuddy.

All that should be done dynamically without having to restart the application. The Hibernate Dynamic mapping is not an option, since we will be creating new classes entirely and re-map them.

I have also considered an EAV model but that will not work since we will need separate tables for each set of data so the JSON cannot be mixed in the same table.

The first solution that I'm considering is to proxy the EntityManagetFactory and when we have a new entity to map , I'll re-create the EntityManagetFactory and add the new mapped entity to it, I'll also have the hbm2ddl.auto set to "update" in order to ensure that the new tables scheme will get created.

Problem is that I don't know what other classes that might need proxying, I believe I'll have to proxy the Hibernate SessionFactory but I'm not sure how many other classes that will need to be re-created and proxied and I believe that this is a complex path to go.

The other solution is to mix between the SQL and NoSQL solution by using Hibernate OGM, but in that case I'll loose any relationship that I can have with the existing SQL entities and I'm not in favor of running a second NoSQL DB.

is there are any other solutions I can explore ?

Edit:

I would use bytebuddy in order to generate the new classes dynamically and they would have the @Entity annotation, the generated classes are written into a temporary jar file ( e.g. /tmp/myjar.jar )

Using a BeanPostProcessor.postProcessAfterInitialization I would replace the LocalContainerEntityManagerFactoryBean with a proxy class.

Also I used the LocalContainerEntityManagerFactoryBean .setPersistenceUnitPostProcessors to add an extra processor that would process the classes from the newly created jar

So now after creating the new class with bytebuddy I would manually call the LocalContainerEntityManagerFactoryBeanProxy.afterProperties that does all the job of bootstrapping the JPA and hibernate layer , I do also set the "hibernate.hbm2ddl.auto" property to "update" so that the schema would be created ( I know that this is risky to be done in a production environment )

2

2 Answers 2

1

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:

  1. You make the class changes you need.
  2. You build the project artifacts.
  3. You deploy the new project artifacts to a new server.
  4. You switch traffic from the old server instance to the new one from the load balancer without any downtime.

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.

3
  • Thanks for the feedback , but basically we cannot rebuild the application since the user will expect to create and fill the tables and add entries at runtime. will the JSON be supported on multiple DB providers ? we are supposed to support postgres/h2/mysql and will it allow is to query specific field values ?
    – Ramy Siha
    Nov 16, 2017 at 13:46
  • 2
    Is this still true, in 2020, that JPA entity cannot be defined dynamically at runtime?
    – user555945
    Jul 8, 2020 at 5:14
  • If you definite the entity classes dynamically or the XML mappings and bootstrap the EntityManagerFactory, it woll work just fine Jul 8, 2020 at 5:42
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.