While using multiple inheritance in domain model design you need to be cautious as programming languages such as Java do not support it. Hence, for designing such a domain model, GoF structural design pattern – Bridge pattern can be used. There are two layers of abstraction used in Bridge pattern.
In my blog on Bridge pattern, I had written about how we had implemented it (see Figure 1).
Figure 1. UML class diagram – Bridge Pattern
In this blog, I’m writing about implementing this domain model using JPA 2.0. In Figure 1. OrderableItem is an interface. An object of type OrderableItem can either be an instance of Product or ProductSuite. For implementing the domain model of Figure 1. in JPA 2.0, we will have to use a collection of OrderableItem in Order entity. However, in JPA if we use property of type interface then we need to specify its implementing entity class using targetEntity attribute of @OneToOne, @OneToMany, @ManyToOne and @ManyToMany annotations (in the above case it is @OneToMany). Since the entity class of OrderableItem can vary in each order, it cannot be hardcoded using the targetEntity attribute. Thus, to implement this domain model, we will have to use OrderableItem as an abstract class as given in Figure 2. instead of an interface.
Figure 2. Using Bridge Pattern in JPA domain model
Single Table Inheritance strategy of JPA 2.0 can be used for OrderableItem and its subclasses whereas for Order and its subclasses, Joined Table Inheritance strategy can used.
While designing domain model, avoid using multiple inheritance directly in the design since some programming languages do not support multiple inheritance. GoF structural design pattern – Bridge pattern can be used instead in the domain model design. JPA 2.0 provides three inheritance strategies which can be used as appropriate to implement the two levels of abstraction in Bridge pattern.