Java Persistence API Martin Kraj Systinet / HP CZJUG - - PowerPoint PPT Presentation

java persistence api
SMART_READER_LITE
LIVE PREVIEW

Java Persistence API Martin Kraj Systinet / HP CZJUG - - PowerPoint PPT Presentation

Java Persistence API Martin Kraj Systinet / HP CZJUG http://java.cz/jug Content JPA introduction Entities Relationships, Inheritance, JPQL Callbacks, Listeners, Transactions EntityManager, PeristenceContext Hibernate


slide-1
SLIDE 1

CZJUG http://java.cz/jug

Java Persistence API

Martin Krajčí Systinet / HP

slide-2
SLIDE 2

CZJUG http://java.cz/jug

Content

  • JPA introduction
  • Entities
  • Relationships, Inheritance, JPQL
  • Callbacks, Listeners, Transactions
  • EntityManager, PeristenceContext
  • Hibernate vs. Toplink, IDE support
  • Demo application
slide-3
SLIDE 3

CZJUG http://java.cz/jug

Java persistence comparision

  • September 2006 results
slide-4
SLIDE 4

CZJUG http://java.cz/jug

Primary features

  • Simple to use and intuitive to learn

– Configure by exception (smart defaults)

  • POJO development model
  • Object-oriented, inheritance, polymorphism, etc.
  • Standardized metadata for O/R mapping (annotations,

XML)

  • Entity detachment to other tier or JVMs
  • Java persistence query language
  • Java SE and EE persitence model
  • Support pluggable persistence providers
slide-5
SLIDE 5

CZJUG http://java.cz/jug

JPA vendors

  • Toplink Essential (Oracle; CDDL)
  • Hibernate (JBoss; LGPL)
  • OpenJPA (Solarmetric/BEA; Apache 2.0)
  • Castor (Codehaus; Apache 2.0)
  • JPOX (JPOX; Apache 2.0)
  • Toplink (Oracle; commercial)
  • Kodo (BEA; commercial)
  • CocoBase (Thoughtinc; commercial)
  • SAP JPA (SAP; commercial)
slide-6
SLIDE 6

CZJUG http://java.cz/jug

Content

  • JPA introduction
  • Entities
  • Relationships, Inheritance, JPQL
  • Callbacks, Listeners, Transactions
  • EntityManager, PeristenceContext
  • Hibernate vs. Toplink, IDE support
  • Demo application
slide-7
SLIDE 7

CZJUG http://java.cz/jug

Entities

  • Serializable POJO annotated with

@Entity

  • Persistent state

– Defined by persistent fields or properties (Hibernate

extension)

  • @AccessType.FIELD, @AccessType.PROPERTY

– Entities may also have transient state

  • @Transient, transient

– Optimistic locking - @Version – Can be a concrete or abstract class

slide-8
SLIDE 8

CZJUG http://java.cz/jug

Entity identification

  • Persistent identity must be defined

– @Id, @EmbeddedId, @IdClass

  • Primary key generation -

@GenerationType

– TABLE, SEQUENCE, IDENTITY, AUTO

slide-9
SLIDE 9

CZJUG http://java.cz/jug

Cascade & Fetch type

  • CascadeType

– ALL, PERSIST, MERGE, REMOVE, REFRESH

  • FetchType

– LAZY, EAGER

slide-10
SLIDE 10

CZJUG http://java.cz/jug

Entity lifecycle

slide-11
SLIDE 11

CZJUG http://java.cz/jug

Update and Detach

  • Updates are flushed

– Before query is executed – Transaction commit – Explicit flush using EM.flush()

  • Detach

– Persistent context ends – EM.clear() is called – deserialized

slide-12
SLIDE 12

CZJUG http://java.cz/jug

Content

  • JPA introduction
  • Entities
  • Relationships, Inheritance, JPQL
  • Callbacks, Listeners, Transactions
  • EntityManager, PeristenceContext
  • Hibernate vs. Toplink, IDE support
  • Demo application
slide-13
SLIDE 13

CZJUG http://java.cz/jug

Relationships

  • Supports composition and

aggregation relationship

– @Embedable, @ManyToOne, ...

  • Relationships (uni/bi directional)

– One-to-one, one-to-many, many-to-one, many-to-

many

  • Bidirectional relationships have
  • wning and inverse side
  • @JoinColumn for FK definition
slide-14
SLIDE 14

CZJUG http://java.cz/jug

Inheritance

  • Mixed inheritance hierarchies, entities

and non-entities, either concrete or abstract

  • @MappedSuperclass

– Doesn't map to any table, can not query

  • @Inheritance

– SINGLE_TABLE, JOINED, TABLE_PER_CLASS

slide-15
SLIDE 15

CZJUG http://java.cz/jug

Inheritance

slide-16
SLIDE 16

CZJUG http://java.cz/jug

JPQL

  • JPA supports static (named), dynamic

and native queries

– @NamedQuery, precompiled, createNamedQuery() – createQuery() – @SQLResultSetMapping, createNativeQuery()

  • Projection into non-existing classes
  • Aggregate functions, Subquery, Inner

and outer joins

  • Paging, Max result returned, Bind

parameter, Flush mode

slide-17
SLIDE 17

CZJUG http://java.cz/jug

Content

  • JPA introduction
  • Entities
  • Relationships, Inheritance, JPQL
  • Callbacks, Listeners, Transactions
  • EntityManager, PeristenceContext
  • Hibernate vs. Toplink, IDE support
  • Demo application
slide-18
SLIDE 18

CZJUG http://java.cz/jug

Callback & Listeners

  • Attached to entity by @EntityListener

– @PrePersist - when application calls persist() – @PostPersist – after the SQL insert – @PreRemove – when application calls remove() – @PostRemove – after the SQL delete – @PreUpdate – when a container detects that the

instance is dirty

– @PostUpdate – after the SQL update – @PostLoad – after the instance was loaded

  • Listener methods are defined on non-

entity classes

slide-19
SLIDE 19

CZJUG http://java.cz/jug

Transactions

  • In SE transactions and bootstraping

must be handled by the application code

– EntityTransaction

  • begin(), commit(), setRollbackOnly(), rollback(), isActive()
  • EE transactions are always JTA
  • @javax.ejb.TransactionAttribute

– REQUIRED, REQUIRES_NEW, SUPPORTS

NOT_SUPPORTED, NEVER, MANDATORY

slide-20
SLIDE 20

CZJUG http://java.cz/jug

Content

  • JPA introduction
  • Entities
  • Relationships, Inheritance, JPQL
  • Callbacks, Listeners, Transactions
  • EntityManager, PeristenceContext
  • Hibernate vs. Toplink, IDE support
  • Demo application
slide-21
SLIDE 21

CZJUG http://java.cz/jug

Packaging

  • persistence.xml
  • Defines

– Name of persistence unit – Transaction strategy for EM – Etities – Persistence provider – O/R mapping file (overrides annotations)

  • Persistent Unit

– Set of entities that are mapped to the single DB and

their mapping metadata

slide-22
SLIDE 22

CZJUG http://java.cz/jug

Entity Manager & Persistence context

  • Two types of EntityManagers

– Container managed in EE

  • @PersistenceContext, @PersistenceUnit

– Application managed in SE/EE

  • EntityManager, EntityManagerFactory
  • Two types of Persistence context

– Transaction-scoped

  • PersistenceContextType.TRANSACTION

– Extended (new to java ORM)

  • PersistenceContextType.EXTENDED
slide-23
SLIDE 23

CZJUG http://java.cz/jug

Content

  • JPA introduction
  • Entities
  • Relationships, Inheritance, JPQL
  • Callbacks, Listeners, Transactions
  • EntityManager, PeristenceContext
  • Hibernate vs. Toplink, IDE support
  • Demo application
slide-24
SLIDE 24

CZJUG http://java.cz/jug

Hibernate vs. Toplink Essential

  • 13 files, 4.2 MB
  • Cleaner DDL

generation

  • 2 files, 2.5 MB
  • Better documentation
  • Checks integrity
  • Finer logging support
  • Showsql more

verbose

slide-25
SLIDE 25

CZJUG http://java.cz/jug

JPA IDE support

  • Eclipse Dali plugin
  • Netbeans 5.5
slide-26
SLIDE 26

CZJUG http://java.cz/jug

Content

  • JPA introduction
  • Entities
  • Relationships, Inheritance, JPQL
  • Callbacks, Listeners, Transactions
  • EntityManager, PeristenceContext
  • Hibernate vs. Toplink, IDE support
  • Demo application
slide-27
SLIDE 27

CZJUG http://java.cz/jug

Demo application - UML

slide-28
SLIDE 28

CZJUG http://java.cz/jug

Demo application - Layers

slide-29
SLIDE 29

CZJUG http://java.cz/jug

JPA usage

  • JPA + Spring using JpaDaoSupport
  • JPA + Spring in SE
  • JPA in SE
  • JPA in EE
  • Run on Toplink Essential, Hibernate,

JBoss

slide-30
SLIDE 30

CZJUG http://java.cz/jug

References

Nice Documentations

  • http://www.oracle.com/technology/products/ias/toplink/jpa/resources-index.h
  • http://incubator.apache.org/openjpa/docs/openjpa-0.9.0-incubating/manual/

JPA Extensions

  • http://www.oracle.com/technology/products/ias/toplink/jpa/resources/toplink
  • http://www.hibernate.org/hib_docs/annotations/reference/en/html_single/#e

JPA IDE comparision

  • http://blogs.sun.com/klingo/entry/jpa_netbeans_5_5_vs

Java persistence comparision

  • http://www.java.net/pub/pq/122
  • http://www.jpox.org/
  • http://jpa.hibernate.org/
slide-31
SLIDE 31

CZJUG http://java.cz/jug

Questions & Answers

  • Now

OR

  • martin.krajci@gmail.com

Thanx for attention