0

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.save(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.

1
  • Just use it as you use any other class with hibernate. Nothing seems to be odd here. Commented Aug 5, 2011 at 6:59

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

I solved it by adding "@MappedSuperclass" to the top of abstract class. Thank you.
ok, you solved what I didn't understand you had as a problem. For the next time - include the exact problem in the question description

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.