Page 6 of 6
Joined-table inheritance
In the joined-table inheritance strategy, the common states of the class are stored in one table, and the state of the subclass is stored in another table that is joined to the first table, as shown in Table 3.
Table 3. Joined-table inheritance mapping strategy
ENTITY | TABLE NAME |
---|---|
Customer | CUSTOMER |
OnlineCustomer | ONLINECUSTOMER (only Website information is stored here; the rest of the information is stored in the CUSTOMER table) |
The common data that the OnlineCustomer
entity shares with Customer
is stored in the CUSTOMER table; OnlineCustomer
-specific data is stored in the ONLINECUSTOMER table, connected by a foreign key constraint. From the JPA implementation standpoint, the only change needed in the OnlineCustomer
entity is that the JOINED
strategy must be provided, as in Listing 17.
Listing 17. A superclass in joined-table inheritance
@Entity(name = "CUSTOMER") //Name of the entity
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name="CUST_TYPE", discriminatorType=DiscriminatorType.STRING,length=10)
@DiscriminatorValue("RETAIL")
public class Customer implements Serializable{
@Id //signifies the primary key
@Column(name = "CUST_ID", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private long custId;
@Column(name = "FIRST_NAME", nullable = false,length = 50)
private String firstName;
@Column(name = "LAST_NAME", length = 50)
private String lastName;
@Embedded
private Address address = new Address();
@Column(name = "CUST_TYPE", length = 10)
private String custType;
.................
}
In the OnlineCustomer
entity, shown in Listing 18, you specify the subclass-specific attributes and a foreign key join column with the @PrimaryKeyJoinColumn
annotation, which maps to the primary key of the parent table.
Listing 18. A sample subclass in joined-table inheritance
@Table(name="ONLINECUSTOMER")
@Entity(name = "ONLINECUSTOMER") //Name of the entity
@DiscriminatorValue("ONLINE")
@PrimaryKeyJoinColumn(name="CUST_ID",referencedColumnName="CUST_ID")
public class OnlineCustomer extends Customer{
@Column(name = "WEBSITE", length = 100)
private String website;
................
}
In Listing 18, the name
property of @PrimaryKeyJoinColumn
denotes the primary key of the subclass table. referencedColumnName
denotes the name of the superclass table column to which this subclass table column joins. Nothing changes in the way in which you persist and fetch the Customer
and OnlineCustomer
objects.
Table per class inheritance
In the table per class inheritance strategy, each class state in a hierarchy is stored in a separate table, as illustrated in Table 4.
Table 4. Table per class inheritance mapping strategy
ENTITY | TABLE NAME |
---|---|
Customer | CUSTOMER |
OnlineCustomer | ONLINECUSTOMER |
You don't need to provide the @DiscriminatorColumn
annotation here, as the entities are stored in separate tables altogether. Also, no @PrimaryKeyJoinColumn
annotation is required, as no relationship exists between subclass and superclass tables. The Customer
superclass will look like Listing 19.
Listing 19. A sample superclass in table per class inheritance
@Entity(name = "CUSTOMER")
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class Customer implements Serializable{
@Id //signifies the primary key
@Column(name = "CUST_ID", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private long custId;
@Column(name = "FIRST_NAME", nullable = false,length = 50)
private String firstName;
@Column(name = "LAST_NAME", length = 50)
private String lastName;
@Embedded
private Address address = new Address();
...........
}
OnlineCustomer
will be like a normal subclass, as shown in Listing 20. Nothing changes in the way in which you persist and fetch the Customer
and OnlineCustomer
objects.
Listing 20. A sample subclass in table per class inheritance
@Entity(name = "ONLINECUSTOMER") //Name of the entity
public class OnlineCustomer extends Customer{
@Column(name = "WEBSITE", length = 100)
private String website;
.................
}
Toward a more object-oriented world
In this first half of "Understanding the Java Persistence API," you saw how you can apply object-oriented concepts like inheritance and event callbacks using JPA. The object-oriented possibilities in JPA go far beyond what has been covered in this article: JPA also lets you write JPQL queries (similar to Hibernate's HQL queries) or native SQL queries, and has transaction management facilities.
In the second half of this article, you'll have the opportunity to explore data relationships the JPA way -- that is, with object-oriented grace. Look for that article next week. In the meantime, have fun on your own, exploring JPA's object-oriented paradigm of data persistence!