WebLogic 12c: Use JPA in your Web Application

Where as in WebLogic 11g JPA was not support by default, in WebLogic 12c it is the default persistency provider.JPA 2.0 is part of JAVA EE 6.

I was trying some new JAVA EE 6 features in WebLogic 12c, so here is a is a way to create a Web Application with JPA under WebLogic 12c

Some of the JAVA EE 6 specifications we’re already supported in WebLogic 11g. JPA 2.0 was one of them. Though version 1.0 was the default. 2.0 also worked.Unless an explicit <provider>…</provider> wass specified in the persistence.xml file of a deployed application, WebLogic 11g used OpenJPA/Kodo by default.

The default JPA provider setting is exposed via a new MBean: JPAMBean on the DomainMBean, and persists the configuration into the config.xml file.

Furthermore, you needed to install the patch QWG8 – Enable JPA 2.0 support on WebLogic Server.

To make it work on 11g, you had to use Oracle TopLink as the persistency provider like the image shows you in the WebLogic Admin Console

WebLogic 12c: Use JPA in your Web Application admin console jpa

Now for 12c this is not needed anymore, TopLink will be the default JPA Provider

We will create our own JAVA EE 6 Web Application with the NetBeans 7.1 IDE.

Choose File > New Project

Select Web Application from the Java Web category. Click Next.

Type WLS12cJPA for the project name and specify the project location.

Deselect the Use Dedicated Folder option, if selected. Click Next.

Select Oracle WebLogic Server from the Server dropdown list.

Select Java EE 6 as the Java EE Version. Click Next.WebLogic 12c: Use JPA in your Web Application New Web Application 2012 02 14 11 38 02

Use the registered JSF 2.1 library and Select JSF 2.1 from the  dropdown list. Click Finish.

WebLogic 12c: Use JPA in your Web Application New Web Application 2012 02 14 11 40 53

NetBeans creates the web application project and opens index.xhtml in the editor. The faces/index.xhtml acts as the default index page.In WebLogic 12c your weblogic.xml shows no reference anymore to JSF

 

WebLogic 12c: Use JPA in your Web Application WLS12cJPA NetBeans IDE 7

In 11g there was a reference in your weblogic.xml:

<library-ref>
      <library-name>jsf</library-name>
      <specification-version>2.0</specification-version>
      <implementation-version>1.0.0.0_2-0-2</implementation-version>
      <exact-match>true</exact-match>
  </library-ref>

For now, disable the fastswap feature by setting it to false in the weblogic.xml.

Connect to a database

We will now create the database connection by using the embedded derby database.

Click on the services tab in the left pane, and expand the derby APP tables

WebLogic 12c: Use JPA in your Web Application derby

Create the persistence unit

To have  persistence in the application you only need to create a persistence unit, specify which data source and entity manager to use, and then let the container do the work of managing entities and persistence. You create a persistence unit by defining it in persistence.xml.

Right-click the project node in the Projects window and choose Properties, and specify in the Sources category of the Properties window,  JDK 6 as the Source/Binary Format.

WebLogic 12c: Use JPA in your Web Application Project Properties WLS12cJPA 2012 02 14 12 46 24

–  Choose New File and select Persistence Unit from the Persistence category. Click Next.

WebLogic 12c: Use JPA in your Web Application New File 2012 02 14 12 50 52

–  Use the suggested unit name and create a new datasource from the derby connection, jdbc/jpasample.Edit the persistence.xml to use TopLink by specifying  oracle.toplink.essentials.PersistenceProvider as the persistence provider . The IDE will add the Oracle TopLink Essentials – 2.0.1 libraries to the classpath. In current and future versions of Oracle TopLink, Oracle TopLink Essentials is replaced by EclipseLink. Where possible, you should choose to use Oracle TopLink/EclipseLink instead of Oracle TopLink Essentials.

WebLogic 12c: Use JPA in your Web Application New Persistence Unit 2012 02 14 12 55 01

After clicking Finish the persistence unit will be created.

WebLogic 12c: Use JPA in your Web Application WLS12cJPA NetBeans IDE 7 2

Creating  Entity Classes

You will now use the Entity Classes from Database wizard to create the entity classes based on the  database.

  1. Choose New File
  2. Select Entity Classes from Database from the Persistence category. Click Next.
  3. In the Entity Classes from Database wizard, select jdbc/jpasample from the Data Source drop-down list and supply the password, if necessary.
  4. WebLogic 12c: Use JPA in your Web Application New File 2012 02 14 13 27 15
  5. Select the Customer table from the Available Tables and click Add. Click Next.The customertable and  related tables will be placed under Selected Tables.WebLogic 12c: Use JPA in your Web Application New Entity Classes from Database 2012 02 14 13 28 22
  6. Type ejb as the package for the generated classes. Click Finish.

After Finish, NetBeans creates entity classes for each of the selected tables.

You can expand the ejb source package node to view the generated entity classes.

Generating JSF Pages

In this exercise you will use a wizard to generate JSF pages based on the existing entity classes.

  1. Right-click the project node and choose New > Other.
  2. Select from Persistence JSF Pages from Entity Classes in the JavaServer Faces category . Click Next.WebLogic 12c: Use JPA in your Web Application New File 2012 02 14 13 43 11
  3. Click Add All to create JSF pages for all the available entities. Click Next.WebLogic 12c: Use JPA in your Web Application New JSF Pages from Entity Classes 2012 02 14 13 45 58
  4. Type web in the JSF fields. Click Finish.

When you click Finish, the IDE generates JSF 2.0 pages and controller and converter classes for the JSF pages.

Running the Project

In this exercise you will build and deploy the web application to the WebLogic Server. You will use the Run command in the IDE to build, deploy and launch the application.

Right-click the project node and choose Run.

When you click Run, building  and deploys the WAR archive to  WebLogic r and creates and registers the new JDBC datasource. The welcome page of the application (http://<weblogicHost>:7001/WLS12cJPA/) opens in your browser.

WebLogic 12c: Use JPA in your Web Application Facelet Title Google Chrome 2012 02 14 14 30 22

In WebLogic the application and data source are deployed

WebLogic 12c: Use JPA in your Web Application jdbc

More interesting features are for example to use JPA with Coherence as L2Cache, maybe in another post.

 

 

3 Comments

  1. hanynowsky February 21, 2012
  2. Michel Schildmeijer February 16, 2012
  3. Srikrishna February 16, 2012