Search code examples
javamongodbhibernatehibernate-ogm

Should we use OGM/ORM like hibernate in java for Mongodb?


Recently, I have started working on mongo. As the whole concept of mongodb is schemaless and document-oriented, how can I transition relationships to entities?

I have used hibernate ORM in java for mapping objects to tables. Is there a need to do the same for mongodb?

Things which restrict me from using OGM:

  1. Once the Java Entity is mapped to a collection in mongo, will the concept of mongo being schemaless still be valid. In this case I have to map member variables to keys of that collection. Also if a mongo collection is supposed to contain nested objects or an array of objects, then what?

  2. Hibernate has launched their OGM for NoSQL databases recently (January 2015).

Could anyone help me out with the decision to choose OGM for mongo?


Solution

  • Edit: Kundera is no longer maintained. Please use alternatives.


    Of course, ORMs are easy to use and many of them allow us to use SQL or JPQL for querying in a similar way like RDBMS but ORMs have their own limitation and yeah they will add a little bit of overhead too(in terms of performance). That's why using Native MongoDB Java Driver is preferred.

    For NOSQL databases, choosing an ORM purely depends upon the use case. An ORM will map your table(Collection in the case of MongoDB) to an Entity Object. You added, you can keep a map in entity which will allow your design to become a little bit of schemaless. Most of the time our data is almost structured so ORMs can be used. But if you have completely versatile and unstructured data go for Java Driver. Check Mongo's documentation.

    Also, there are other stable ORM tools like Kundera, Spring Data. You should explore them.

    PS:

    • This answer is for ORMs vs native driver not on Hibernate ORM
    • I am a Kundera developer