问题描述
我是Java开发人员.我正在使用 spring 4.0.1 和休眠4.2.21 .我有一堂课,如下:
I'm a java developer. I'm using spring 4.0.1 and hibernate 4.2.21. I have a class as follow:
@Entity
@Inheritance(...)
public abstract class Feature{
@Id
@GeneratedValue
protected Long id;
...
}
现在我有很多课,如下:
Now I have some many class as follow:
Label.java类:
@Entity
public class Label extends Feature{
protected String str;
...
}
Point.java类:
@Entity
public class Point extends Feature{
protected Integer intg;
...
}
我有20多个从Feature
类扩展的Entity类.有什么方法可以在不编写硬代码的情况下将此类动态添加到项目中(例如Label
和Point
)?
I have more than 20 Entity class that extends from Feature
class. Is there any way to add dynamically this classes(such as Label
and Point
) to the project without writing hard code?
更新:
例如,Hibernate从数据库获取数据,然后根据该数据创建模型.
For example, Hibernate get data from a database and then according this data, create models.
- 有可能吗?
- 我该怎么办?
推荐答案
您可以尝试收集所需的数据以构建模型,并为每个实体生成一个休眠的hbm.xml
文件(是xml格式,易于使用Java生成)读取您在更新中描述的所需数据后
You can try to collect the needed data to build the model and generate a hibernate hbm.xml
file for each entity (is xml format and easy to generate with java after reading the data needed as you describe in your update)
我认为通过这种方法,如果我对您的问题很了解,就能实现您想要的目标.
I Think with that approach you can achieve what you want if I understand well your question.
这篇关于如何在休眠中动态添加实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!