在运行时动态创建表和 java 类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1192031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dynamically create table and java classes at runtime
提问by
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?
我的申请中有一个要求。我的表不会预先定义。例如,如果用户创建一个名为 Student 的表单,并添加其属性,如名称、卷号、主题、类等。那么在运行时,应该有一个名为 student 的表列名称、卷号、主题、类及其相关类及其休眠映射文件。有没有办法这样做?
Thanks in advance,
提前致谢,
Rima Desai
里玛·德赛
回答by Gregory Mostizky
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 Velocityin the past and it was very good for this task, but there are others too if you want to try them.
但一般来说,是的,您可以根据某些输入动态生成数据库表、休眠类和映射。最简单的方法是使用一些模板引擎。我过去使用过Velocity,它非常适合这项任务,但如果您想尝试,还有其他的。
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.
按照 OP 说明,更好的方法是使用 XML 来存储用户定义的数据。上面的解决方案是好的,但是无论表单是否更改,都需要重新编译应用程序。如果您不想在每次用户编辑后停止并重新编译,XML 是更好的答案。
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.
给定表单的定义,保存和加载保存为 XML 的数据将是微不足道的。
Add a comment if you would like some more clarifications.
如果您需要更多说明,请添加评论。
回答by Paul Morie
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:
Hibernate 支持动态模型,即在运行时定义的实体,但你必须写出一个映射文件。关于动态模型,您应该注意以下几点:
You may be restricted in how you definethese at run-time (viz. you will have to use the
Sessiondirectly instead of using a helper method fromHibernateTemplateor something like that).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).
您在运行时定义这些的方式可能会受到限制(即,您必须
Session直接使用 the而不是使用辅助方法 fromHibernateTemplate或类似的东西)。使用
Maps 作为实体字段的容器支持动态模型,因此您将在运行时丢失输入和 POJO 样式的 API(除了内置的动态模型支持之外,不会做任何事情)。
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.
说了这么多,您没有提到动态定义的表是否需要跨应用程序会话保持持久性。这可能会使事情复杂化。
回答by user578182
last week I was looking for same solution and then I got idea from com.sun.tools.javac.Main.compileclass, you create the Entity class using java IOand compile using java tools, for this you need tools.jarto locate on CLASS_PATH, now I am looking for run time hibernatemapping 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.
上周我正在寻找相同的解决方案,然后我从com.sun.tools.javac.Main.compile课堂上得到了想法,您使用创建实体类java IO并使用编译java tools,为此您需要tools.jar定位CLASS_PATH,现在我正在寻找hibernate无需重新启动的运行时映射。有人在帖子中就这种类型的要求说“但不清楚你为什么要这样做”,答案是这个要求是针对CMS(Content Management System). 我也在做同样的事情。代码如下。
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);
}

