Table Per Class Hierarchy


Table Per Class Hierarchy

Using an XML file, create a Hibernate Table Per Hierarchy. Using this inheritance technique, we can map the entire hierarchy with a single table. An extra column (also known as a discriminator column) is added to the database to identify the class.

If we want to put the data of all classes hierarchy into a single database table, we may use the Hibernate Table per Class approach. We can store all of the data for all classes in a single table using this technique.

[sql] [sql] [sql] [sql] [sql]

ENGINE=InnoDB, CREATE TABLE payment ( paymode varchar(6) NOT NULL, payid int(11) NOT NULL, amount double DEFAULT NULL, chqnumber int(11) DEFAULT NULL, chtype varchar(10) DEFAULT NULL, ccnumber int(11) DEFAULT NULL, cctype varchar(10) DEFAULT NULL, CHARSET=latin1 BY DEFAULT

[/sql]

@Table(name = "PRODUCT") @Entity
public class Product @Inheritance(strategy = InheritanceType.TABLE PER CLASS) @Inheritance(strategy = InheritanceType.TABLE PER CLASS) @Inheritance(strategy = Inher

private Long id @Id @GeneratedValue @Id @GeneratedValue @Id @GeneratedValue @Id @


name (private String);

/
public class @Entity a private int brand; a private int model; a private int brand; a private int model; a private int brand; a private int model;
public class @Entity Toy extends Product's minimalAge private int;

Because the default strategy is Single Table, we must explicitly declare it in the @Inheritance annotation at the root object (described here). And this is the only information we should supply - the strategy doesn't require a discriminator column, subclasses don't require any unique configuration, and the @DiscriminatorValue annotation may even be used to specify discriminator values.