Sunday, February 17, 2013

Possible ways to use MongoDB in next Servlet based project


Hibernate OGM
No JPQL query support,
must use raw Mongo query language directly and as a result
getting back Mongo Objects, not entities

To solve this the project integrates with Hibernate Search which uses
Lucene as a search engine.

When you insert/update an entity the data is also added to the search engine
This is pretty good for a full text search capabilities
and later can use JPQL against the Lucene instead of against MongoDB
This means that must install/configure and depend on Lucene

Upside: relatively easy to setup without upgrading anything on Application server
Can find entity by _id easily

Downside: either depend on Lucene OR use only native MongoDB queries

====================

EclipseLink NoSQL
Support for JPQL
Mongo document will be a normal entity
Can also use native MongoDB queries if JPQL just cannot do certain queries

Overall this is the most convenient way and pretty sweet
The downside is that Application Server may not include the latest version of the
EclipseLink jpa libriries and as a result must update the jar files directly in the
application server, which may not be possible in case of cloud hosting

===========

No dependencies on anything - just use MongoDB drivers
directly - no entities.
Get cursor back, iterate over results manually, add to collection,
then return collection
Write classes that know how to deal with BasicDBObject from mongo
For example when creating new Question a class QuestionParser will
take the request object, generate SubmittedQuestgionWWW, get all
data from it, put data into BasicObject
and then save BasicObject into DB

Must create indexes using "EnsureIndex" or something like that.

https://openshift.redhat.com/community/blogs/spatial-jee6-jax-rs-cdi-mongodb-on-paas
Something like this: (Requires CDI support)
@Named
@ApplicationScoped
public class DBConnection {

  private DB mongoDB;

  @PostConstruct
  public void afterCreate() {
    …
  }

  …
}

Most likely no JEE server is required, so Tomcat with CDI support
is all it takes

=========================

Mongo JEE
https://github.com/angelozerr/mongo-jee
Has good support for pagination
and integration with JAX RS
Also maps mongo cursor directly to
output stream json - no need to
create List from mongo cursor manually

Looks like will work on just about any server
that has CDI, most likely
inside tomcat container, no JEE application server
required.

============================



Saturday, February 16, 2013

MongoDB in EJB project with EclipseLink


First you need to update persistense related jar files
in Glassfish modules folder

Update 7 persistence related jar files
in glassfish/modules
This folder in my PC is located
in c:\Program Files\glassfish-3.1.2.2\glassfish\modules

first backup current files just in case - create a new sub-folder _backup

move the files from modules to this new folder:
org.eclipse.persistence.antlr.jar
org.eclipse.persistence.jpa.jar
org.eclipse.persistence.asm.jar
org.eclipse.persistence.jpa.modelgen.jar
org.eclipse.persistence.core.jar
org.eclipse.persistence.oracle.jar
javax.persistence.jar

Then from eclipselink website download new zip file,
look for OSGI bundle:
http://www.eclipse.org/eclipselink/downloads/index.php#2.4

unzip that downloaded zip into some directory like
c:\javalibs

Then copy the updated versions on the same 7 .jar files
to
glassfish\modules directory
In case here:
c:\Program Files\glassfish-3.1.2.2\glassfish\modules
and then rename each of these .jar files to remove the versioning part of
file name, for example
org.eclipse.persistence.asm_3.3.1.v201206041142.jar
should be renamed to
org.eclipse.persistence.asm.jar

Do the same of the other updated files

Finally add these 2 .jar files to your project libs via NetBeans:
org.eclipse.persistense.nosql_xxxxxxx.jar (xxxxx is a version string like _2.4.1.v20121003-ad44345.jar
and also download the java client .jar from mongodb download site
and also add the mongo-java-driver-2.6.2.jar to your project library (the actual latest version may
be greater than 2.6.2 by now)



Restart Glassfish server, this can be done from inside NetBeans

The mongodb can be used now, so a regular @Entity annotation can be used
to persist data to mongo

So far so good.

Mongodb does not have transaction support so you will be use translation-type="RESOURCE_LOCAL"
in the persistence-unit section in persistence.xml

Then you have to provide your own transaction support in the classes

Some examples and basic documentation
is here

http://wiki.eclipse.org/EclipseLink/Examples/JPA/NoSQL