This tutorial discusses what are the types of inheritance models in Hibernate and describes how they work like vertical inheritance and horizontal.
3. With Joined Strategy Single Table Strategy
In Single table per subclass, the union of all the properties from the inheritance hierarchy is mapped to one table. As all the data goes in one table, a discriminator is used to differentiate between different type of data.
Advantages of Single Table per class hierarchy Simplest to implement.
Only one table to deal with.
Performance wise better than all strategies because no joins or sub-selects need to be performed.
Disadvantages:
Most of the column of table are nullable so the NOT NULL constraint cannot be applied. Tables are not normalized.
Lets see the following example code. Vehicle.java view plainprint? 1. package com.sdnext.hibernate.tutorial.dto; 2. 3. import javax.persistence.Column; 4. import javax.persistence.Entity; 5. import javax.persistence.GeneratedValue; 6. import javax.persistence.GenerationType; 7. import javax.persistence.Id; 8. import javax.persistence.Inheritance; 9. import javax.persistence.InheritanceType; 10. import javax.persistence.Table; 11. 12. @Entity 13. @Table(name="VEHICLE")
14. @Inheritance(strategy=InheritanceType.SINGLE_TABLE) //Least normalisation strate gy
15. public class Vehicle 16. {
17. @Id
18. @GeneratedValue(strategy=GenerationType.AUTO) 19. @Column(name="VEHICLE_ID")
20. private int vehicleId; 21.
22. @Column(name="VEHICLE_NAME") 23. private String vehicleName; 24.
25. public int getVehicleId() { 26. return vehicleId;
27. }
28. public void setVehicleId(int vehicleId) { 29. this.vehicleId = vehicleId;
30. }
31. public String getVehicleName() { 32. return vehicleName;
34. public void setVehicleName(String vehicleName) { 35. this.vehicleName = vehicleName; 36. } 37. } TwoWheeler.java view plainprint? 1. package com.sdnext.hibernate.tutorial.dto; 2. 3. import javax.persistence.Column; 4. import javax.persistence.Entity; 5. import javax.persistence.Table; 6. 7. @Entity 8. @Table(name="TWO_WHEELER") 9. //@DiscriminatorValue("Bike")
10. public class TwoWheeler extends Vehicle 11. {
12. @Column(name="STEERING_TYPE") 13. private String steeringTwoWheeler; 14.
15. public String getSteeringTwoWheeler() 16. {
17. return steeringTwoWheeler; 18. }
19.
20. public void setSteeringTwoWheeler(String steeringTwoWheeler) 21. { 22. this.steeringTwoWheeler = steeringTwoWheeler; 23. } 24. } FourWheeler.java view plainprint? 1. package com.sdnext.hibernate.tutorial.dto; 2. 3. import javax.persistence.Column; 4. import javax.persistence.Entity; 5. import javax.persistence.Table; 6. 7. @Entity 8. @Table(name="FOUR_WHEELER") 9. //@DiscriminatorValue("Car")
10. public class FourWheeler extends Vehicle 11. {
12. @Column(name="STEERING_TYPE") 13. private String steeringFourWheeler; 14.
15. public String getSteeringFourWheeler() 16. {
17. return steeringFourWheeler; 18. }
19.
20. public void setSteeringFourWheeler(String steeringFourWheeler) 21. {
23. } 24. } hibernate.cfg.xml view plainprint? 1. <hibernate-configuration> 2. <session-factory>
3. <!-- Database connection settings -->
4. <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 5. <property name="connection.url">jdbc:mysql://localhost:3306/vehicleDB2</prope rty>
6. <property name="connection.username">root</property> 7. <property name="connection.password">root</property> 8.
9. <!-- JDBC connection pool (use the built-in) --> 10. <property name="connection.pool_size">1</property> 11.
12. <!-- SQL dialect -->
13. <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 14.
15. <!-- Enable Hibernate's automatic session context management --> 16. <property name="current_session_context_class">thread</property> 17.
18. <!-- Disable the second-level cache -->
19. <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</pr operty>
20.
21. <!-- Echo all executed SQL to stdout --> 22. <property name="show_sql">true</property> 23.
24. <!-- Drop and re-create the database schema on startup --> 25. <property name="hbm2ddl.auto">create</property>
26. 27.
28. <mapping class="com.sdnext.hibernate.tutorial.dto.Vehicle"> 29. <mapping class="com.sdnext.hibernate.tutorial.dto.TwoWheeler"> 30. <mapping class="com.sdnext.hibernate.tutorial.dto.FourWheeler"> 31.
32. </mapping></mapping></mapping></session-factory> 33. </hibernate-configuration>
Now run the following test class HibernateTestDemo.java view plainprint? 1. package com.sdnext.hibernate.tutorial; 2. 3. import org.hibernate.Session; 4. import org.hibernate.SessionFactory; 5. import org.hibernate.cfg.AnnotationConfiguration; 6. 7. import com.sdnext.hibernate.tutorial.dto.FourWheeler; 8. import com.sdnext.hibernate.tutorial.dto.TwoWheeler; 9. import com.sdnext.hibernate.tutorial.dto.Vehicle; 10.
11. public class HibernateTestDemo { 12.
14. * @param args 15. */
16. public static void main(String[] args) 17. {
18. SessionFactory sessionFactory = new AnnotationConfiguration().configure().buil dSessionFactory();
19. Session session = sessionFactory.openSession(); 20. session.beginTransaction();
21.
22. Vehicle vehicle = new Vehicle(); 23. vehicle.setVehicleName("Car"); 24.
25. TwoWheeler twoWheeler = new TwoWheeler(); 26. twoWheeler.setVehicleName("Bike");
27. twoWheeler.setSteeringTwoWheeler("Bike Steering Handle"); 28.
29. FourWheeler fourWheeler = new FourWheeler(); 30. fourWheeler.setVehicleName("Alto");
31. fourWheeler.setSteeringFourWheeler("Alto Steering Wheel"); 32. 33. session.save(vehicle); 34. session.save(twoWheeler); 35. session.save(fourWheeler); 36. 37. session.getTransaction().commit(); 38. session.close(); 39. } 40. }
In the above table Vehicle there are four columns (DTYPE, VEHICLE_ID, VEHICLE_NAME, STEERING_TYPE).
The first column has the value of discriminator type(DTYPE) is Vehicle, TwoWheeler, FourWheeler as its entity name by default.
For user convenience we can override the default value of column as well as column name by using the following annotation.
@DiscriminatorColumn Target:
Classes
Specifies the discriminator column for the SINGLE_TABLE and JOINED Inheritance mapping strategies. The strategy and the discriminator column are only specified in the root of an entity class hierarchy or subhierarchy in which a different inheritance strategy is applied
If the DiscriminatorColumn annotation is missing, and a discriminator column is required, the name of the discriminator column defaults to "DTYPE" and the discriminator type to DiscriminatorType.STRING. @DiscriminatorValue
Target: Classes
Specifies the value of the discriminator column for entities of the given type.
The DiscriminatorValue annotation can only be specified on a concrete entity class.
If the DiscriminatorValue annotation is not specified and a discriminator column is used, a provider- specific function will be used to generate a value representing the entity type. If the DiscriminatorType is STRING, the discriminator value default is the entity name.
The inheritance strategy and the discriminator column are only specified in the root of an entity class hierarchy or subhierarchy in which a different inheritance strategy is applied. The discriminator value, if not defaulted, should be specified for each entity class in the hierarchy.
@Inheritance Target: Classes
Defines the inheritance strategy to be used for an entity class hierarchy. It is specified on the entity class that is the root of the entity class hierarchy. If the Inheritance annotation is not specified or if no
inheritance type is specified for an entity class hierarchy, the SINGLE_TABLE mapping strategy is used. Now adding the following annotation to the Vehicle class is
view plainprint?
1. @Entity
2. @Table(name="VEHICLE")
3. @Inheritance(strategy=InheritanceType.SINGLE_TABLE) //Least normalisation strate gy
4. @DiscriminatorColumn( 5. name="VEHICLE_TYPE",
6. discriminatorType=DiscriminatorType.STRING 7. )
8. public class Vehicle 9. {
Now adding following annotation to the TwoWheeler class view plainprint?
1. @DiscriminatorValue("Bike")
2. public class TwoWheeler extends Vehicle 3. {
Now adding following annotation to the FourWheeler class view plainprint?
1. @DiscriminatorValue("Car")
2. public class FourWheeler extends Vehicle 3. {
After these above modification we run the code then we will get the following output.