Entity Persistence with JPA
Upcoming SlideShare
Loading in...5
×
 

Entity Persistence with JPA

on

  • 3,327 views

An introduction to JPA framework

An introduction to JPA framework

Statistics

Views

Total Views
3,327
Views on SlideShare
3,249
Embed Views
78

Actions

Likes
5
Downloads
222
Comments
1

14 Embeds 78

http://subinsugunan.blogspot.com 42
http://subinsugunan.blogspot.in 12
http://www.subinsugunan.blogspot.com 5
http://subinsugunan.blogspot.co.uk 2
http://subinsugunan.blogspot.it 2
http://subinsugunan.blogspot.com.br 2
http://subinsugunan.blogspot.gr 2
http://subinsugunan.blogspot.de 2
http://subinsugunan.blogspot.fr 2
http://subinsugunan.blogspot.sk 2
http://subinsugunan.blogspot.nl 2
http://subinsugunan.blogspot.ru 1
http://subinsugunan.blogspot.mx 1
http://subinsugunan.blogspot.co.nz 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • Thank you ,.. very nicely crafted ( though short )
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • Why JPA?   * JPA is standardized specification and part of EJB3 specification * Many free ORM frameworks are available with can be used to develop applications of any size * Application developed in JPA is portable across many servers and persistence products (ORM frameworks). * Can be used with both JEE and JSE applications * JSE 5 features such as annotations can be used * Both annotations and xml based configuration support
  • Entity Entity is the persistence (POJO) objects that represent one record in the table. The Entity is simple annotated POJO class, which is easy to develop. Here are the characteristics of an Entity: Entity can be persisted to the relational database.   Entity is identified by persistence identity (the primary key of the table)
  • Persistence Context To be very precise, a persistent context manages a set of entities which in turn is managed by the EntityManager. A persistent context keeps track of the state (or the changes) that an entity object may undergo. And the EntityManager takes the support of this persistence context to commit or to undo the changes. As soon as an EntityManager object is created, it is implicitly associated with a persistence context formanaging a set of entities. Persistent Context comes in two flavors, one is the transaction-scoped persistent context and the other one is extended persistent context. Transaction-Scoped Persistence Context: Imagine that a set of entities are managed by the persistence context. At this time, we say that the entities are bound to (or attached) to the persistent context. Changes may happen to these entities and these changes will occur within a transaction. Sometimes later when the transaction ends (commits or roll-back), the entities will unbind (detached) from the persistent context. As soon as the entities are detached from the persistence context, they are no longer being managed. Any changes that happen to these entities will not be persisted as they don’t have any association with the persistence context. Such kind of persistent context whose life-time is dependent on the life-time of the transaction (for a set of entities at that particular point of time) is termed as transaction-scoped persistent context. All the transaction-scoped persistence context are configured by injecting @PersistentContext to EntityManager objects , like this,   @PersistenceContext(name=”PersistentUnitName”) private EntityManager entityManager; [Note: There is no such public interface called PersitenceContext in the Java Persistence API. The EntityManager may implicitly depend on a virtual Persistence Context class formanaging the entities. So, creating a transaction-scoped persistence context is nothing but creating an EntityManager object that has been configured with @PersistenceContext annotation.] Extended Persistence Context: Unlike transaction-scoped persistence context, where the life-time of the persistence context will end as soon the transaction is completed, a persistence context may be configured to live even after a transaction completes. In such a case, all the entities that are managed by such a persistence context will still be in a manageable state only even after the transaction ends. They won’t be detached from the context. Such long-lived persistence context that will continue to exist even after the completion of a transaction is called extended persistence context. All the extended persistence context objects are created and managed manually by the application code only (that is by the developers). [As soon as an EntityManager object is created, an implicit Persistence context will be associated with it and it is soon kept open and prepared formanaging a set of entities, meaning that the calling EntityManager.isOpen() method will always return true. Calling the EntityManager.clear() method will clear the current persistence context associated with the EntityManager and all the entities that have their associations will now be cleared and they become detached from the EntityManager. To release all the resources referenced by the EntityManager, call the close() method, After this method call, calling any of the methods will throw IllegalStateException.]
  • EntityManager The EntityManager interface is providing the API for interacting with the Entity. Some of the functions provided by EntityManager API are: persist – this method is used to save a new entity merge – this method is used to update the sate of entity into database remove – this method is used to remove the entity instance EntityManagerFactory The EntityManagerFactory is used to create an instance of EntityManager. In your application when there is no use of EntityManagerFactory or application shuts down then it is necessary to close the instance of EntityManagerFactory . Once the EntityManagerFactory is closed, all its EntityManagers are also closed.
  • The Query API http://www.javabeat.net/articles/5-introduction-to-java-persistence-apijpa-6.html
  • @Entity / @Table Table defaults to the class name if not annotated We choose to explicitly specify the table for each mapped class @Id All Entities require a unique identifier @Basic is the default @Column needed only if column name != field name @Transient So any field that is unannotated in your class will be persisted (or at least attempted, Remember @Basic) For fields that don’t need persistenting – they must be marked with @Transient @Enumerated To use enum as field. e.g. @Enumerated(EnumType.STRING) @Column(name = "PRSP_STATUS_EN") ProspectStatusEnum prospectStatusEnum; @Temporal The @Temporal is used to specify the type that a JPA extension must persist for entity fields of type java.util.Date and java.util.Calendar only e.g. @Temporal(TemporalType.DATE) @Column(name = "PRSP_STATUS_DT") private Date prospectStatusDate;
  • Entities and Transactions All entities have the property of transactionability and their CRUD operations will take place within a transactional context. Transactions can be broadly classified into two types based on who actually owns (or manages) the transaction. They are JTA and Resource-local transaction. In the case of a J2EE Application, the default transaction type is JTA (Java Transaction API), unless explicitly specified. A method can be simply annotated with @RequiresNew (or @Requires) in which case a new transaction will always started by the container and the transaction completes as soon as themethod ends. Whereas in a J2SE application, the transaction-type defaults to Resource-local, which means that the developer (or the application code) has to explicitly start and end a transaction. It means that the user has to explicitly start a transaction with the help of EntityManager object, and then have to commit it, if everything goes normal, else have to roll-back the transaction if some error occurs. With container-managed transactions, the transactional behavior for your EJBs is determined at deployment time. A transaction attribute determines the scope of a transaction when an EJB method is invoked. It specifies whether an EJB method executes within an existing transaction context. An EJB method can have one of the following transaction attributes: Never The method must never execute within an existing transaction context. If the client invokes such a method within a transaction context, the EJB container must throw a RemoteException (EJBException for local client). NotSupported The EJB container ensures that the method doesn't execute in a transaction context. If one exists, the container suspends the transaction for the duration of the EJB method, and resumes the transaction once the method exits. Supports The method executes with or without an existing transaction context. A new transaction isn't created before the method starts, but if one exists, it will be used, and it may influence the outcome of the transaction. Required The method requires a transaction context before it is invoked. If one doesn't exist, a new transaction is created before the method starts. Mandatory The method must never execute without a transaction context. If an existing transaction context doesn't exist, the EJB container must throw a TransactionRequiredException. RequiresNew A new transaction is created regardless of whether a client transaction context exists. The existing transaction is suspended, the new transaction is created for the duration of the method call, and the existing transaction is resumed once the method terminates.

Entity Persistence with JPA Entity Persistence with JPA Presentation Transcript

  • Entity Persistence with JPA [email_address]
  • What’s Inside
    • JPA
    • JPA Architecture
    • Entity
    • Object/Relational Mapping
    • Entity Relationship
    • Persistence Unit
    • Persistence Context
    • Entity Manager
    • Entity Manager API
    • Query API
    • Annotations
    • Transactions
    • Next Steps
  • JPA
    • What
      • JPA is just a specification from Sun, which is released under JEE 5 specification.
      • A feature-rich implementation of the persistence part of Enterprise Java Beans 3.0. JPA defines an interface to perform CRUD operations between POJO's and a data store.
    • Why
      • JPA is not a new technology; rather, it has collected the best ideas from existing persistence technologies like Hibernate, TopLink etc . The result is a standardized specification that helps you build a persistence layer that is independent of any particular persistence provider.
  • JPA Architecture
  • Entity
    • Lightweight persistent domain object – the thing you persist
    • Restrictions
      • must have a public or protected no-arg constructor
      • cannot be final
      • cannot have final methods or final instance variables that are to be persisted
      • can be abstract or concrete class
      • must have a primary key
      • Example (Entity)
  • Object/Relational Mapping
  • Entity Relationship
    • Defined by relationships in the database schema
      • ManyToOne
      • OneToOne
      • OneToMany
      • ManyToMany
  • Entity Relationship
  • Persistence Unit
    • The set of all classes mapped to a single database for the application
    • Defined in META-INF/persistence.xml
    • An application can have multiple persistence units
    <?xml version= &quot;1.0&quot; encoding=&quot;UTF-8&quot;?> <persistence version= &quot;1.0&quot; xmlns= &quot;http://java.sun.com/xml/ns/persistence&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation= &quot;http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd&quot;> <persistence-unit name= “subin-persistence-unit“ transaction-type=&quot; JTA &quot;> <jta-data-source> jndi/jdbc/traceds </jta-data-source> </persistence-unit> <persistence-unit name= “subin-persistence-unit-test“ transaction-type=&quot; RESOURCE_LOCAL &quot;> <provider>org.hibernate.ejb.HibernatePersistence</provider> <class> com.subin.entity.HistoryBean </class> <!-- <class>com.subin.entity.ServiceBean</class> <class>com.subin.entity.LookupBean</class> --> <properties> <property name= &quot;hibernate.dialect&quot; value=&quot;org.hibernate.dialect.MySQLDialect&quot; /> <property name= &quot;hibernate.connection.driver_class&quot; value=&quot;com.mysql.jdbc.Driver&quot; /> <property name= &quot;hibernate.show_sql&quot; value=&quot;true&quot; /> <property name= &quot;hibernate.format.sql&quot; value=&quot;true&quot; /> <property name= &quot;hibernate.connection.username&quot; value=&quot;root&quot; /> <property name= &quot;hibernate.connection.password&quot; value=&quot;root&quot; /> <property name= &quot;hibernate.connection.url&quot; value=&quot;jdbc:mysql://192.168.160.107/testdb&quot; /> </properties> </persistence-unit> </persistence>
  • Persistence Context
    • Abstraction representing a set of “managed” entity instances
      • Entities keyed by their persistent identity
      • Only one entity with a given persistent identity may exist in the PC
      • Entities are added to the PC, but are not individually removable (“detached”)
    • Controlled and managed by EntityManager
      • Contents of PC change as a result of operations on EntityManager API
    • “ local working copy” of persistent objects
  • Persistence Context Application Persistence Context Entities MyEntity A MyEntity B MyEntity C MyEntity a EntityManager MyEntity b Entity state
  • Entity Manager
    • API for interacting with the Entity
    • Can think of it as a proxy to a persistence context
      • May access multiple different persistence contexts throughout its lifetime
    • The EntityManagerFactory is used to create an instance of EntityManager.
    • Example (Persistence Context,EntityManagerFactory,EntityManager)
  • Entity Manager API
    • persist()- Insert the state of an entity into the db
    • remove()- Delete the entity state from the db
    • refresh()- Reload the entity state from the db
    • merge()- Synchronize the state of detached entity with the pc
    • find()- Execute a simple PK query
    • createQuery()- Create query instance using dynamic JP QL
    • createNamedQuery()- Create instance for a predefined query
    • createNativeQuery()- Create instance for an SQL query
    • contains()- Determine if entity is managed by pc
    • flush()- Force synchronization of pc to database
  • Query API
    • getResultList() – execute query returning multiple results
    • getSingleResult() – execute query returning single result
    • executeUpdate() – execute bulk update or delete
    • setFirstResult() – set the first result to retrieve
    • setMaxResults() – set the maximum number of results to retrieve
    • setParameter() – bind a value to a named or positional parameter
    • setHint() – apply a vendor-specific hint to the query
    • setFlushMode()– apply a flush mode to the query when it gets run
  • Annotations
    • Core
      • @Entity
      • @Table
      • @Id
      • @Basic
      • @Column
      • @Transient
      • @Enumerated
      • @Temporal
    • Relationships
      • @ManyToOne
      • @OneToOne
      • @OneToMany
      • @JoinColumn
  • Transactions
    • Two types
      • Default to JTA in a Java EE environment
      • Default to RESOURCE_LOCAL in a Java SE environment
    • @TransactionAttribute Annotation
    • TransactionAttributeType.REQUIRED
    • TransactionAttributeType.REQUIRES_NEW
    • TransactionAttributeType.MANDATORY
    • TransactionAttributeType.NOT_SUPPORTED
    • TransactionAttributeType.NEVER
    • TransactionAttributeType.SUPPORTS
    • How can we specify
  • Next Steps
    • Dynamic quries
    • Named quries
    • JPA Queries and JPQL (http://www.objectdb.com/java/jpa/query)
  • References Entity
    • http://www.javabeat.net/articles/5-introduction-to-java-persistence-apijpa-1.html
    • http://www.roseindia.net/jpa
    • http://www.cs.bilkent.edu.tr/~ccelik/cs412/jpatransaction.ppt
    • http://www.javamug.org/mainpages/presentations/JPAtheEnterpriseStandard.ppt
  • Thank You