java persistence api 2.1
TRANSCRIPT
![Page 1: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/1.jpg)
Java Persistence API
![Page 2: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/2.jpg)
Agenda- Overview- OR Mapping and Entity Introduction- Entity Manager- Transactions- Persistence Context- Relationships- Query Capabilities- Criteria
![Page 3: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/3.jpg)
So, what's the big deal ?- Relational DB and Java are world apart- JDBC
- Mandates manual marshaling- SQL Queries are difficult to manage
- No compile time support- Boiler plate- Database portability
- EJB 2.1- Beast- Horrible to work with
- Need of the hour : Lightweight POJO framework
![Page 4: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/4.jpg)
What we need to save the day ?- Higher level abstraction above JDBC- Provide object view of RDBMS resources- Simple to configure- Lightweight- Unobtrusive- Local, but Mobile- Integration and Testability- Above all, Fun to work with
![Page 5: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/5.jpg)
JPA- All of the items listed in prev slide and ...- Feature rich - Spec is about 500+ pages- More than one way to do, that way looks complex- Can be used in JavaEE as well as JavaSE environments- Wait, we do have alternatives
- Data Mapper- Java Data Objects- JDBC
![Page 6: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/6.jpg)
History- JPA 1.0 (2006)
- Part of EJB 3.0 Spec- Start over (Leaving Legacy Entity framework)- Leading ORM experts on the panel (Hibernate, TopLink, etc)
- JPA 2.0 (2009)- Added significant amount of features to catch up with ORM vendors- Criteria API- Bean Validation- Metamodel
- JPA 2.1 (2013)- Converters- Criteria Update/Delete- Stored Procedures
![Page 7: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/7.jpg)
Big Players- Client- JPA Spec API- JPA implementation
- Hibernate- EclipseLink (prev TopLink)- Apache OpenJPA
- Database Driver- DB
- App Level : <persistence-unit>- DB location : JNDI or DB URL- List of Entity classes- JPA provider : Hibernate
- Each Entity Class- Table name- Column names
- Client that uses JPA API
Code / Configuration
![Page 8: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/8.jpg)
Lab - 1 - Module Setup- Step 1 : Create a git repo- Step 2 : Create a maven module (all defaults)- Step 3 : Git commit- Step 4 : Add Dependencies
- Client (JUnit)- JPA Spec API- Hibernate Core- Entity Manager- Database driver jar
- Step 5 : Add JPA configuration file : persistence.xml- Step 6 : Assert em.isOpen()- Step 7 : Git commit
![Page 9: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/9.jpg)
OR Mapping- Class <-> Table
- Property <-> Column- Properties without mapping to any column : Transient
- Instance of Class <-> Row in the table- Relies on Convention over Configuration- Same old POJO underneath- Entities are still EJBs ? Yes and No !
- Typically created with new operator- Like SBs to container, Entities to EntityManager- Resides on server but client directly interacts with them unlike EJB siblings
- Can define DB schema (not for production use)
![Page 10: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/10.jpg)
Lab - 2 : Find Employee- Step 1 : Employees Entity- Step 2 : persistence.xml using MySQL Employees DB- Step 3 : Assert employee presence- Step 5 : Getters and Setters- Step 6 : Updated test run
![Page 11: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/11.jpg)
Object-Relational Mapping- POJO needs something to become Entity- 2 Ways : Annotations or XML- Two types of Annotations
- Logical - Describes object model- @Entity, @Basic, @Temporal etc.
- Physical - Describes physical schema- @Table, @Column etc.
- Configuration by Exception- Optional (Almost)- Can be specified on Fields or Bean Properties- Field annotations : Getters/Setters ignored- Bare minimum : @Entity and @Id annotations
![Page 12: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/12.jpg)
Mapping simple types- Java Primitive types and Wrappers- Byte[], Byte[], char[], Character[]- java.lang.String- java.math.BigInteger, BigDecimal- java.util.Date, Calendar- java.sql.Date, Time, Timestamp- Enums
![Page 13: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/13.jpg)
Entity, Who are you ?- Entity must have an identity- Annotate the field with @Id- Equivalent of primary key
- Primary key not mandatory in DB table- Multiple rows with same value in @Id mapped column. Trouble!
- Composite primary key- @IdClass- @EmbeddedId- @MapsId
![Page 14: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/14.jpg)
ID Generation- 4 Types
- AUTO- For prototyping only
- IDENTITY- SEQUENCE- TABLE
- DB agnostic
- IDENTITY and SEQUENCE need DB support
![Page 15: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/15.jpg)
Serializable Entities- Not mandated by Spec- Application can opt for when
- Entities are shared across VMs- Entities are stored in (clustered/serialized) sessions
![Page 16: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/16.jpg)
Entity LIfecycle
Source
![Page 17: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/17.jpg)
JPA Key ElementsPersistence
Source
EntityManagerFactory
Persistence Context
![Page 18: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/18.jpg)
Persistence Unit (Tangible)- Named configuration of entity classes
![Page 19: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/19.jpg)
EntityManager (Tangible)- Central authority- Qualifies simple POJOs into Entities- Almost entire API is built in this one interface- Provides API to
- Do CRUD- Query- Synchronize with Database
- Automatically sync to DB- 1st Level Caching- Works with JTA in server env
![Page 20: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/20.jpg)
EntityManager (Tangible)- Obtained from EntityManagerFactory- Not thread-safe and must not be shared between threads- Java EE
- Can be obtained directly using CDI injection or JNDI lookup- Typically a transaction involves multiple components- Need same entity context all through the transaction- Multiple entity managers (within a tx) automatically points to same Persistence Context- We don't have to pass reference of EntityManager between calls
- Java SE- Using standalone bootstrap class Persistence- Each call to emf.createEntityManager() creates a new isolated persistence context
![Page 21: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/21.jpg)
Persistence Context (Intangible)- Invisible- A set of managed Entity objects- Associated to a Persistence Unit- Managed by one or more EntityManagers- Only one Entity with same identity- Entities contained within are “managed”
![Page 22: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/22.jpg)
Source
![Page 23: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/23.jpg)
Transaction- ACID- Transaction Demarcation : Act of beginning or completing- JPA supports two types
- Resource Local- JavaSE and JavaEE (without container support) applications- Always application demarcated
- JTA- aka Global Transactions- Spans across resources (multiple DBs, Messaging server, etc…)- Demarcated by containers (most of the times)
![Page 24: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/24.jpg)
Transaction Contd...
- Transaction Synchronization : Process by which a persistence context is registered with a transaction
- Transaction Association : Act of binding a persistence context to a transaction- Transaction Propagation : Process of sharing a persistence context between
multiple entity managers within a transaction
![Page 25: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/25.jpg)
Entity Manager Types- EntityManager can be
- Container Managed Transaction-scoped- Container Managed Extended- Application scoped
- Scope is defined when the EntityManager is created
![Page 26: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/26.jpg)
Container Managed Entity Manager- Mostly we are talking about Java EE environment- Injected via @PersistenceContext- No need to Open or Close it- Two flavors
- Transaction Scoped- Extended
![Page 27: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/27.jpg)
Transaction Scoped- Default- Lives as long as Transaction is alive- Once transaction finished, all objects are detached- Lazily created : When an operation on EntityManager is called- @PersistenceContext hands over a proxy- Stateless- Maintenance free
![Page 28: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/28.jpg)
Transaction Scoped Contd...
- Gets associated to the ongoing transaction seamlessly
![Page 29: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/29.jpg)
Transaction Scoped Contd...
![Page 30: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/30.jpg)
Extended Context- Meant for @Stateful beans only- Begins with EM creation and live till its closed- Eagerly created : As soon as the txn begins- The context gets destroyed when @Remove on SFSB is called- Extended spans transactions and manages Entities between Txns- Caching of Entity is not the objective but mostly you will be doing it
![Page 31: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/31.jpg)
Extended Context- Prevents Entities from becoming detached when txn ends- Client can use bean fields to hold state and interact with DB
![Page 32: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/32.jpg)
Extended Contd...
![Page 33: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/33.jpg)
Application managed Context- Mostly Java SE environments- Can be used in Java EE Environment as well- Application is responsible for context’s life cycle- Extended by nature and spans multiple transactions- Needs Explicit
- Creation : EntityManagerFactory.createEntityManager()- Flushing/Closing : EntityManager.close()
- Is standalone and not propagated with transaction- Can join transaction by EntityManager.joinTransaction()- Typically used in Unit Testing
![Page 34: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/34.jpg)
Recommendations- Stateless Beans
- Preferred way to manage persistence operations- Use @PersistantContext to inject EntityManager
- Stateful Beans- Extended context- State can include EntityContext and its result data
- Use JNDI lookup to get Stateful beans (no @Inject/@EJB)- In case of rollback, DO NOT revive the detached object
![Page 35: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/35.jpg)
Caching- Concept is to avoid going to DB by maintaining an in-sync object in memory- Entity Manager serves as 1st level cache- Infinispan is default for WildFly- Caching hurdles
- In memory objects are not transactional- Data sync with Database- Memory occupancy
![Page 36: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/36.jpg)
Relationships in DB- Achieved via
- Foreign Key- Mapping tables
- Table with Foreign Key column is the owner
![Page 37: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/37.jpg)
A bit insights into DB
![Page 38: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/38.jpg)
Relationships- No Entity is an Island!- Table with foreign key always ends up with single association- Roles
- Employee “works in” a Department- Department “has” employees working in it- One Entity can participate in many relationships
- Directionality- Unidirectional- BiDirectional
-
![Page 39: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/39.jpg)
Relationships Cntd...
- Cardinality- One to One- One to Many- Many to One- Many to Many
- Ordinality (aka Optionality)
![Page 40: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/40.jpg)
Relationships Myths- Way to avoid business logic
- You still have to deal with CascadeType
- Gives flexibility- Too little for too much price- Lazy Vs Eager loading- OpenEntityManagerInViewFilter
- Shields developer from database- To very little extent- Often the cause for killer bugs/defects
-
![Page 41: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/41.jpg)
Single Valued Associations- Cardinality of Target entity is 1
- ManyToOne- OneToOne
- ManyToOne - Rules- @JoinColumn goes in Entity that has foreign key defined- @ManyToOne goes into the owning side i.e with foreign key- @OneToMany(mappedBy) goes into the target Entity in case of Bidirectional
- OneToOne - Two Rules- @JoinColumn goes in Entity that has foreign key defined- @OneToOne(mappedBy) goes into the target Entity in case of Bidirectional
-
![Page 42: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/42.jpg)
Many To One- Always Source has has attribute of Target type- Enables navigation from Source to Target- @ManyToOne and @JoinColumn- Table with foreign key is owning side- Ownership is important because of foreign key column- @JoinColumn is specified on the owning side- Many to One mappings are always on owning side
![Page 43: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/43.jpg)
Many To One- @JoinColumn is optional- Default join column : targetTable_targetIdColumn- @Column won’t gel with these annotations
![Page 44: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/44.jpg)
One To One- Almost similar to Many To One- @OneToOne and @JoinColumn- Table with foreign key is the owning side- @JoinColumn has to be on the side with foreign key
![Page 45: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/45.jpg)
One To One Bidirectional- Source Entity already has all the information- It just needs which property on source entity has it- @OneToOne(mappedBy)
![Page 46: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/46.jpg)
Collection Valued Associations- Source entity references one or more Target entities
- OneToMany- ManyToMany
![Page 47: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/47.jpg)
One To Many- Nothing but Bidirectional ManyToOne- Always implies a ManyToOne presence- Always Bidirectional- Always on the target side i.e. Not the Source side- All configuration is in Source entity already- Just needs the Source entity property information- @OneToMany(mappedBy)
![Page 48: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/48.jpg)
Many To Many- @JoinColumn is not sufficient- Can be achieved only by a join table- Difficult to say who is the owner- Either can be the owner- The target side will have @ManyToMany(mappedBy)- Absence of mappedBy will make them unidirectional
![Page 49: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/49.jpg)
Many To Many Bidirectional- mappedBy on the target side- All it needs is the property configured in Source Entity
![Page 50: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/50.jpg)
One To Many using a @TableJoin- @OneToMany is not just bidirectional @ManyToOne- Can be achieved with a @TableJoin
![Page 51: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/51.jpg)
Lazy Relationships- Not specified, @ManyToOne and @OneToOne guaranteed to Eagerly load- Collection valued relationships by default is Lazily load- Provider may choose to do Eager loading
![Page 52: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/52.jpg)
Queries- CRUD - Retrieval Rules- As expected, constructed using EntityManager- API
- Query- TypedQuery
- Type- Static - Annotation or XML- Dynamic
- JPQL- Criteria
- Dynamic queries are expensive
![Page 53: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/53.jpg)
JPQL- Limited yet powerful- Metamodel is not mandatory (almost)- Limited compile time safety- Two types
- @NamedQuery- Dynamic string query
![Page 54: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/54.jpg)
Criteria API- Programmatic control- Robust- Uses Objects instead of Strings- Can result in bloated code- More than One way to do it- Query is prepared dynamically- Allows flexible filtering
![Page 55: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/55.jpg)
Criteria Query - Entity Result
![Page 56: Java persistence api 2.1](https://reader034.vdocument.in/reader034/viewer/2022052312/587991eb1a28ab95318b5c35/html5/thumbnails/56.jpg)
References- JPA 2.1 Specification (Not for humans)- JPA 2.1 Java Doc (Amazingly small)- (Blog) JPA 2.1 – 12 features every developer should know- MySQL Employees Sample DB