如何在休眠中动态添加实体? [英] How to dynamically add Entity in Hibernate?

查看:137
本文介绍了如何在休眠中动态添加实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是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类.有什么方法可以在不编写硬代码的情况下将此类动态添加到项目中(例如LabelPoint)?

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.

  1. 有可能吗?
  2. 我该怎么办?

推荐答案

您可以尝试收集所需的数据以构建模型,并为每个实体生成一个休眠的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.

这篇关于如何在休眠中动态添加实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆