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?
3 Answers
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:
- Document Database versus Relational Database : how to choose?
- Pros/cons of document-based databases vs. relational databases
- Wikipedia: Document-oriented database
- Wikipedia: Relational database
UPDATE Regarding Hibernate OGM
You can probably use Hibernate OGM. IMHO, there are two drawbacks:
- 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.
- 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.

- 1
- 1

- 9,483
- 4
- 37
- 54
-
Can you please tell me what are the steps i have to follow to migrate spring java hibernate to spring java mongodb project? – Murali Aug 02 '13 at 07:12
-
Thanks for sending valuable link. – Murali Aug 02 '13 at 07:32
-
1Does Hibernate OGM give better solution to migration? – Murali Aug 02 '13 at 09:40
-
OGM is not in beta anymore. Latest stable release is 5.3.1 (2018-03-28). – Jose Duarte May 01 '18 at 17:36
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.

- 319
- 3
- 5
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/

- 11
- 1