Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Migrate spring hibernate mysql to mongodb

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?

like image 629
Murali Avatar asked Aug 02 '13 06:08

Murali


People also ask

Can Hibernate be used with MongoDB?

How to use Hibernate OGM with MongoDB? To get started with Hibernate MongoDB, you first need to build the OGM from Source via GitBug. To include OGM in your Hibernate MongoDB project, just include one more dependency in your list of dependencies. Next, to define the persistence unit, create a META-INF/persistence.

Can we use JPA with MongoDB?

Yes, DataNucleus JPA allows it, as well as to many other databases.


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:

  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.
like image 194
mthmulders Avatar answered Oct 03 '22 21:10

mthmulders


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.

like image 33
Udit Avatar answered Oct 03 '22 21:10

Udit


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/

like image 25
RcD Avatar answered Oct 03 '22 21:10

RcD