I have an interface and three classes which implements that interface and i should use Hibernate framework to save them to the database. The problem is that, it should create different tables depending on one of the three classes. For example,
interface Vehicle;
class Truck;
class Bus;
class Motorbike;
Vehicle vehicle = new Truck(); // In that case, Truck table should be generated
session.save(vehicle); // In that case, Truck table should be generated
Vehicle vehicle = new Bus();
session.sav开发者_如何学运维e(vehicle); // In that case, Bus table should be generated
Vehicle vehicle = new Motorbike();
session.save(vehicle); // In that case, Motorbike table should be generated
How can i do this with annotations? Any help will be appreciated. Thank you.
Hibernate inspects your classes at runtime, so it doesn't matter whether you refer to your object by class or by interface.
Note that it won't create tables when you call session.save()
. It will insert records in the already existing tables. If you want tables created by hibernate, look for hibernate.hbm2ddl.auto
精彩评论