Java Persistence Programming with Entities - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Java Persistence Programming with Entities

Description:

Accessing Entities in the Persistence Context. persistence.xml ... Can handcraft, or use tools like Hibernate, TopLink, etc. ORM Example. What is an Entity? ... – PowerPoint PPT presentation

Number of Views:186
Avg rating:3.0/5.0
Slides: 22
Provided by: pinn2
Category:

less

Transcript and Presenter's Notes

Title: Java Persistence Programming with Entities


1
Java Persistence Programming with Entities
2
What Well Cover
  • Overview
  • ORM
  • ORM Example
  • What is an Entity?
  • Entities vs Session Beans
  • Entity classes
  • Accessing Entities in the Persistence Context
  • persistence.xml file
  • EntityManager API
  • Entity Life Cycle
  • Entity Life Cycle Diagram
  • Life-cycle call backs
  • Entity Synchronization
  • Transaction Isolation
  • Tuning for concurrent access
  • Dirty Read
  • Unrepeatable Read
  • Entity Lookup and Query API

3
Overview
  • Java Persistence provides
  • Standard OR mappings
  • Not tied to the J2EE container
  • Defines an SPI so different service providers can
    be used

4
ORM
  • Simplest Java Serialization objects written
    to files
  • Store Java Objects in RDBMS (JDBC maps your
    object data to DB)
  • ORM- Object Relational Mapping
  • Can have arbitrary queries for information
  • Can visually inspect data
  • Can handcraft, or use tools like Hibernate,
    TopLink, etc.

5
ORM Example
6
What is an Entity?
  • Two different kinds of objects application
    logic and persistent data
  • Persistent data can be saved to storage by
    persistence mechanisms
  • Called entities in Java Persistence Specification
  • Store data as fields and have getters/setters
  • Can group related data into coherent objects and
    factor out common attributes in inheritance
    hierarchy

7
Entities vs Session Beans
  • Entities have a client-visible persistent
    identity
  • Entities have a visible state
  • Entities are not remotely acessible
  • Entities lifetimes are independent of
    applications lifetime
  • Entities are translated back and forth by the
    persistence provider

8
Entity classes
  • Entity is a POJO (can implement Serializable so
    it can be used as a data record)
  • Entity maps to a definition in an RDBMS
  • Must have a primary key
  • Access via direct field state or setter/getter
  • Entity can expose business methods

9
Accessing Entities in the Persistence Context
  • Clients must retrieve entity from the persistence
    context or create one
  • Persistence context connection between in
    memory instances and the database EntityManager
    API
  • Persist() method puts an entity into the managed
    state
  • Transaction scoped context Persistence Context
    ends with enclosing txn
  • Extended persistence context ends when
    enclosing stateful bean is removed by the
    container (managed across several invocations
  • Detached state entities have no connection with
    a entity manager and are not synchronized with
    the DB
  • Detached entities have to be retrieved by id
    (reattached or merged into new context).

10
persistence.xml file
  • Must have persistence-unit element with name
    attribute
  • Example
  • ltpersistencegt
  • ltpersistence-unit name"manager1"gt
  • ltdescriptiongtps unit onelt/descriptiongt
  • ltjta-data-sourcegtjava/DefaultDSlt/jta-data-sourcegt
  • ltmapping-filegtormap2.xmllt/mapping-filegt
  • ltexclude-unlisted-classes/gt
  • ltjar-filegt../MyApp.jarlt/jar-filegt
  • ltclassgtorg.acme.Employeelt/classgt
  • ltpropertiesgt
  • ltproperty name"hibernate.dialect"
    value"org.hibernate.dialect.HSQLDialect"/gt
  • lt/propertiesgt
  • lt/persistence-unitgt
  • lt/persistencegt

11
EntityManager API
  • Container-managed injected via
    _at_PersistenceContext
  • Application-managed created via
    EntityManagerFactory
  • Three types of operations
  • Life-cycle management
  • DB synchronization
  • Entity lookup/queries

12
Entity Life Cycle
  • new entity instance created in memory, not
    associated with persistence identity or context
    (right after creation)
  • managed has a persistent identity, changes are
    synched w/ DB (after persist())
  • detached does have a persistent id but not
    associated with a persistence context
  • removed associated with a context, but is
    scheduled for removal from the DB (after remove()
    illegal on a detached entity)

13
(No Transcript)
14
Life-cycle call backs
  • _at_PrePersist
  • _at_PostPersist
  • _at_PreRemove
  • _at_PostRemove
  • _at_PreUpdate
  • _at_PostUpdate
  • _at_PostLoad
  • Can also define separate listener classes
  • _at_EntityListeners(MyListener.class)
  • Callback methods for listeners takes an entity
    argument

15
Entity Synchronization
  • SetFlushMode
  • COMMIT default synchronize entities in
    context at commit
  • AUTO at commit and before query execution
  • Flush() synchs the state of all entities in the
    context, but does not refresh from DB

16
Transaction Isolation
  • Protected by transaction isolation
  • Container-managed txns use isolation levels
    configured by persistence provider or containers
    txn service
  • SERIALIZABLE max txn isolation, may degrade
    performance
  • READ UNCOMMITTED may lead to inconsistent
    data/bad behavior

17
Tuning for concurrent access
  • Optimistic locking using _at_Version
  • Not done by default
  • Checks for version of entity before updates
  • Globally set stricter txn isolation
  • Application level locks
  • manager.lock(account LockMode.WRITE)
  • READ prevents dirty/non-repeatable reads
  • WRITE READ plus forces an increase in the
    version number (if any)

18
Dirty Read
19
Unrepeatable Read
20
Entity Lookup and Query API
  • Find by primary key with the EntityManager.find()
  • Create a query
  • Get a javax.persistence.Query from the EM
  • Customize (if needed) query params/result set
    limits
  • Execute
  • EM.createQuery EJB QL or createNativeQuery
    standard SQL
  • Named query
  • _at_NamedQuery (namename queryStringqueryString

21
What We Covered
  • Overview
  • ORM
  • ORM Example
  • What is an Entity?
  • Entities vs Session Beans
  • Entity classes
  • Accessing Entities in the Persistence Context
  • persistence.xml file
  • EntityManager API
  • Entity Life Cycle
  • Entity Life Cycle Diagram
  • Life-cycle call backs
  • Entity Synchronization
  • Transaction Isolation
  • Tuning for concurrent access
  • Dirty Read
  • Unrepeatable Read
  • Entity Lookup and Query API
Write a Comment
User Comments (0)
About PowerShow.com