Monday, December 14, 2015

Business Component Development with EJB Technology - Module 06

Module 6
Implementing Entity Classes : Modelling Inheritance and Embedded Relationships

Objectives

  • Examine entity inheritance 
  • Examine object/relational inheritance hierarchy mapping strategies 
  • Inherit from an entity class 
  • Inherit from a mapped superclass 
  • Inherit from a non-entity class 
  • Examine inheritance mapping strategies 
  • Use an embedded class 
  • Use a composite primary key


Examining Entity Inheritance

Key decisions associated with using entity inheritance

  • The choice of class for the inheriting classes’ superclass 
  • The choice of object/relational mapping strategy

Choices for selecting the entity superclass


  • An entity class (abstract or concrete) 
  • A mapped superclass (abstract or concrete) 
  • A non entity class (abstract or concrete)

Available object/relational mapping strategies
  • Single table per class hierarchy strategy 
  • Table per class strategy 
  • Joined subclass strategy
Specifying the object/relational mapping strategies
  • Inheritance annotation 
  • inheritance element in DD

Examining Object / Relational Inheritance Hierarchy Mapping Strategies



Mapping Inheritance Using the Single Table per Class Hierarchy Strategy
  • All the classes in a hierarchy map to a single table in the relational database 
  • Column for each unique persistence field in the hierarchy. 
  • Additional discriminator column

  • Benefits
Provides good support for polymorphic relationships between entities and for queries that range over the class hierarchy. 
  • Drawbacks 
It requires that the columns that correspond to state specific to the sub classes be nullable.


Mapping Inheritance Using a Table per Class Strategy
  • Requires a table for each concrete class in the hierarchy. 
  • Each table will have a column for each persistence field (including inherited fields) in the entity it maps to. 

  • Drawback
 It provides poor support for polymorphic relationships. 

Note: Java Persistence API does not require application servers to support the table per class mapping strategy.


Mapping Inheritance Using the Joined Subclass Strategy

  • Requires a common table to represent the root entity class in the hierarchy. 
  • Each entity subclass is represented by a separate table that contains columns specific to the sub class as well as the columns that represent its primary key. 
  • The primary key column(s)of the sub class table serves as a foreign key to the primary key of the superclass table.

  • Benefit 
Provides support for polymorphic relationships between entities. 
  • Drawbacks
 It requires that one or more join operations be performed to instantiate instances of a subclass.


Inheriting From an Entity Class

1.Declare the root entity class of the inheritance hierarchy. The root class can be an                abstract entity class. 
2.Choose the object / relational strategy you plan to use for the inheritance hierarchy you        are about to create. 
3.Use the strategy attribute of the Inheritance annotation to specify your selected object          relational mapping strategy on the root entity class. 
4.Declare the remaining entity classes of the hierarchy


Inheriting From an Entity Class

Root entity class example:


Child entity class example 1:


Child entity class example 2:



Inheriting Using a Mapped Superclass
  • What is a mapped superclass? 
  • What is the purpose of inheriting from a mapped superclass class? 
  • What is inherited? 
  • What features are common between a mapped superclass and an entity? 
  • How does a mapped superclass differ from an entity?
Mapped Superclass example:


Example of Entity Extending Mapped Superclass:


Second example of entity extending mapped superclass:



Inheriting From a Non-Entity Class
  • What is the purpose of inheriting from a non entity class? To inherit behavior and not state of parent. 
  • What is persistent and what is not persistent? Only state declared in the entity class is persistent. 
  • What annotations are processed? Only annotations declared on the entity class are processed. 
  • What other restrictions apply to non entity superclasses? Cannot be passed as arguments to entity manager or query interface.
Example of non entity superclass:


Example of entity inheriting from non entity superclass:



Entity Classes :Using an Embeddable Class
  • What is an embeddable class? A fine grained class that encapsulates common state used by many domain classes. Example :An address class 
  • Why use an embeddable class? To implement a composition relationship. 
  • How is persistence identity affected? Embeddable class instances do not have persistent identity.
  • What are the requirements for an embeddable class? Same as entity classes but substitute the Embeddable annotation for the Entity annotation 
  • How many levels of embedding is permitted? One

Example of an embeddable class


Example of using an embeded class




Entity Classes:Using a Composite Primary Key

  • What is composite primary key? A composite primary key(as opposed to a simple primary key)uses the value of multiple fields or properties as a composite to uniquely identify an entity. 
  • What options are available to define a composite primary key? 
You have two options:
  • Use the EmbeddedId annotation 
  • Use the IdClass annotation

Defining a Composite Primary Key Using the Embedded Id Annotation

1. Define an embeddable class that contains only the fields or properties that make up the         composite primary key. 

1        @Embeddable 
2           public class EmployeePK{ 
3            public String name; 
4            public Date birthDate; 
5          }

2. Embed the primary key class using EmbeddedId

1        @Entity 
2          public class Employee{ 
3             @EmbeddedId 
4             protected EmployeePK empPK; 
5             //... 
6          }


Deļ¬ning a Composite Primary Key Using the IdClass Annotation

1. Define a(primary key)class that contains only the fields or properties that make up the           composite primary key. 
2. Annotate the entity class using the IdClass annotation. 
3. Declare in the entity class a duplicate of every field or property declared in the primary           key class. 
4. Annotate every field or property of the primary key class declared in the entity class with       the Id annotation.

Example of a composite primary key class

1          public class EmployeePK implements Serializable { 
2            String name; 
3            Date birthDate; 
4          }

Example of using an embeddable class as a composite key

1         @IdClass(EmployeePK.class). 
2         @Entity 
3         public class Employee{ 
4                 @Id String name; 
5                 @Id Date birthDate; 
6                 String department 
7                 //... 
8         }



No comments:

Post a Comment