Hibernate API supports Inheritance mapping in 3 ways
Table per class hierarchy
Table per subclass
Table per concrete class
Table per Concrete class
In this example we will see how to implement one Table per Concrete class , it means each concrete class is mapped as normal persistent class and mapping of the subclass repeats the properties of the parent class.
Let us consider Vehicle as parent class and Bus, Car are the subclasses, as per Table per concrete class three table will create and in the sub class table will get the properties of the super class Vehicle.
Final table structure (Each sub table will have the properties of parent class e.g.: Vehicle Type)
1. Create a Java project
2. Create Vehicle class (Vehicle.java)
3. Create Bus.java (Sub class)
4. Create Car.java (Sub class)
In the above two sub classes we used two additional annotations ie
@AttributeOverrides annotation is used to override mappings of multiple properties or fields and
@AttributeOverride annotation is used to override the mapping of property.
5. Create Hibernate config file (hibernate.cfg.xml)
6. Create Main class
Create a main class to create and save Vehicle objects.