9
JPA version 2.0 Tutorial [email protected] Dec 2011

Introduction to JPA (JPA version 2.0)

  • Upload
    ejlp12

  • View
    3.389

  • Download
    2

Embed Size (px)

DESCRIPTION

Introduction to JPA (JPA version 2.0)

Citation preview

Page 1: Introduction to JPA (JPA version 2.0)

JPA version 2.0 [email protected]

Dec 2011

Page 2: Introduction to JPA (JPA version 2.0)

The Java Persistence API provides an object/relational mapping facility to 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 database

Java Persistence consists of three areas:• The Java Persistence API• Object/relational mapping metadata (using annotations or in XML

deployment descriptor)• The query language

Java Persistence API (JPA)

Page 3: Introduction to JPA (JPA version 2.0)

An entity is a lightweight persistence domain object. Typically, represents a table in a relational database An 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)

Entity

Page 4: Introduction to JPA (JPA version 2.0)

Code Example: Entity

Page 5: Introduction to JPA (JPA version 2.0)

Persistence 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

Persistence Context & Entity Manager

Page 6: Introduction to JPA (JPA version 2.0)

Container-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();

Entity Manager

Page 7: Introduction to JPA (JPA version 2.0)

Code Example: Insert data (persist object)

Page 8: Introduction to JPA (JPA version 2.0)

persistance.xml Defines the database and entity manager options for deploying JPA

applications Packaged in the META-INF directory of the project

orm.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

XML Configuration files

Page 9: Introduction to JPA (JPA version 2.0)

<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>

persistence.xml in Java SE