Dynamically create table and java classes at runtime

送分小仙女□ 提交于 2019-11-30 19:28:42

问题


I have a requirement in my application. My tables wont be defined beforehand.For Example,if the user creates a form by name Student,and adds its attributes like name,roll no,subject,class etc.. then on runtime,there should be a table created by name student with columns name,roll no,subject,class and also its related class and its hibernate mapping file. Is there any way of doing so?

Thanks in advance,

Rima Desai


回答1:


It's possible, but it's not clear why would you want to do something like that, so it's hard to suggest any specific solution.

But generally, yes, you can generate database tables, hibernate classes and mappings dynamically based on some input. The easiest approach is to use some template engine. I've used Velocity in the past and it was very good for this task, but there are others too if you want to try them.

EDIT:

Following OP clarification the better approach is to use XML to store user defined data. The above solution is good but it requires recompiling the application whether forms are changed. If you don't want to stop and recompile after each user edit, XML is much better answer.

To give you some head start:

@Entity
public class UserDefinedFormData {
    @Id
    private long id;

    @ManyToOne
    private FormMetadata formMetadata;

    @Lob
    private String xmlUserData;
}

Given a definition of the form it would trivial to save and load data saved as XML.

Add a comment if you would like some more clarifications.




回答2:


Hibernate supports dynamic models, that is, entities that are defined at run-time, but you have to write out a mapping file. You should note a couple things about dynamic models:

  1. You may be restricted in how you define these at run-time (viz. you will have to use the Session directly instead of using a helper method from HibernateTemplate or something like that).

  2. Dynamic models are supported using Maps as the container for the fields of an entity, so you will lose typing and a POJO-style API at run-time (without doing something beyond the baked-in dynamic model support).

All of that said, you didn't mention whether it was a requirement that the dynamically defined tables be persistent across application sessions. That could complicate things, potentially.




回答3:


last week I was looking for same solution and then I got idea from com.sun.tools.javac.Main.compile class, you create the Entity class using java IO and compile using java tools, for this you need tools.jar to locate on CLASS_PATH, now I am looking for run time hibernate mapping without restart. some one was saying in the post regarding to this type of requirement that "but it's not clear why would you want to do something like that" answer is this requirement is for CMS(Content Management System). and I am doing the same. code is as below.

 public static void createClass()
{
String methodName=“execute”;
String parameterName=“strParam”;
try{
//Creates DynamicTestClass.java file
FileWriter fileWriter=new FileWriter(fileName,false);
fileWriter.write(“public class “+ className +” {\n”);
fileWriter.write(“public String “+methodName +“(String “+parameterName+“) {\n”);
fileWriter.write(“System.out.println(\” Testing\”);\n”);
fileWriter.write(“return “+parameterName +“+ \” is dumb\”;\n }\n}”);
fileWriter.flush();
fileWriter.close();

String[] source = { new String(fileName) };
com.sun.tools.javac.Main.compile(source);
}


来源:https://stackoverflow.com/questions/1192031/dynamically-create-table-and-java-classes-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!