8

I am trying to migrate a hibernate/mysql project to mongodb. Does hibernate support migration to mongodb? If so, how are ORM mappings like one-to-one or one-to-many relationships translated?

Hans Then
  • 10,149
  • 2
  • 30
  • 49
Murali
  • 319
  • 2
  • 5
  • 9

3 Answers3

8

Hibernate supports relational databases, like MySQL, PostgreSQL, Oracle, DB2 and SQL Server. MongoDB, however, isn't a relational database, it's a document database. The differences are quite huge:

  • a relational database defines relations between tables. Tables consist of rows and columns. The columns, together with any type or relation constraints on them, define the so-called schema.
  • a document database defines document collections. Documement database don't know a schema: every document can have different properties. Note that these aren't called "rows" and "columns", but "documents" and "properties" instead.

So, to answer your question, Hibernate doesn't support MongoDB and I think chances are close to zero that it ever will.

However, there are ORM implementations for Java / MongoDB, for example MJORM.

However, since these are two completely different points-of-view to database organisation, there is no simple process to migrate. At the very least it will include re-thinking your database design. So it may be worth to re-consider the migration, and see whether or not it is actually needed. In the end, MongoDB is not a drop-in replacement for a relational database, since it isn't a relational database. See the following links for some discussion on the two types of databases:

UPDATE Regarding Hibernate OGM

You can probably use Hibernate OGM. IMHO, there are two drawbacks:

  1. Hibernate OGM Is currently in beta; there has been no official release yet, and the last beta was released last January, that's half a year ago. It depends on your project whether that is an acceptable risk or not.
  2. Hibernate OGM provides a JPA implementation for NoSQL storage (among them MongoDB). According to your question, you're currently using Hibernate, and not JPA. That means you'll still have to change your domain objects to use JPA annotations instead of Hibernate annotations.
Community
  • 1
  • 1
mthmulders
  • 8,940
  • 4
  • 29
  • 51
2

Also it's worth to look at Morphia https://github.com/mongodb/morphia. It's basically Java ORM library similar to Hibernate but does not support JPA annotations.

Udit
  • 299
  • 3
  • 5
1

Your best choice should be to go through Spring Data MongoDB

It's quiet straight forward to implement.

Have look there: http://www.mkyong.com/mongodb/spring-data-mongodb-hello-world-example/

RcD
  • 11
  • 1