IBM WebSphere Application Server Feature Pack for OSGi Applications and Java Persistence API 2.0Java Persistence API 2.0 overview
IBM WebSphere Application Server Feature Pack for OSGi Applications and Java Persistence API 2.0 Java Persistence API 2.0 overview
This presentation will introduce Java™ Persistence API in general and some of the JPA 2.0 features shipped in the feature pack.
Table of contents
Table of contents Brief introduction to Java Persistence API (JPA) JPA 2.0 Feature Pack overview OpenBooks sample program description
This presentation contains a brief introduction to the Java Persistence API, an overview of what is in the JPA feature of the IBM WebSphere® Application Server Feature Pack for OSGi Applications and Java Persistence API 2.0, and a description of a sample program available on the web that you can try.
What is JPA? (1 of 2)
What is JPA? (1 of 2) Standard persistence technology JPA 1.0 introduced in Java EE 5 JPA 2.0 is part of Java EE 6 standards Object/Relational Mapping Designed for highly distributed applications Especially web-enabled applications Life cycle manageable by application server to increase quality of service Simplifies development Provides a framework and tools around providing automatic persistence Objects are mapped to tables using metadata Metadata is used to transform data from one representation to another
Here are some of the general JPA concepts. JPA 1.0 was introduced back in Java EE 5. WebSphere Application Server has had a couple of releases supporting that, starting with the EJB3 feature pack for WebSphere Application Server Version 6.1 and the support was rolled into Version 7. JPA 2.0 is the next iteration of that specification. JPA provides a lot of functions, but one of the most important is the object mapping capability. If you are familiar with JDBC or CMP Beans or another older technology, then you are likely to see object to relational mapping as a difficult task. JPA provides the capability to do that mapping much easier and in such a way that it is much more applicable to object oriented programmers. JPA greatly simplifies development because it uses standardized metadata to map the objects to the tables. There are standardized mechanisms for accessing these entities whether they are being persisted or queried against. There is now a common query language that can be used. Many aspects of JPA are now standardized and therefore portable so moving between vendors is easier than it was in the past. For example each provider had their own implementation for CMP beans and they were different so moving from one provider to another was very difficult.
What is JPA? (2 of 2)
What is JPA? (2 of 2) JPA specifications made very significant simplifications: Standardizes O/R mapping metadata (not the case for EJB 2) Java SE 5.0 annotations can be used instead of XML deployment descriptor No deploy code implementing abstract classes— Entities are POJOs Application server is not required The Java Persistence API is available outside of containers Unit testing greatly simplified Removal of checked exceptions (for instance, remote exceptions) No bean (or business) interface required
The first JPA, which was shipped with Java EE 5, used the annotation support that was part of Java SE 5. Your metadata can be specified either using annotations, which are very intuitive and natural for programmers, or it can be done in XML so you have more control over how you want the mapping without having to change the code. One thing to highlight on this slide is that an application server is not required in order to reap the benefits of JPA. JPA is fully functional in a JSE mode. That aids development because developers can test their mapping to the database without needing an application server to do it. Once your database interaction is tested and modified to your liking you can bring it into your application in the model-view-controller type model.
Web application example – MVC framework
Web application example – MVC framework Database Java . Beans / Objects / DB interaction Model Controller View HTML / JSP / ... Action Form / Servlet / Support / ... To change existing applications from plain JDBC to use JPA No change required to View and Controller Add annotations to Java Objects Simplify DB interaction code Some configuration changes (for example, persistence.xml)
On this slide you see the Model View Controller model. One of the key things is that you want to separate your database actions (Model) from the rest of your application. That is where JPA comes into play because it can be done completely separate from the rest of your application. Presentation code goes in the View and your business logic in the Controller.
Annotate entity object class
Annotate entity object class import javax.persistence.*; @Entity public class Client { @Id @GeneratedValue(strategy=GenerationType.AUTO) private int ID; private String name; private String industry; private String headquarterLocation; private String cepClient; private String betaClient; private String productsInUse; private String wasVersions; public Client() { ... } //all necessary getter and setter methods ... } Import the javax.persistence packages Declare the class as an entity Identify a property as a primary key
This chart shows a very basic view of a JPA entity. Note here that there are two things required to have a JPA entity: the @Entity annotation and the @ID annotation. Both are necessary to show that this is an entity within the JPA specification terminology. This is what maps to rows in your table. The columns of your table are identified here by the properties of this entity. The @ID annotation defines your table’s primary key.
JPA compared to Plain JDBC – Insert
JPA compared to Plain JDBC – Insert @PersistenceContext(unitName=“test”) EntityManager em; public void insertClient(Client client){ doInsert(Client) }//end insertClient public void doInsert(Object newObject){ em.persist(newObject); }//end doInsert public void insertClient(Client client){ String insert ="INSERT INTO clientInfo ("name, industry,”+ “headquarterLocation, cepClient, betaClient,”+ “productsInUse, wasVersions) VALUES('" + client.getName()+"', '"+client.getIndustry()+"', '"+ client.getHeadquarterLocation()+"', '"+ client.getCepClient()+"','"+ client.getBetaClient()+"', '"+ client.getProductsInUse()+"', '"+client.getWasVersions()+"');" ; doInsert(insert); }//end insertClient public void doInsert(String insert){ Statement stmt; Connection conn; CONSTANTS c = new CONSTANTS(); try { java.lang.Class.forName(c.DRIVER).newInstance(); }catch (Exception E) { System.err.println("Unable to load driver."); E.printStackTrace(); } try{ conn = DriverManager.getConnection(c.DB_CONNECTION); stmt = conn.createStatement(); stmt.executeUpdate(insert); stmt.close(); conn.close(); }catch (SQLException E) { System.out.println("SQLException: " + E.getMessage()); } }//end doInsert JDBC JPA Controller Input Client info into JSP form Get input, assign values to new Client instance and call insertClient(c) View
This chart shows some differences in JDBC and JPA. As the WebSphere team has been learning over the past few releases with WebSphere many users are still using JDBC. Now with JPA you do not need to deal with all of the complexities you have been dealing with using JDBC. You do not need to know SQL or how to parse results from SQL; the JPA runtime does all of that for you. In the lower left corner note that the Client object here is a Plain Old Java Object (POJO). In order to write it to the database one updates the attributes of the object then persists the object. All of the data held in the attributes of the POJO are transferred to the data base without you having to write SQL. The attributes in your POJO correlate to columns in your database table.
JPA compared to plain JDBC – Retrieve
JPA compared to plain JDBC – Retrieve @PersistenceContext(unitName=“test”) EntityManager em; public Client [] getClients(){ String query = "SELECT c FROM Client c"; Query q = em.createQuery(query, Client.class); Client {} cArr = q.getResultList().toArray(); return cArr; }//end getClients public Client [] getClients(){ String query = "SELECT * FROM clientInfo;"; LinkedList list = doQuery(query); int size = list.size(); Client [] cArr = new Client[size]; for(int i=0; i