SlideShare a Scribd company logo
JPA 2.0
in JAVA
EE 6
Basics
Mapping
Query
Transaction and Concurrency
JPA 2.0 in JAVA EE 6
Basics
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 6
Basics
Why JPA?
JPA 2.0 in JAVA EE 6
Basics
Why JPA?
JPA 2.0 in JAVA EE 6
Basics
Java Persistence Features
 Simplify the persistence programming
 Default over configuration (Convention over configuration )
 Elimination of deployment descriptor
 Provide light-weight persistence model
 In 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 6
Basics
Java Persistence Features
 Support rich domain modelling
 Support inheritance and polymorphism among entities
 Provide standardized and efficient ORM
 Optimized for relational database
 Standardize 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 6
Basics
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 6
Basics
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 6
Basics
Simple Mapping
Mapping 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 6
Basics
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 6
Basics
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 6
Basics
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 6
Basics
@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 6
BasicsO/R Mapping Annotations
JPA 2.0 in JAVA EE 6
Basics
@Column – changes
attibutes of the column such
as the name, length and
whether allows NULL or NOT.
JPA 2.0 in JAVA EE 6
Basics
JPA 2.0 in JAVA EE 6
Basics
@Basic – overrides basic
persistence
 fetch.LAZY
 fetch.EAGER
JPA 2.0 in JAVA EE 6
Basics
@Temporal – provides representation of
date, hour or milliseconds
JPA 2.0 in JAVA EE 6
Basics
JPA 2.0 in JAVA EE 6
Basics
@Enumerated – maps the enum values as Text( ORDINAL
default)
JPA 2.0 in JAVA EE 6
Basics
@Transient – disables the mapping of a given attribute
JPA 2.0 in JAVA EE 6
Basics
@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 6
Basics
JPA 2.0 in JAVA EE 6
Basics DEMO
JPA 2.0 in JAVA EE 6
Basics
Concepts of JPA Operations
 Entity Manger
 Persistent context
 Persistent unit
JPA 2.0 in JAVA EE 6
Basics
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….
larslemos@gmail.com
Lars Lemos
toplars
JPA 2.0 in
JAVA EE 6Mapping
Mapping in JPA 2.0 in JAVA EE 6
Mapping
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 6
Mapping
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 6
Mapping
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 6
Mapping
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 6
Mapping
Uni-directional Mappings
 @OneToOne : Employee – ParkingSpace
Mapping in JPA 2.0 in JAVA EE 6
Mapping
Uni-directional Mappings
 @ManyToOne : Employee – Department
Mapping in JPA 2.0 in JAVA EE 6
Mapping
Bi-directional Mappings
 @OneToOne : Employee – ParkingSpace
 The owner is the side with he foreign key
Mapping in JPA 2.0 in JAVA EE 6
Mapping
Bi-directional Mappings
 @OneToMany: Employee – Department
 The owner is always the “Many” side
Mapping in JPA 2.0 in JAVA EE 6
Mapping
Bi-directional Mappings
 @ManyToMany: Employee – Project
Mapping in JPA 2.0 in JAVA EE 6
Mapping
Embedded Objects
 @ManyToMany: Employee – Project
JPA 2.0 in
JAVA EE 6Query
Mapping in JPA 2.0 in JAVA EE 6
Query
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 6
Query
 JPQL
Mapping in JPA 2.0 in JAVA EE 6
Query
 Named query
 Queries tha can be reusable.
Mapping in JPA 2.0 in JAVA EE 6
Query
 Named query
Mapping in JPA 2.0 in JAVA EE 6
Query
 Dynamic Query
 Build and execute queries dynamically at runtime.
Mapping in JPA 2.0 in JAVA EE 6
Query
 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….
larslemos@gmail.com
Lars Lemos
toplars

More Related Content

What's hot (19)

PPT
plsql les06
sasa_eldoby
 
PPTX
Java Beans
Ankit Desai
 
PPTX
Oracle Sql Developer Data Modeler 3 3 new features
Philip Stoyanov
 
PPTX
Database Programming Techniques
Raji Ghawi
 
PPT
plsql les02
sasa_eldoby
 
PPTX
Introduction to JPA (JPA version 2.0)
ejlp12
 
DOC
3963066 pl-sql-notes-only
Ashwin Kumar
 
PPT
06 Using More Package Concepts
rehaniltifat
 
PDF
Pl sql
chaitanyanaredla
 
DOC
DATA BASE MANAGEMENT SYSTEM - SHORT NOTES
suthi
 
PPTX
Spring (1)
Aneega
 
PDF
Lesson03 学会使用单行函数
renguzi
 
PPT
SQL- Introduction to PL/SQL
Vibrant Technologies & Computers
 
PPT
ORACLE PL SQL
Srinath Maharana
 
PPT
plsql les03
sasa_eldoby
 
PDF
IBM Solutions '99 XML and Java: Lessons Learned
Ted Leung
 
PDF
Arrays and lists in sql server 2008
nxthuong
 
PPT
11 Understanding and Influencing the PL/SQL Compilar
rehaniltifat
 
plsql les06
sasa_eldoby
 
Java Beans
Ankit Desai
 
Oracle Sql Developer Data Modeler 3 3 new features
Philip Stoyanov
 
Database Programming Techniques
Raji Ghawi
 
plsql les02
sasa_eldoby
 
Introduction to JPA (JPA version 2.0)
ejlp12
 
3963066 pl-sql-notes-only
Ashwin Kumar
 
06 Using More Package Concepts
rehaniltifat
 
DATA BASE MANAGEMENT SYSTEM - SHORT NOTES
suthi
 
Spring (1)
Aneega
 
Lesson03 学会使用单行函数
renguzi
 
SQL- Introduction to PL/SQL
Vibrant Technologies & Computers
 
ORACLE PL SQL
Srinath Maharana
 
plsql les03
sasa_eldoby
 
IBM Solutions '99 XML and Java: Lessons Learned
Ted Leung
 
Arrays and lists in sql server 2008
nxthuong
 
11 Understanding and Influencing the PL/SQL Compilar
rehaniltifat
 

Similar to Jpa 2.0 in java ee 6 part ii (20)

PDF
JUG Berlin Brandenburg: What's new in Java EE 7?
gedoplan
 
PDF
Spring Boot Tutorial Part 2 (JPA&Hibernate) .pdf
abdelr7man3mad2004
 
PPT
Slice: OpenJPA for Distributed Persistence
Pinaki Poddar
 
PPT
Jpa
Manav Prasad
 
PPT
Jpa basics
Balaji Vericharla
 
PPT
Al rihieli persistence
Jhonny Villarroel
 
PPT
YDP_API&MS_UNIT_IIIii8iiiiiiiii8iiii.ppt
NikhilBoda
 
PPT
YDP_API&MS_UNIT_hiii detail notes to understand api.ppt
NikhilBoda
 
PPT
Java New Evolution
Allan Huang
 
PPTX
Accelerating Development.pptx
Uraz Pokharel
 
PPT
Persisting Your Objects In The Database World @ AlphaCSP Professional OSS Con...
Baruch Sadogursky
 
PDF
Java EE 6 : Paving The Path For The Future
IndicThreads
 
PPT
Entity Persistence with JPA
Subin Sugunan
 
PPT
Have You Seen Java EE Lately?
Reza Rahman
 
PDF
5050 dev nation
Arun Gupta
 
PDF
Java SE 8
Simon Ritter
 
PPT
OSGi Persistence With EclipseLink
Shaun Smith
 
PDF
50 New Features of Java EE 7 in 50 minutes @ Devoxx France 2014
Arun Gupta
 
PPTX
Protein and RNA alignment and analysis with Jalview 2.8.2 and JABA 2.1
Jim Procter
 
PPTX
Hibernate in Nutshell
Onkar Deshpande
 
JUG Berlin Brandenburg: What's new in Java EE 7?
gedoplan
 
Spring Boot Tutorial Part 2 (JPA&Hibernate) .pdf
abdelr7man3mad2004
 
Slice: OpenJPA for Distributed Persistence
Pinaki Poddar
 
Jpa basics
Balaji Vericharla
 
Al rihieli persistence
Jhonny Villarroel
 
YDP_API&MS_UNIT_IIIii8iiiiiiiii8iiii.ppt
NikhilBoda
 
YDP_API&MS_UNIT_hiii detail notes to understand api.ppt
NikhilBoda
 
Java New Evolution
Allan Huang
 
Accelerating Development.pptx
Uraz Pokharel
 
Persisting Your Objects In The Database World @ AlphaCSP Professional OSS Con...
Baruch Sadogursky
 
Java EE 6 : Paving The Path For The Future
IndicThreads
 
Entity Persistence with JPA
Subin Sugunan
 
Have You Seen Java EE Lately?
Reza Rahman
 
5050 dev nation
Arun Gupta
 
Java SE 8
Simon Ritter
 
OSGi Persistence With EclipseLink
Shaun Smith
 
50 New Features of Java EE 7 in 50 minutes @ Devoxx France 2014
Arun Gupta
 
Protein and RNA alignment and analysis with Jalview 2.8.2 and JABA 2.1
Jim Procter
 
Hibernate in Nutshell
Onkar Deshpande
 
Ad

Recently uploaded (20)

PDF
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PDF
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
PDF
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
PDF
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
PDF
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PPTX
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
PPTX
Tally software_Introduction_Presentation
AditiBansal54083
 
PDF
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
PDF
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
PDF
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PDF
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
PPTX
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
PDF
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
Tally software_Introduction_Presentation
AditiBansal54083
 
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Ad

Jpa 2.0 in java ee 6 part ii

  • 1. JPA 2.0 in JAVA EE 6 Basics Mapping Query Transaction and Concurrency
  • 2. JPA 2.0 in JAVA EE 6 Basics 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
  • 3. JPA 2.0 in JAVA EE 6 Basics Why JPA?
  • 4. JPA 2.0 in JAVA EE 6 Basics Why JPA?
  • 5. JPA 2.0 in JAVA EE 6 Basics Java Persistence Features  Simplify the persistence programming  Default over configuration (Convention over configuration )  Elimination of deployment descriptor  Provide light-weight persistence model  In 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
  • 6. JPA 2.0 in JAVA EE 6 Basics Java Persistence Features  Support rich domain modelling  Support inheritance and polymorphism among entities  Provide standardized and efficient ORM  Optimized for relational database  Standardize 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
  • 7. JPA 2.0 in JAVA EE 6 Basics JPA Architecture Java Application Java Persistence API Hibernate EclipseLink Kodo (OpenJPA) Everyone can use their own favorite persistence technology
  • 8. JPA 2.0 in JAVA EE 6 Basics 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
  • 9. JPA 2.0 in JAVA EE 6 Basics Simple Mapping Mapping 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
  • 10. JPA 2.0 in JAVA EE 6 Basics 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
  • 11. JPA 2.0 in JAVA EE 6 Basics 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)
  • 12. JPA 2.0 in JAVA EE 6 Basics 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
  • 13. JPA 2.0 in JAVA EE 6 Basics @Table – change the default name of the table
  • 14. JPA 2.0 in JAVA EE 6 Basics @SecondaryTables – associates a secondary table
  • 15. JPA 2.0 in JAVA EE 6 BasicsO/R Mapping Annotations
  • 16. JPA 2.0 in JAVA EE 6 Basics @Column – changes attibutes of the column such as the name, length and whether allows NULL or NOT.
  • 17. JPA 2.0 in JAVA EE 6 Basics
  • 18. JPA 2.0 in JAVA EE 6 Basics @Basic – overrides basic persistence  fetch.LAZY  fetch.EAGER
  • 19. JPA 2.0 in JAVA EE 6 Basics @Temporal – provides representation of date, hour or milliseconds
  • 20. JPA 2.0 in JAVA EE 6 Basics
  • 21. JPA 2.0 in JAVA EE 6 Basics @Enumerated – maps the enum values as Text( ORDINAL default)
  • 22. JPA 2.0 in JAVA EE 6 Basics @Transient – disables the mapping of a given attribute
  • 23. JPA 2.0 in JAVA EE 6 Basics @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.
  • 24. JPA 2.0 in JAVA EE 6 Basics
  • 25. JPA 2.0 in JAVA EE 6 Basics DEMO
  • 26. JPA 2.0 in JAVA EE 6 Basics Concepts of JPA Operations  Entity Manger  Persistent context  Persistent unit
  • 27. JPA 2.0 in JAVA EE 6 Basics Life Cycle of an Entity
  • 28. JPA 2.0 in JAVA EE 6 Basics DEMO
  • 29. 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
  • 30. JPA 2.0 in JAVA EE 6Mapping
  • 31. Mapping in JPA 2.0 in JAVA EE 6 Mapping 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
  • 32. Mapping in JPA 2.0 in JAVA EE 6 Mapping 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.
  • 33. Mapping in JPA 2.0 in JAVA EE 6 Mapping 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
  • 34. Mapping in JPA 2.0 in JAVA EE 6 Mapping 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
  • 35. Mapping in JPA 2.0 in JAVA EE 6 Mapping Uni-directional Mappings  @OneToOne : Employee – ParkingSpace
  • 36. Mapping in JPA 2.0 in JAVA EE 6 Mapping Uni-directional Mappings  @ManyToOne : Employee – Department
  • 37. Mapping in JPA 2.0 in JAVA EE 6 Mapping Bi-directional Mappings  @OneToOne : Employee – ParkingSpace  The owner is the side with he foreign key
  • 38. Mapping in JPA 2.0 in JAVA EE 6 Mapping Bi-directional Mappings  @OneToMany: Employee – Department  The owner is always the “Many” side
  • 39. Mapping in JPA 2.0 in JAVA EE 6 Mapping Bi-directional Mappings  @ManyToMany: Employee – Project
  • 40. Mapping in JPA 2.0 in JAVA EE 6 Mapping Embedded Objects  @ManyToMany: Employee – Project
  • 41. JPA 2.0 in JAVA EE 6Query
  • 42. Mapping in JPA 2.0 in JAVA EE 6 Query 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
  • 43. Mapping in JPA 2.0 in JAVA EE 6 Query  JPQL
  • 44. Mapping in JPA 2.0 in JAVA EE 6 Query  Named query  Queries tha can be reusable.
  • 45. Mapping in JPA 2.0 in JAVA EE 6 Query  Named query
  • 46. Mapping in JPA 2.0 in JAVA EE 6 Query  Dynamic Query  Build and execute queries dynamically at runtime.
  • 47. Mapping in JPA 2.0 in JAVA EE 6 Query  Native Query  Build and execute queries based on native SQL.
  • 48. JPA 2.0 in JAVA EE 6 Mapping & Querying DEMO
  • 49. 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