Java Magazine, May/June 2018
ORACLE COM JAVAMAGAZINE MAY JUNE 2018 28 design patterns Lets start by noting that relational databases support only the concept of composition but not the concept of inheritance Composition models a has a association as foreign key associations between one or more database tables In your domain model you can implement composition with an annotation that defines the type of association such as @ ManyToMany @ ManyToOne or @ OneToMany and an attribute of the type List or Set or of the type of the associated entity The following code snippet shows an example of a simple many to one association between an Employee and a Department entity @ Entity public class Employee @ ManyToOne @ JoinColumn name department_ id private Department department As you can see in this code composition is not only well supported by relational databases but is also easy to map in your domain model This makes it a great choice Inheritance on the other hand models an is a association This kind of association cant be modeled in a relational table model Thats why the JPA specification defines a set of mapping strategies that enable you to map an inheritance hierarchy to one or more relational database tables You can choose from the following A mapped superclass strategy which maps all subclasses as entities to their own independent database table without supporting polymorphic queries A table per class strategy which models all classes including the superclass as entities and maps them to independent database tables This strategy supports polymorphic queries A joined strategy which maps the specific attributes of each entity to its own database table but does not include the attributes of the superclass So whenever you want to fetch one of
You must have JavaScript enabled to view digital editions.