How to dynamically add Entity in Hibernate?

2020-02-28 00:20发布

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 class:

@Entity
public class Label extends Feature{
   protected String str;

   ...
}

Point.java class:

@Entity
public class Point extends Feature{
   protected Integer intg;

   ...
}

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?

update:

For example, Hibernate get data from a database and then according this data, create models.

  1. Is it possible?
  2. How do I do?

7条回答
爷的心禁止访问
2楼-- · 2020-02-28 01:17

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)

After that, you can create programmatically a hibernate configuration object following this http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/session-configuration.html#configuration-programmatic

I Think with that approach you can achieve what you want if I understand well your question.

查看更多
登录 后发表回答