Introduction to JPA (JPA version 2.0)
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Introduction to JPA (JPA version 2.0)

on

  • 4,001 views

Introduction to JPA (JPA version 2.0)

Introduction to JPA (JPA version 2.0)

Statistics

Views

Total Views
4,001
Views on SlideShare
3,165
Embed Views
836

Actions

Likes
1
Downloads
70
Comments
0

3 Embeds 836

http://mohdshamsudinabdhamid.blogspot.com 834
http://mohdshamsudinabdhamid.blogspot.com.ar 1
http://mohdshamsudinabdhamid.blogspot.in 1

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…
Post Comment
Edit your comment

Introduction to JPA (JPA version 2.0) Presentation Transcript

  • 1. JPA version 2.0 Tutorial ejlp12@gmail.comDec 2011
  • 2. Java Persistence API (JPA)The Java Persistence API provides an object/relational mapping facilityto Java developers for managing relational data in Java applications. Provided by the javax.persistence package Part of EJB 3.0 specification (JSR 220) Enables persisting POJO to relational databaseJava Persistence consists of three areas:• The Java Persistence API• Object/relational mapping metadata (using annotations or in XML deployment descriptor)• The query language
  • 3. EntityAn entity is a lightweight persistence domain object.Typically, represents a table in a relational databaseAn entity class must follow these requirements: annotated with the javax.persistence.Entity annotation. have a public or protected, no-argument constructor. not be declared final, including methods or persistent instance variables If an entity instance be passed by value as a detached object, such as through a session bean’s remote business interface, the class must implement the Serializable interface. may extend both entity and non-entity classes. persistent instance variables must be declared private, protected, or package-private, and can only be accessed directly by the entity class’s methods (get/set)
  • 4. Code Example: Entity
  • 5. Persistence Context & Entity ManagerPersistence context a set of managed entity instances that exist in a particular data store.The EntityManager interface defines the methods that are used to interact with the persistence context creates and removes persistent entity instances finds entities by the entity’s primary key allows queries to be run on entities
  • 6. Entity ManagerContainer-Managed Entity Managers an EntityManager instance’s persistence context is automatically propagated by the container to all application components instance within a single Java Transaction Architecture (JTA) transaction. Usually used in Java EE environment @PersistenceContext EntityManager em;Application-Managed Entity Managers the persistence context is not propagated to application components, and the life cycle of EntityManager instances is managed by the application. Usually used in Java SE environment @PersistenceUnit EntityManagerFactory emf; EntityManager em = emf.createEntityManager();
  • 7. Code Example: Insert data (persist object)
  • 8. XML Configuration filespersistance.xml Defines the database and entity manager options for deploying JPA applications Packaged in the META-INF directory of the projectorm.xml Stores metadata to describe object-relational mappings Not really required – JPA spec emphasizes using annotation Mapping on this file overrides any mapping defined using annotations
  • 9. persistence.xml in Java SE<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0"> <persistence-unit name="pu1"> <provider>oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider</provider> <!-- All persistence classes must be listed --> <class>entity.Customer</class> <class>entity.Order</class> <class>entity.Item</class> <properties> <!-- Provider-specific connection properties --> <property name="toplink.jdbc.driver" value="<database driver>"/> <property name="toplink.jdbc.url" value="<database url>"/> <property name="toplink.jdbc.user" value="<user>"/> <property name="toplink.jdbc.password" value="<password>"/> <!-- Provider-specific settings --> <property name="toplink.logging.level" value="INFO"/> </properties> </persistence-unit></persistence>