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          }


Defining 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         }



Business Component Development with EJB Technology - Module 05

Module 5
Implementing Entity Classes : Modelling Data Association Relationships

Objectives

Upon completion of this module, you should be able to:

  • Examine association relationships in the data and object models 
  • Use relationship properties to define associations 
  • Implement one-to-one unidirectional associations 
  • Implement one-to-one bidirectional associations 
  • Implement many-to-one/one-to-many bidirectional associations 
  • Implement many-to-many bidirectional associations 
  • Implement many-to-many unidirectional associations 
  • Examine fetch and cascade mode settings

Relationships Within the Auction System’s Data Model

 

Object Model Showing Object Association Relationships 




Using Relationship Properties to Define Associations
  • Cardinality: 
              •OneToOne 
              •ManyToOne 
              •OneToMany 
              •ManyToMany 
  • Direction: 
              •Bidirectional 
              •Unidirectional
  • Ownership:
              •Owning side 
              •Inverse side 
  • Cascade type (operation propagation): 
              •All 
              •Persist 
              •Merge 
              •Remove 
              •Refresh 
              •None


Cardinality - Direction Combinations



Examples of One-to-One Relationships

One-to-One Bidirectional Relationships


One-to-One Unidirectional Relationships



Examples of One-to-Many Relationships

One-to-Many Bidirectional Relationship


One-to-Many Unidirectional Relationships



Examples of Many-to-One Relationships

Many-to-One Unidirectional Relationships



Examples of Many-to-Many Relationships

Many-to-Many bidirectional Relationships



Many-to-Many Unidirectional Relationships



Implementing One-to-One Unidirectional Association 








Implementing One-to-One Bidirectional Association






Implementing One-to-Many / Many-to-One Bidirectional Association






Implementing Many-to-Many Bidirectional Association







Fetch Mode Attribute

@ManyToOne(fetch=EAGER) 
private Customer customer; 
  • EAGER 
  • LAZY
Fetch Mode Default Values 


Cascade Mode Attribute

@OneToOne(cascade=PERSIST) 
private Customer customer;

Cascade mode attribute values
  • PERSIST 
  • MERGE 
  • REMOVE 
  • REFRESH 
  • ALL

Business Component Development with EJB Technology - Module 04

Module 4

 Implementing Entity Classes: The Basics


Objectives

Upon completion of this module, you should be able to:

  • Examine Java persistence 
  • Define entity classes: Essential tasks 
  • Manage entity instance life-cycle states 
  • Deploy entity classes



Examining Java Persistence
  • What is data persistence? 
  • What is the Java persistence specification? 
  • How does the Java Persistence API relate to Java EE application servers? 
  • What are the key features of the persistence model specified in the Java Persistence API?


Static Relationship Mapping - Data Tier Elements




Static Relationship Mapping - Object Tier Elements




Dynamic Relationship - Object / Data Tier Data Synchronization


With entities, the data synchronization is maintained by the persistence provider.


EntityClassExample



Declaring the Entity Class

1. Collect information required to declare the entity class. 
2. Declare a public Java technology class. 
3. Implement Serializable if required. 
4. Do not include a finalize method. 
5. Use Entity annotation. 
6. Declare attributes. 
7. Optionally declare get and set methods. 
8. Designate primary key using the Id annotation. 
9. Declare no- arg constructor.


Entity Code Example


Corresponding CUSTOMER table:



Verifying and Overriding Default Mapping
  • Table annotation 
@Entity 
@Table(name=”Cust”)//Cust is the name of the database table 
public class Client { 
  • Column annotation 
@Entity 
@Table(name=”Cust”) public class Client { 
@Column(name=”cname”) 
private String clientName; 
  • Lob annotation 
26     @Lob 
27     @Column(name="PHOTO" columnDefinition="BLOB NOT NULL") 
28     protected JPEGImage picture;


Examining Managing Entity Instance Life Cycle States
  • Entity life-cycle states 
                          New 
                          Managed 
                          Detached 
                          Removed 
  • Entity manager 
  • Persistence contexts 
  • Persistence identity

Entity Instance State Diagram




Changing Entity Life - Cycle States



Other Entity Manager Methods




Persistence Contexts
  • What causes a persistence context to end? 
                   •Transaction scoped 
                   •Extended scoped 
  • How is a persistence context created? 
  • How is an extended scoped entity managerc reated in a Java EE container? 
  • How is a transaction scoped entity manager created in a Java EE container? 
  • What tasks are relevant to managing the life - cycle state of an entity instance?


Entity Instance Management Using Transaction Scoped Persistence Context



Entity Instance Management Using Extended Scoped Persistence Context



Deploying Entity Classes
  • What is a persistence unit?
  • Where should components of the persistence unit be placed?

Default Locations for Persistence Unit Components



Example of Persistence Unit Components in Default Settings