Java EE 6 and NoSQL/MongoDB on GlassFish using JPA and EclipseLink 2.4 (TOTD #175)
Posted
by arungupta
on Oracle Blogs
See other posts from Oracle Blogs
or by arungupta
Published on Wed, 11 Apr 2012 01:30:00 -0500
Indexed on
2012/04/11
11:36 UTC
Read the original article
Hit count: 546
/General
TOTD #166 explained how to use MongoDB in your Java EE 6 applications. The code in that tip used the APIs exposed by the MongoDB Java driver and so requires you to learn a new API. However if you are building Java EE 6 applications then you are already familiar with Java Persistence API (JPA). Eclipse Link 2.4, scheduled to release as part of Eclipse Juno, provides support for NoSQL databases by mapping a JPA entity to a document. Their wiki provides complete explanation of how the mapping is done.
This Tip Of The Day (TOTD) will show how you can leverage that support in your Java EE 6 applications deployed on GlassFish 3.1.2.
Before we dig into the code, here are the key concepts ...
- A POJO is mapped to a NoSQL data source using @NoSQL or <no-sql> element in "persistence.xml".
- A subset of JPQL and Criteria query are supported, based upon
the underlying data store
- Connection properties are defined in "persistence.xml"
- Download the latest EclipseLink 2.4 Nightly Bundle. There is a Installer, Source, and Bundle - make sure to download the Bundle link (20120410) and unzip.
- Download GlassFish
3.1.2 zip and unzip.
- Install the Eclipse Link 2.4 JARs in GlassFish
- Remove the following JARs from "glassfish/modules":
org.eclipse.persistence.antlr.jar org.eclipse.persistence.asm.jar org.eclipse.persistence.core.jar org.eclipse.persistence.jpa.jar org.eclipse.persistence.jpa.modelgen.jar org.eclipse.persistence.moxy.jar org.eclipse.persistence.oracle.jar
- Add the following JARs from Eclipse Link 2.4 nightly build
to "glassfish/modules":
org.eclipse.persistence.antlr_3.2.0.v201107111232.jar org.eclipse.persistence.asm_3.3.1.v201107111215.jar org.eclipse.persistence.core.jpql_2.4.0.v20120407-r11132.jar org.eclipse.persistence.core_2.4.0.v20120407-r11132.jar org.eclipse.persistence.jpa.jpql_2.0.0.v20120407-r11132.jar org.eclipse.persistence.jpa.modelgen_2.4.0.v20120407-r11132.jar org.eclipse.persistence.jpa_2.4.0.v20120407-r11132.jar org.eclipse.persistence.moxy_2.4.0.v20120407-r11132.jar org.eclipse.persistence.nosql_2.4.0.v20120407-r11132.jar org.eclipse.persistence.oracle_2.4.0.v20120407-r11132.jar
- Start MongoDB
- Download latest MongoDB from here (2.0.4 as of this writing).
- Create the default data directory for MongoDB as:
sudo mkdir -p /data/db/
Refer to Quickstart for more details.
sudo chown `id -u` /data/db
- Start MongoDB as:
arungup-mac:mongodb-osx-x86_64-2.0.4 <arungup> ->./bin/mongod
./bin/mongod --help for help and startup options
Mon Apr 9 12:56:02 [initandlisten] MongoDB starting : pid=3124 port=27017 dbpath=/data/db/ 64-bit host=arungup-mac.local
Mon Apr 9 12:56:02 [initandlisten] db version v2.0.4, pdfile version 4.5
Mon Apr 9 12:56:02 [initandlisten] git version: 329f3c47fe8136c03392c8f0e548506cb21f8ebf
Mon Apr 9 12:56:02 [initandlisten] build info: Darwin erh2.10gen.cc 9.8.0 Darwin Kernel Version 9.8.0: Wed Jul 15 16:55:01 PDT 2009; root:xnu-1228.15.4~1/RELEASE_I386 i386 BOOST_LIB_VERSION=1_40
Mon Apr 9 12:56:02 [initandlisten] options: {}
Mon Apr 9 12:56:02 [initandlisten] journal dir=/data/db/journal
Mon Apr 9 12:56:02 [initandlisten] recover : no journal files present, no recovery needed
Mon Apr 9 12:56:02 [websvr] admin web console waiting for connections on port 28017
Mon Apr 9 12:56:02 [initandlisten] waiting for connections on port 27017 - Check out the JPA/NoSQL sample from SVN
repository. The complete source code built in this TOTD
can be downloaded
here.
- Create Java EE 6 web app
- Create a Java EE 6 Maven web app as:
mvn archetype:generate -DarchetypeGroupId=org.codehaus.mojo.archetypes -DarchetypeArtifactId=webapp-javaee6 -DgroupId=model -DartifactId=javaee-nosql -DarchetypeVersion=1.5 -DinteractiveMode=false
- Copy the model files from the checked out workspace to the
generated project as:
cd javaee-nosql
cp -r ~/code/workspaces/org.eclipse.persistence.example.jpa.nosql.mongo/src/model src/main/java - Copy "persistence.xml"
mkdir src/main/resources cp -r ~/code/workspaces/org.eclipse.persistence.example.jpa.nosql.mongo/src/META-INF ./src/main/resources
- Add the following dependencies:
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa</artifactId>
<version>2.4.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.nosql</artifactId>
<version>2.4.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.7.3</version>
</dependency>
The first one is for the EclipseLink latest APIs, the second one is for EclipseLink/NoSQL support, and the last one is the MongoDB Java driver.
And the following repository:
<repositories> <repository>
<id>EclipseLink Repo</id>
<url>http://www.eclipse.org/downloads/download.php?r=1&nf=1&file=/rt/eclipselink/maven.repo</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories> - Copy the "Test.java" to the generated project:
mkdir src/main/java/example
This file contains the source code to CRUD the JPA entity to MongoDB. This sample is explained in detail on EclipseLink wiki.
cp -r ~/code/workspaces/org.eclipse.persistence.example.jpa.nosql.mongo/src/example/Test.java ./src/main/java/example/
- Create a new Servlet in "example" directory as:
package example;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author Arun Gupta
*/
@WebServlet(name = "TestServlet", urlPatterns = {"/TestServlet"})
public class TestServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet TestServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet TestServlet at " + request.getContextPath() + "</h1>");
try {
Test.main(null);
} catch (Exception ex) {
ex.printStackTrace();
}
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
} - Build the project and deploy it as:
mvn clean package
glassfish3/bin/asadmin deploy --force=true target/javaee-nosql-1.0-SNAPSHOT.war - Accessing http://localhost:8080/javaee-nosql/TestServlet
shows the following messages in the server.log:
connecting(EISLogin(
platform=> MongoPlatform
user name=> ""
MongoConnectionSpec()
))
. . .
Connected:
User:
Database: 2.7 Version: 2.7
. . .
Executing MappedInteraction()
spec => null
properties => {mongo.collection=CUSTOMER, mongo.operation=INSERT}
input => [DatabaseRecord(
CUSTOMER._id => 4F848E2BDA0670307E2A8FA4
CUSTOMER.NAME => AMCE)]
. . .
Data access result: [{TOTALCOST=757.0, ORDERLINES=[{DESCRIPTION=table,
LINENUMBER=1, COST=300.0}, {DESCRIPTION=balls, LINENUMBER=2, COST=5.0},
{DESCRIPTION=rackets, LINENUMBER=3, COST=15.0}, {DESCRIPTION=net,
LINENUMBER=4, COST=2.0}, {DESCRIPTION=shipping, LINENUMBER=5,
COST=80.0}, {DESCRIPTION=handling, LINENUMBER=6, COST=55.0},
{DESCRIPTION=tax, LINENUMBER=7, COST=300.0}], SHIPPINGADDRESS=
[{POSTALCODE=L5J1H7, PROVINCE=ON, COUNTRY=Canada, CITY=Ottawa,
STREET=17 Jane St.}], VERSION=2, _id=4F848E2BDA0670307E2A8FA8,
DESCRIPTION=Pingpong table, CUSTOMER__id=4F848E2BDA0670307E2A8FA7,
BILLINGADDRESS=[{POSTALCODE=L5J1H8, PROVINCE=ON, COUNTRY=Canada,
CITY=Ottawa, STREET=7 Bank St.}]}]
You'll not see any output in the browser, just the output in the console. But the code can be easily modified to do so.
Once again, the complete Maven project can be downloaded
here.
Do you want to try accessing relational and non-relational (aka
NoSQL) databases in the same PU ?
© Oracle Blogs or respective owner