Java Persistence API – Getting Started
- 21
- January
- 2009
- By admin
- Java, Uncategorized
- Comments Off on 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.
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:
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
Related
Recent Posts
- 12.1.0.2 RAC Installation – Step by Step installation on VMware
- OSB FTP Poller continuously throwing exception – while no file pending on FTP Location
- Using JMS Transport with OSB
- Installing Spring Tool Suite and configuring Spring for a development PC
- Rest Enabling SOA using OSB in Oracle 12c
Recent Comments
- Nits on Installing Oracle SOA Suite 12c
- Rajesh Krishna on Installing Oracle SOA Suite 12c
- Nits on Installing Oracle SOA Suite 12c
- Rohinii on Installing Oracle SOA Suite 12c
- Mahitha on Oracle Service Bus (OSB) Development Best Practices.
Archives
- December 2015
- May 2015
- November 2014
- September 2014
- August 2014
- July 2014
- April 2014
- March 2014
- February 2014
- January 2014
- December 2013
- November 2013
- October 2013
- September 2013
- July 2013
- June 2013
- May 2013
- March 2013
- February 2013
- November 2012
- October 2012
- September 2012
- May 2012
- March 2012
- February 2012
- December 2011
- November 2011
- October 2011
- September 2011
- August 2011
- June 2011
- May 2011
- January 2011
- December 2010
- November 2010
- October 2010
- September 2010
- July 2010
- June 2010
- March 2010
- February 2010
- December 2009
- September 2009
- August 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- November 2008
- October 2008
- August 2008
- June 2008
- May 2008
Categories
- AIA
- AXIS2
- Best Practices
- BPEL
- BPM
- Budget
- Build
- Design Patterns
- ESB
- Hermes
- Hibernate
- Home Owners
- Java
- JMS
- JSR 168
- Maven
- Mediator
- Messaging
- Nitin
- OER
- OESB
- Oracle
- OSB
- OWSM
- Portlets
- SDLC
- Sequencing
- Singleton
- SOA
- SOAP
- Spring
- Struts
- UK
- Uncategorized
- Web Services
- WLST
Meta
To find out more, including how to control cookies, see here: Cookie Policy
Recent Posts
- 12.1.0.2 RAC Installation – Step by Step installation on VMware
- OSB FTP Poller continuously throwing exception – while no file pending on FTP Location
- Using JMS Transport with OSB
- Installing Spring Tool Suite and configuring Spring for a development PC
- Rest Enabling SOA using OSB in Oracle 12c
Recent Comments
- Nits on Installing Oracle SOA Suite 12c
- Rajesh Krishna on Installing Oracle SOA Suite 12c
- Nits on Installing Oracle SOA Suite 12c
- Rohinii on Installing Oracle SOA Suite 12c
- Mahitha on Oracle Service Bus (OSB) Development Best Practices.
Archives
- December 2015
- May 2015
- November 2014
- September 2014
- August 2014
- July 2014
- April 2014
- March 2014
- February 2014
- January 2014
- December 2013
- November 2013
- October 2013
- September 2013
- July 2013
- June 2013
- May 2013
- March 2013
- February 2013
- November 2012
- October 2012
- September 2012
- May 2012
- March 2012
- February 2012
- December 2011
- November 2011
- October 2011
- September 2011
- August 2011
- June 2011
- May 2011
- January 2011
- December 2010
- November 2010
- October 2010
- September 2010
- July 2010
- June 2010
- March 2010
- February 2010
- December 2009
- September 2009
- August 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- November 2008
- October 2008
- August 2008
- June 2008
- May 2008
Categories
- AIA
- AXIS2
- Best Practices
- BPEL
- BPM
- Budget
- Build
- Design Patterns
- ESB
- Hermes
- Hibernate
- Home Owners
- Java
- JMS
- JSR 168
- Maven
- Mediator
- Messaging
- Nitin
- OER
- OESB
- Oracle
- OSB
- OWSM
- Portlets
- SDLC
- Sequencing
- Singleton
- SOA
- SOAP
- Spring
- Struts
- UK
- Uncategorized
- Web Services
- WLST