How to dynamically add Entity in Hibernate?

Morteza Malvandi picture Morteza Malvandi · Nov 25, 2015 · Viewed 10k times · Source

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?

Answer

Shafin Mahmud picture Shafin Mahmud · Dec 2, 2015

I think its not a good database design that needs to be changed dynamically. It sounds verbose and not consistent. Observe your domain again and try to design a proper entity relationships that wouldnt be changed over run time.