Using Hibernate OGM to persist objects to MongoDB (deploying to Wildfly 8.2)

For storing data when you’re less concerned about relationships between your data but more interested in entities (documents) and their attributes, a document-based datastore like MongoDB makes a lot of sense. MongoDB’s Java Driver API though is rather clunky in it’s usage pattern.

So I started looking for alternatives. The MongoDB Java ecosystem page has a good collection of some libraries to consider. Initially I took a quick look at MongoJack, which looked like an interesting approach to map between Java POJOs to json data representations (using the Jackson api). I remember reading a while back at Morphia, and was interested what a JPA based approach to interacting with MongoDB would look like. Somewhere I stumbled across the Hibernate OGA project – being a fan of ORM approaches popularized by Hibernate, I wanted to take a look.

The current Hibernate OGM docs (4.1) are here.

For deploying on JBoss/WildFly, OGM is supplied as modules in a zip that you can unzip directly into the modules dir on the server. See here more more details.

Deploying to OpenShift, as for most cartridges, your configuration values are defined in environment variables. Instead of hardcoding the properties and values in your persistence.xml, you can have them passed to your server at startup by adding them to a JAVA_OPTS_EXT file like this (do this one time to create the file):

    -Dhibernate.ogm.datastore.port=$OPENSHIFT_MONGODB_DB_PORT \
    -Dhibernate.ogm.datastore.username=$OPENSHIFT_MONGODB_DB_USERNAME \
    -Dhibernate.ogm.datastore.password=$OPENSHIFT_MONGODB_DB_PASSWORD" \
    > .env/user_vars/JAVA_OPTS_EXT

Mapping your Entities and Id properties

At some point support for the ObjectId MongoDB id type was added. In the current docs don’t use the uuid id generator, use the ‘objectid’ type described here instead:

@GeneratedValue(strategy = GenerationType.IDENTITY)
@Type(type = "objectid")


JPA Persistence.xml

Here’s what my persistence.xml looks like:
[code language=”xml”]

org.hibernate.ogm.jpa.HibernateOgmPersistence if entities not in same jar, list here


2 Replies to “Using Hibernate OGM to persist objects to MongoDB (deploying to Wildfly 8.2)”

  1. Hi,

    First of all thanks for your articles.

    I have a question: why I have to list all entity classes when using JTA transation type? Why entity manager does not see it? To specify package – does not work. Are the any other options rather than listing all entities?

    1. I believe this is a JPA restriction, not OGM, where entities need to be in the same jar/project as your persistence.xml in order to get found, if running in an EEE container. If running in a standalone app, then I think you need to explicitly specify the entities anyway, or in EE if the entities are spread across multiple jars or in a different jar from where the persistence.xml is located. Hibernate has some non-standard JPA extensions where you can turn on autoscanning for entities – there’s a post here where it mentions this feature:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.