TOTD #106 explained how to install Oracle database 10g R2 on Mac OS X. TOTD #107 explained how to connect this Oracle database using NetBeans. This Tip Of The Day will explain how to use the sample HR database (that comes with Oracle database server) to write a simple Java EE 6 application.
This application will use Java Server Faces 2.0 for displaying the results, Enterprise Java Beans 3.1 + Java Persistence API 2.0 for middle tier, and Oracle database server + GlassFish v3 as the backend. The latest promoted build (65 of this writing) will not work because of the issue #9885 so this blog will use build 63 instead.
Several improvements have been made over NetBeans 6.8 M1 build and this blog is using the nightly build of 9/27. The environment used in this blog is:
- NetBeans 9/27 nightly
- GlassFish v3 build 63
- Oracle database server 10.2.0.4.0 R2 on Mac OS X
- Oracle JDBC Driver type 4 (ojdbc6.jar)
Lets get started!
-
Configure GlassFish v3 with JDBC connection
- Download and unzip build 63.
- Download ojdbc6.jar and copy to "glassfishv3/glassfish/domains/domain1/lib/ext" directory.
-
Start the Application Server as:
./bin/asadmin start-domain --verbose & -
Create a JDBC connection pool as:
./bin/asadmin create-jdbc-connection-pool --datasourceclassname oracle.jdbc.pool.OracleDataSource --restype javax.sql.DataSource --property "User=hr:Password=hr:URL=jdbc\:oracle\:thin\:@localhost\:1521\:orcl" jdbc/hrand verify the connection pool as:
./bin/asadmin ping-connection-pool jdbc/hr -
Create a JDBC resource as:
./bin/asadmin create-jdbc-resource --connectionpoolid jdbc/hr jdbc/hr
-
Configure GlassFish v3 build 63 in NetBeans
- In NetBeans IDE "Services" panel, right-click on "Servers" and click on "Add Server…". Choose "GlassFish v3" and provide a name as shown below:

- Click on "Next >" and specify the unzipped GlassFish location as:

and click on "Finish".
- In NetBeans IDE "Services" panel, right-click on "Servers" and click on "Add Server…". Choose "GlassFish v3" and provide a name as shown below:
-
Create the Java EE 6 application
- In "Projects" pane, right-click and select "New Project…".
- Choose "Java Web" and "Web Application" and click on "Next". Choose the project name as "HelloOracle":

and click on "Next >".
- Select the recently added GlassFish v3 server and choose "Java EE 6 Web" profile:

and click on "Next >". Notice "Java EE 6 Web" profile is chosen as the Java EE version.
- Select "JavaServer Faces" on the frameworks page:

and click on "Finish". Notice the JSF libraries bundled with the App Server are used.
-
Create the Java Persistence Unit
- Right-click on the project, select "New", "Entity Classes from Database…":

- From the Data Source, select "jdbc/hr" as shown:

This is the same JDBC resource created earlier. Select "EMPLOYEES" from the Available Table, click on "Add >" to see the output as:

The related tables are automatically included. Click on "Next >".
- Click on "Create Persistence Unit …" and take all the defaults and click on "Create".
- Specify the package name as "model":

and click on "Finish". This generates a JPA-compliant POJO class that provide access to tables in the underlying Oracle database. The class name corresponding to each table is shown in the wizard.
- Right-click on the project, select "New", "Entity Classes from Database…":
-
Create Enterprise Java Beans
- Right-click on the project and select "New Class…".
- Specify the class name as "EmployeesBean" and package as "controller", click on "Finish".
-
Annotate the class to make it an Enterprise Java Bean and a JSF Managed Bean as:
@javax.ejb.Stateless @javax.faces.bean.ManagedBeanNotice, the EJB is bundled in the WAR file and no special type of modules are required. Java EE 6 provides simplified packaging of EJB which makes it really ease to use.
Also this application is currently using JSF managed bean but will use JSR 299 (aka Web Beans) in a future blog.
-
Inject the Persistence Unit by adding the following variable:
@PersistenceUnit EntityManagerFactory emf; -
Add a new method to retrieve the list of all employees as:
public List getEmployees() { return em.createNamedQuery("Employees.findAll").getResultList(); }"Employees.findAll" is a default NamedQuery generated by NetBeans and makes it easy to query the database. Several other queries are generated for each mapped JPA class, such as "Employees.findByEmployeeId" and "Employees.findByFirstName". Custom queries can also be created and specified on the POJO class.
The completed class looks like:
@Stateless @ManagedBean public class EmployeesBean { @PersistenceContext EntityManager em; public List getEmployees() { return em.createNamedQuery("Employees.findAll").getResultList(); } }
-
Use EJB in the generated JSF page
- JSF 2 uses Facelets as the templating mechanism and NetBeans generate a simple "index.xhtml" file to start with. Expand "Web Pages" and open "index.xhtml".
-
Replace the body template with:
<h1>First Java EE 6 app using Oracle database</> <h:dataTable var="emp" value="#{employeesBean.employees}" border="1"> <h:column><h:outputText value="#{emp.lastName}"/>, <h:outputText value="#{emp.firstName}"/></h:column> <h:column><h:outputText value="#{emp.email}"/></h:column> <h:column><h:outputText value="#{emp.hireDate}"/></h:column> </h:dataTable>It uses JSF value expressions to bind the Enterprise Java Bean and dumps the HTML formatted name, email, and hire date of each employee in the database.
- Run the project: Right-click on the project and select "Run" to see the output at "http://localhost:8080/HelloOracle/" as:

So we can easily create a Java EE 6 application using NetBeans, Oracle, and GlassFish.
A complete archive of all the TOTDs is available here.
This and other similar applications will be demonstrated at the upcoming Oracle Open World.
Technorati: totd oracle database glassfish v3 javaee javaserverfaces ejb jpa netbeans oow
Related posts:- TOTD #112: Exposing Oracle database tables as RESTful entities using JAX-RS, GlassFish, and NetBeans
- TOTD #94: A simple Java Server Faces 2.0 + JPA 2.0 application – Getting Started with Java EE 6 using NetBeans 6.8 M1 & GlassFish v3
- TOTD #95: EJB 3.1 + Java Server Faces 2.0 + JPA 2.0 web application – Getting Started with Java EE 6 using NetBeans 6.8 M1 & GlassFish v3
- TOTD #93: Getting Started with Java EE 6 using NetBeans 6.8 M1 & GlassFish v3 – A simple Servlet 3.0 + JPA 2.0 app
- TOTD #107: Connect to Oracle database using NetBeans
[...] showed Deploy-on-Save, Preserving session state across deployments, Java EE 6 wizards in NetBeans (1, 2, 3, 4, 5) and Eclipse (1, 2, 3), Metro JAX-WS Web services development in Eclipse, GlassFish v3 [...]
Pingback by Silicon Valley Code Code Camp 2009 Trip Report « Miles to go … — October 4, 2009 @ 5:41 am
[...] schema. The steps outlined below uses NetBeans solely for creating the JPA entities. Alternatively, TOTD #108 explains how to define a JDBC connection pool and JDBC resource using "asadmin" CLI and [...]
Pingback by TOTD #112: Exposing Oracle database tables as RESTful entities using JAX-RS, GlassFish, and NetBeans « Miles to go … — October 8, 2009 @ 2:31 am
[...] EE 6 wizards using NetBeans and [...]
Pingback by Oracle Open World 2009 – Day 2 Report « Miles to go … — October 12, 2009 @ 10:10 pm
To reproduce your sample with bundle Netbeans 6.8M2 & Glassfish V3 b66 had to remove b66 and add b63.
Comment by Boris — October 17, 2009 @ 1:34 pm
Great article, i wonder if oracle.jdbc.pool.OracleDataSource can works for several concurrent connections or do I need a XA class?
Comment by Pepo — November 4, 2009 @ 8:34 pm
Pepo, that’s more a question for OTN, don’t know the answer.
Comment by arungupta — November 4, 2009 @ 9:24 pm
i host 5 of my blogs on Blogspot and it is really good for beginners. but if you want something with more features, nothing beats wordpress.”-
Comment by Emily Williams — May 13, 2010 @ 9:07 pm
Ha! And where editing data from database?
Comment by Ololo — July 4, 2010 @ 4:25 am