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
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.
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
In 11g there was a reference in your weblogic.xml:
<strong><library-ref> <library-name>jsf</library-name> <specification-version>2.0</specification-version> <implementation-version>18.104.22.168_2-0-2</implementation-version> <exact-match>true</exact-match> </library-ref></strong>
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
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.
- Â Choose New File and select Persistence Unit from the Persistence category. Click Next.
- Â 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.
After clicking Finish the persistence unit will be created.
Creating Â Entity Classes
You will now use the Entity Classes from Database wizard to create the entity classes based on the Â database.
- Choose New File
- Select Entity Classes from Database from the Persistence category. Click Next.
- In the Entity Classes from Database wizard, selectÂ jdbc/jpasample from the Data Source drop-down list and supply the password, if necessary.
- Select theÂ Customer table from the Available Tables and click Add. Click Next.The customertable and Â related tables will be placed under Selected Tables.
- 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.
- Right-click the project node and choose New > Other.
- Select from Persistence JSF Pages from Entity Classes in the JavaServer Faces category . Click Next.
- Click Add All to create JSF pages for all the available entities. Click Next.
- 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.
In WebLogic the application and data source are deployed
More interesting features are for exampleÂ to use JPA with Coherence as L2Cache, maybe in another post.
- Fast Swap in WebLogic 10.3 (and JDeveloper 11g) – redeploy after compile in running application
- WebLogic 12c released!
- Combine Oracle Toplink and the Spring Framework – Rod Johnson & Jim Clark
- Recent changes in the EJB 3.0 Persistence specification (that invalidated my code)
- Toplink's Object to XML mapping
- new Puppet 3 Weblogic provisioning module
- The AMIS Summary of Oracle OpenWorld 2013 is available for download – 60-page white paper
- On the integrity of data in Java applications – presentation from JFall 2013
- WebLogic & FMW Provisioning update
- The road ahead for WebLogic 12c
- OOW13: Cloud roadmap & announcements – from zero to H/A WebLogic Cluster plus Database RAC in 30 minutes
- ADF 12c: Using WebSockets to implement client to client push (in a scalable manner)
- JDeveloper 12c & ADF 12c: Launch of the next generation
- Weblogic Admin and Managed servers as a Windows service
- Dear Java/JEE developer – why should you care about ADF?