I am new to working with databases, so my question is probably naive. I currently have a project using Spring Data with JPA. I am using Hibernate and MySQL under the hood. Is it possible to switch to MongoDB? When I googled "jpa MongoDB" the top link showed something interesting (DataNucleus JPA and MongoDB). This link: MongoDB docs appears to show that the Java driver is JPA compliant, so should it be simple to swap out database implementations?
The Spring Data MongoDB project provides integration with the MongoDB document database. Key functional areas of Spring Data MongoDB are a POJO centric model for interacting with a MongoDB DBCollection and easily writing a Repository style data access layer.
Hibernate OGM provides Java Persistence API (JPA) support for NoSQL datastores. NoSQL is an umbrella term covering a wide variety of data storage. For example, this includes key-value, document, column-oriented and graph-oriented datastores.
Spring Boot is the easiest way to spin a spring project quickly and MongoDB is the most popular NoSQL database. Let's see how to integrate spring with MongoDB database.
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.
It highly depends on your definition of "work". "Generally work"? - Yes. "Reasonably work" - depends. "Works seamlessly" - not so much.
JPA is an inherently relational API, so be prepared you run into these aspects:
transaction.rollback()
? Strictly speaking, you can't implement JPA 100% (by definition) and most of the self-proclaimed JPA implementations for NoSQL are basically providing a tiny subset of JPA: some of the annotations for mapping, some of the EntityManager
API.While I can see the incentive to put a familiar API over something new to gain some kind of knowledge transfer but ultimately think this is a fallacy if the target space doesn't support key aspects (e.g. transactions). All of the approaches I've seen so far end up spending a huge part of their documentation to document what's not supported for both the JPA and the store side of things.
That said, there are other approaches moving away from "one API to rule them all" and proactively leveraging the diversity in the NoSQL space (think about it: a set of technologies defined by what they are not will be quite diverse by definition).
The Spring Data project (disclaimer: I am the lead of this) is moving up one level of abstraction and rather provides a consistent repository programming model that than one, unifying API. This allows still providing support for store specific functionality but the general means of use stay the same.
So I suggest to move to the dedicated Spring Data MongoDB project here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With