Java Persistence API – Getting Started


The Java persistence API (JPA) allows you to store data from POJO’s (Plain old Java Objects) into database tables.

The persistence metadata is specified using Java annotations, XML mapping files or a combination of both. In case both are used then the XML information overwrites the annotations in the Java code. This persistence metadata defines the mapping to the database and the relationship between the objects.

JPA allows us to work with entity classes, which are denoted as such using the annotation @Entity or configured in an XML file (we’ll call this persistence meta information). When we acquire the Entity Manager Factory using the Persistence class, the Entity Manager Factory finds and processes the persistence meta information.

To work with a database using JPA, we need an Entity Manager. Before we can do that, we need to create an Entity Manager Factory.

  • EntityManagerFactory.jpeg

  • PersistenceSequence.jpg

To acquire an Entity Manager Factory, we use the class javax.persistence.Persistence. It reads a file called persistence.xml in the META-INF directory. It then creates the named Entity Manager Factory, which processes persistence meta information stored in XML files or annotations (we only use annotations).

Creating an Entity Manager once we have the Entity Manager Factory is simple:

  • CreateEntityManager.jpeg

Once we have an Entity Manager, we can ask it to perform several operations such as persisting or removing an entity from the database or creating a query.

Term Description
javax.persistence.Persistence This is a class used as an entry point for using JPA. The primary method you’ll use on this class is createEntityManagerFactory(“someName”) to retrieve an entity manager factory with the name “someName”. This class requires a file called persistence.xml to be in the class path under a directory called META-INF.
EntityManagerFactory An instance of this class provides a way to create entity managers. Entity Managers are not multi-thread safe so we need a way to create one per thread. This class provides that functionality. The Entity Manager Factory is the in-memory representation of a Persistence Unit.
EntityManager An Entity Manager is the interface in your underlying storage mechanism. It provides methods for persisting, merging, removing, retrieving and querying objects. It is not multi-thread safe so we need one per thread. The Entity Manager also serves as a first level cache. It maintains changes and then attempts to optimize changes to the database by batching them up when the transaction completes.
persistence.xml A required file that describes one or more persistence units. When you use the javax.persistence.Persistence class to look up an named Entity Manager Factory, the Persistence class looks for this file under the META-INF directory.
Persistence Unit A Persistence Unit has a name and it describes database connection information either directly (if working in a JSE environment) or indirectly by referencing a JNDI-defined data source (if working in a managed/JEE environment). A Persistence Unit can also specify the classes(entities) it should or should not manage .
Persistence Meta Information Information describing the configuration of entities and the database and the association between entity classes and the persistence units to which they relate. This is either through annotations added to classes or though XML files. Note that XML files take precedence over annotations.

JPA requires that you identify the classes that you will store in a database. JPA uses the term Entity to define classes that it will map to a relational database.

JPA is a specification and for using JPA you always need a JPA provider.

The JPA provider will use the persistence metadata information to perform the correct database operations.

Most JPA persistence provider offer the option to automatically create the database schema based on the metadata.

I will be soon compiling more information on JPA here.

Meanwhile you can access these links to get started.

http://blog.springsource.com/2006/05/30/getting-started-with-jpa-in-spring-20/

http://java.sun.com/javaee/overview/faq/persistence.jsp

http://schuchert.wikispaces.com/JPA+Tutorial+1+-+Getting+Started

http://www.vogella.de/articles/JavaPersistenceAPI/article.html#jpaintro

http://www.javabeat.net/articles/5-introduction-to-java-persistence-apijpa-1.html

only working example codes http://www.java2s.com/Code/Java/JPA/CatalogJPA.htm

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s