Your SlideShare is downloading. ×
0
Introduction to JPA (JPA version 2.0)
Introduction to JPA (JPA version 2.0)
Introduction to JPA (JPA version 2.0)
Introduction to JPA (JPA version 2.0)
Introduction to JPA (JPA version 2.0)
Introduction to JPA (JPA version 2.0)
Introduction to JPA (JPA version 2.0)
Introduction to JPA (JPA version 2.0)
Introduction to JPA (JPA version 2.0)
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Introduction to JPA (JPA version 2.0)

3,387

Published on

Introduction to JPA (JPA version 2.0)

Introduction to JPA (JPA version 2.0)

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,387
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
81
Comments
0
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

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>

×