Hibernate with MongoDB Hibernate with MongoDB spring spring

Hibernate with MongoDB


You can't easily do this. The point of Hibernate is to map Java Objects to a relational database. Although Hibernate abstracts a lot of details away you still need to understand how relational databases work with things such as foreign and primary keys, and the performance implications of queries you run. MongoDB requires an entire different way of designing your database focusing on objects instead of columns and tables. while you may be able to create a Hibernate dialect for MongoDB creating a design that would work on both a relational database and a NoSql database will give you a design that works poorly on both.


What about Hibernate OGM? It provides JPA for No-SQL databases.


Migration would be easier if you use Spring MongoTemplate (similar to HibernateTemplate).
Among its features is support for JPA annotations (although, I'm not sure to what extent).
See more: http://www.springsource.org/spring-data/mongodb

You'll need the following:

  1. Add spring-data-mongodb JAR to your project (available in mavencentral).
  2. Add mongo-java-driver JAR to your project (available inmaven central).
  3. Use the provided MongoTemplate class in a similarmanner to HibernateTemplate. E.g.:mongoTemplate.findById(id, MyClass.class);
    mongoTemplate.insert(myObject);

Here's a concrete example with code: use-spring-and-hibernate-with-mongodb