JPA Hibernate Dynamic entity mapping & persistence at runtime

后端 未结 2 1123
感动是毒
感动是毒 2021-01-19 21:55

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

2条回答
  •  长发绾君心
    2021-01-19 22:53

    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 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.

提交回复
热议问题