jpa 2.0 in java ee 6 part ii
TRANSCRIPT
JPA 2.0 in JAVA
EE 6
Basics
Mapping
Query
Transaction and Concurrency
JPA 2.0 in JAVA EE 6Basics
What is JPA? Java EE Standard ORM Framework JPA 2.0 (JSR 317)
JPA 1.0 part of EJB 3.0 JSR 220
JPA 2.0 standalone JSR 317
JPA 2.1 (JSR 338) included in Java EE 7
Enables transparent POJO Persistence
Handles Object Relational impedance mismatch
Enables building persistent objects following common
OOP concepts
JPA 2.0 in JAVA EE 6Basics
Why JPA?
JPA 2.0 in JAVA EE 6Basics
Why JPA?
JPA 2.0 in JAVA EE 6Basics
Java Persistence Features
Simplify the persistence programming Default over configuration (Convention over configuration )
Elimination of deployment descriptor
Provide light-weight persistence modelIn both programming model and deployment
Runtime performance
Enable testability outside of the containers Enables test-driven development
Test entities as part of nightly-buid process
JPA 2.0 in JAVA EE 6Basics
Java Persistence Features
Support rich domain modelling Support inheritance and polymorphism among entities
Provide standardized and efficient ORMOptimized for relational databaseStandardize annotations and XML configuration files
Provide extensive querying capabiliies Support for pluggable, third-party persistence providers.
Through persistence unit – represented by persistence.xml Persistence API expanded to include use outside EJB
container Evolved into “common” Java persistence API between Java
SE and Java EE apps
JPA 2.0 in JAVA EE 6Basics
JPA Architecture
Java Application Java Persistence API
Hibernate EclipseLink Kodo (OpenJPA)
Everyone can use their own favorite persistence technology
JPA 2.0 in JAVA EE 6Basics
Entity
Plain Old Java Object (POJO) Supports OO programming model, (PIE)
Can be in either persistent(manage) or non-
persistent state (detached)Example of non-persistent state is “transient” state
Have persistent identity When it is in managed state
Can extend other entity and non-entity classes
(Inheritance)
Serializable; usable as detached objecs in other tiers
JPA 2.0 in JAVA EE 6Basics
Simple MappingMapping defaults to matching column name.
Default Mapping Entity name -> table name
(customizable via @Table) Attribute name -> column
name(customizable via @Column)
Data type mapping (some differences among databases) String -> VARCHAR(255) Long, long -> BIGINT Double, double -> DOUBLE Boolean -> SMALLINT
JPA 2.0 in JAVA EE 6Basics
Entity Class (@Entity)
Annotated with @Entity
Can extend another entity
Can be a concrete or abstract class
Programming requirement Must have a primary key field
Must have a public no-arg constructor
Instance variables must not be public
Must not be final or have final methods
JPA 2.0 in JAVA EE 6Basics
Entity Identity (@Id, @GeneratedValue ) Every entity has a persistence identity
Maps to primary key in database
Cancorrespond to simple type @Id – single field/property in entity class
@GeneratedValue – value can be generated automatically using
various strategies
AUTO – Choose type depending on database, e.g: IDENTITY for
MySQL
IDENTITY – using a database identity column
SEQUENCE – using a database sequence
TABLE – Use a sequence table for key generation (most portable)
JPA 2.0 in JAVA EE 6Basics
O/R Mapping Annotations
Set of annotations defined for mapping
(Relationships, Joins, Database tables and
columns)
An Example Data Model Maps entity state to data store
Maps relationships to other entities
JPA 2.0 in JAVA EE 6Basics
@Table – change the default
name of the table
JPA 2.0 in JAVA EE 6 Basics
@SecondaryTables –
associates a secondary table
JPA 2.0 in JAVA EE 6Basics
O/R Mapping Annotations
JPA 2.0 in JAVA EE 6Basics
@Column – changes
attibutes of the column such
as the name, length and
whether allows NULL or
NOT.
JPA 2.0 in JAVA EE 6Basics
JPA 2.0 in JAVA EE 6Basics
@Basic – overrides basic
persistence
fetch.LAZY
fetch.EAGER
JPA 2.0 in JAVA EE 6Basics
@Temporal – provides representation of
date, hour or milliseconds
JPA 2.0 in JAVA EE 6Basics
JPA 2.0 in JAVA EE 6Basics
@Enumerated – maps the enum values as Text( ORDINAL
default)
JPA 2.0 in JAVA EE 6Basics
@Transient – disables the mapping of a given attribute
JPA 2.0 in JAVA EE 6Basics
@CollectionTable – specifies the table
that is used for the mapping of
collection of basic or embeddable types.
@ElementCollection – inform the
persistence provider rerence is a set of
collection and defines the “fetch”
mode.
JPA 2.0 in JAVA EE 6Basics
JPA 2.0 in JAVA EE 6 Basics DEMO
JPA 2.0 in JAVA EE 6Basics
Concepts of JPA Operations Entity Manger
Persistent context
Persistent unit
JPA 2.0 in JAVA EE 6Basics
Life Cycle of an Entity
JPA 2.0 in JAVA EE 6
Basics
DEMO
JPA 2.0 in JAVA EE 6
End of
Part I
Basics
Part II – Mapping
Thank you for your patience….
[email protected] Lars Lemos
toplars
JPA 2.0 in JAVA
EE 6Mapping
Mapping in JPA 2.0 in JAVA EE 6Mapping
Types of Entity Relationship
Directionality Uni-directional
Bi-directional
Cardinality relationships One to one
One to many
Many to many
Inheritance relationship Single table
Joined Table
Mapping in JPA 2.0 in JAVA EE 6Mapping
Directionality & Ownership
Relationship has an ownership Who owns the relationship affects how tables are created.
Uni-directional Ownership is implied
Bi-directional Ownership needs to be explicitly specified.
Owner of the relationship, inverse-owner of the relationship.
Mapping in JPA 2.0 in JAVA EE 6Mapping
Directionality & Navigation
Directionality affects the navigation
Uni-directional customer.getAddress() is allowed but address.getCustomer() is not
supported for 1-1 relationship
customer.getOrders() is allowed but order.getCustomers() is not supported
for 1-m relationship.
speaker.getEvents() is allowed but event.getSpeakers() is not supported for
n-m relationship.
Bi-directional Navigation on both direction
Mapping in JPA 2.0 in JAVA EE 6Mapping
Cardinality Relationship Cardinality relationships
@OneToOne : Customer – Address
@OneToMany: Customer – Orders
@ManyToMany : Speakers – Events
@ManyToOne: Employee - Project
One-to-Many and Many-to-Many relationships are
represented in Java code through
Collection, Set, List and Map
Mapping in JPA 2.0 in JAVA EE 6Mapping
Uni-directional Mappings @OneToOne : Employee – ParkingSpace
Mapping in JPA 2.0 in JAVA EE 6Mapping
Uni-directional Mappings @ManyToOne : Employee – Department
Mapping in JPA 2.0 in JAVA EE 6Mapping
Bi-directional Mappings @OneToOne : Employee – ParkingSpace
The owner is the side with he foreign key
Mapping in JPA 2.0 in JAVA EE 6Mapping
Bi-directional Mappings @OneToMany: Employee – Department
The owner is always the “Many” side
Mapping in JPA 2.0 in JAVA EE 6Mapping
Bi-directional Mappings @ManyToMany: Employee – Project
Mapping in JPA 2.0 in JAVA EE 6Mapping
Embedded Objects @ManyToMany: Employee – Project
JPA 2.0 in JAVA
EE 6Query
Mapping in JPA 2.0 in JAVA EE 6Query
JPA Query Features Static queries
Defined with Java language metadata or XML
Anonotations: @NamedQuery, @NamedNativeQuery
Dynamic queries
Query string can be created at runtime.
Use Java Persistence Query Language (JPQL) or SQL
Named or positional parameters
EntityManager is factory for Query objects createNamedQuery, createQuery, createNativeQuery
Query methods for controlling max results, pagination,
flush mode
Mapping in JPA 2.0 in JAVA EE 6Query
JPQL
Mapping in JPA 2.0 in JAVA EE 6Query
Named query Queries tha can be reusable.
Mapping in JPA 2.0 in JAVA EE 6Query
Named query
Mapping in JPA 2.0 in JAVA EE 6Query
Dynamic Query Build and execute queries dynamically at runtime.
Mapping in JPA 2.0 in JAVA EE 6Query
Native Query Build and execute queries based on native SQL.
JPA 2.0 in JAVA EE 6
Mapping & Querying
DEMO
JPA 2.0 in JAVA EE 6
End of
Part III
Basics
Part IV – Advanced JPA & Transactions
Thank you for your patience….
[email protected] Lars Lemos
toplars