Mastering JPA
Objective of this documentation.
- Organize my notes on JPA
- Try to compile a comprehensive material on the topic by combining existing documents and my notes
Hopefully, it will provides:
- Overview of various topics in JPA programming
- Provide pointers for each topics
- Filling-in the missing pieces
What is ORM
ORM stands for object-relational-mapping. It is a technology emerged to overcome the object-relational impendance mismatch problem. See The Object-Relation Impedance Mismatch for good starting point on this topic.
What is JPA
Specification of a standard ORM framework. From the perspective of application programmers, it consists of the following elements:
- Annotation for defining ORM model
- Conceptual definitions (Persistence Unit, Persistence Context)
API for manipulate with persistable objects (EntityManager and Query)
- Query language for performing queries. (EJB-QL)
Getting Started
To get a taste of what to be provided by JPA, try reading the tutorial briefly.
http://www.oracle.com/technology/pub/articles/gupta-jpa.html
http://www.javaworld.com/javaworld/jw-01-2007/jw-01-aop.html
In order to develop application with JPA, you will have the following steps which should be answered first
- Determine kind of configuration
- Setup the JPA environment properly
- Define some unit-of-work and the object model (aka domain model)
- Code the classes with annotations, and appropriate finder methods
- Write the persistence.xml
Configuration alternatives
Major choices:
- Container Managed / Application Managed
- JTA bound EM, Resource Local EM
- Extended PC propagation
Defining the mapping
When defining the mapping, generally you need to consider the association between entities.
- Identify classes (key, and supplementary)
Multipicity (OneToOne, OneToMany, ManyToOne, ManyToMany)
- Multi-value semantics (Collection / Set / List)
- Association (Navigation: Uni-direction / Bi-direction)
- Owning Side and inverse side
- Eager pre-fetch and Lazy load
Transaction and Concurrency Issue
- Use of Optimistic lock (@Version)
- JTA and JPA
Performance Issue
- Use 'lazy' by default
Other Issue
- Schema Create and Update
- Data Migration
Reference and Resources
Introduction to ORM topics
JPA Tutorial
http://www.oracle.com/technology/pub/articles/gupta-jpa.html
http://www.javaworld.com/javaworld/jw-01-2007/jw-01-aop.html
Specification
Official Documentation for well-known JPA providers
Quick Reference