Primefaces Spring EclipseLink NoSQL with MongoDB & Oracle NoSQL DB

We’ve explored how can we achieve integration between EclipseLink JPA and a well-known Relational Database like MySQL. EclipseLink provides you more than integrating with Relational Databases Platforms, it also has supported MongoDB and Oracle NoSQL databases.

Next coming releases will also give you the ability to use: Cassandra, Google Big Table and CouchDB as a NoSQL persistence stores. In this tutorial we would provide you a full-fledged example that help you make a JPA integration with MongoDB and Oracle NoSQL databases.

Required Tools

Final Project Structure

Employee Mapping

Here’s detailed explanations for above code:

  • Employee entity must be annotated with @Entity annotation. That annotation is required by JPA specification.
  • Employee identifier must be annotated with @Id. This attribute should be mapped into defaulted _id attribute. In case you’ve eliminated such that mapping, a new EMPLOYEEIDcolumn will be added into your Employee collection. The value of _id attribute has generated automatically by both of MongoDB and Oracle NoSQL databases.
  • You have an option of annotating your Employee’s attributes using @Field annotation. In case they’re eliminated the eclipselink will do the mapping automatically. If you weren’t created an Employee collection into your own MongoDB or Oracle NoSQL, eclipselink will do the process of creation behind you and it will insert your document (i.e. employee instance) respectively.
  • Your entity must be annotated with @NoSql. This annotation will make sure the entity being persisted doesn’t represent a relational entity. DataFormat attribute specify the type that’s used for representing the data stored. MongoDB and Oracle NoSQL use Key-BSON format which is similar to a map in structure, so MAPPED value is used.

MongoDB – Persistence Context

For connecting NoSQL persistence store, a persistence.xml file should be defined.

persistence.xml

Here’s detailed explanation for the code listed above:

  • NoSQL persistence unit are configured the same as JPA persistence units, persistence.xml is used to define the persistence unit.
  • The eclipselink.nosql.connection-spec specifies name of the EISConnectionSpec class which will be used to connect the NoSQL persistence store.
  • The eclipselink.target.database specifies the NoSQL platform class.

OracleNoSQL- Persistence Context

persistence.xml

Here’s below detailed explanation for listed code above:

  • Oracle NoSQL database provides you ability to create your own store at bootstrap time. If you’ve installed Oracle NoSQL database, you should execute java -Djava.net.preferIPv4Stack=true -jar libkvstore.jar kvlite for getting Oracle NoSQL database started. As a result of execution last command, you should notice  Created new kvlite store with args:
    -root ./kvroot -store kvstore -host mohammad-amr-lt -port 5000 -admin 5001
    entries.
  • Notice using of kvstorethat’s used in the persistence unit.

EclipseLink – Spring Context Integration

Eclipselink can be integrated seamlessy with Spring Framework. This type of integration will allow you acquiring the required entity manager easily and without any need for you to get involved.

applicationContext.xml

Here’s detailed explanation for code listed above:

  • Spring framework will be looking for its configuration context – by default applicationContext.xml – once the server getting its listener started. The listener of Spring framework should be defined inside your application’s deployment descriptor. See Primefaces Deployment Descriptor section below.
  • MongoDB is a Transactional persistence store, therefore, it’s required to include all Transactional staffs for getting your documents retained.
  • EntityManagerFactory configured this time by passing the name of the persistence unit. Unlike Oracle and MySQL, for creating an instance of it, EclipseJPAVendor instance should be passed.
  • Use given persistence unit name that’s defined for Oracle NoSQL once you want to connect Oracle NoSQL database.

Primefaces Deployment Descriptor

web.xml

Here’s detailed explanation for listed code above:

  • Defining of ContextLoaderListener is mandatory for starting up Spring framework.

Primefaces – Faces Configuration

faces-config.xml

Here’s detailed explanation for listed code above:

  • SpringBeanFacesELResolver will help you inject your Spring beans into your own JSF ManagedBean

Spring Employee Service

Here’s detailed explanation for the code listed above:

  • Using of @PersistenceContext will inject an instance of EntityManager. EntityManagerFactory that’s defined within Spring context is will be used for creating an instance of EntityManager

Primefaces RegisterEmployee ManagedBean

Primefaces Registration View

index.xhtml

MongoDB Demo

Here’s below, you would see all screen shots that explain you the employee while it’s registered and the persisted employee document against Mongo database.

Primefaces-Employee-Registered-Against-MongoDB-1024x189

  • We’ve used MongoVUE Tool for displaying Mongo database collections and records.
  • As we’ve mentioned earlier, _id column is added automatically for all of your defined collections. To map your collection id and _id defaulted one, @Filed(name=”_id”) annotation should be used. In case you’ve not mapped it, a new column EMPLOYEEID will be added.

OracleNoSQL Demo

 

 

Primefaces-Employee-Registered-Against-MongoDB-1024x189

Technical Help

Getting Oracle NoSQL database installed and started into your environment isn’t easy task. Following main steps you need to ensure that help you achieve that.

  • Download Oracle NoSQL binary.
  • Unzip downloaded file.
  • Execute java -Djava.net.preferIPv4Stack=true -jar libkvstore.jar kvlite for starting the database.
  • Configuring Apache Tomcat 8 by adding -Djava.net.preferIPv4Stack=true into its VM arguments.
  • Oracle-NoSQL-Terminal-Commands

Maven Dependencies File

pom.xml

Summary

Not only Relational databases can be integrated with a JPA persistence layer. EclipseLink provides you the ability to integrate seamlessly with two major popular NoSQL vendors. This tutorial has used both of Mongo and Oracle NoSQL databases for persisting an instance of employee entity. Contribute us by commenting below and find project source code to download.

By admin

Leave a Reply