我们应该使用 OGM/ORM 就像 java 中的休眠模式一样用于 Mongodb 吗?

Should we use OGM/ORM like hibernate in java for Mongodb?

最近,我开始研究 mongo。由于 mongodb 的整个概念是无模式和面向文档的,我如何将关系转换为实体?

我在 java 中使用 hibernate ORM 将对象映射到表。是否需要对 mongodb 执行相同的操作?

限制我使用 OGM 的因素:

  1. 一旦 Java 实体映射到 mongo 中的集合,mongo 无模式的概念是否仍然有效。在这种情况下,我必须将成员变量映射到该集合的键。此外,如果 mongo 集合应该包含嵌套对象或对象数组,那么呢?

  2. Hibernate 最近(2015 年 1 月)为 NoSQL 数据库推出了 OGM

谁能帮我决定为 mongo 选择 OGM?

编辑:Kundera 不再维护。请使用替代品。


当然,ORM 易于使用,其中许多允许我们使用 SQL 或 JPQL 以类似 RDBMS 的方式进行查询,但 ORM 有其自身的局限性,是的,它们会增加一点也有开销(就性能而言)。这就是为什么首选使用本机 MongoDB Java 驱动程序的原因。

对于 NOSQL 个数据库,选择 ORM 完全取决于用例。 ORM 会将您的 table(Collection 在 MongoDB 的情况下)映射到实体对象。您补充说,您可以在实体中保留一个地图,这将使您的设计变得有点无模式。大多数时候我们的数据几乎是结构化的,因此可以使用 ORM。但是,如果您拥有完全通用且非结构化的数据,请选择 Java Driver。检查 Mongo's documentation.

此外,还有其他 stable ORM 工具,例如 Kundera, Spring Data。你应该探索它们。

PS:

  • 此答案适用于 ORM 与非 Hibernate ORM 上的本机驱动程序
  • 我是昆德拉开发者

我会推荐像 Tinkerpop, or if you need something a bit higher level and more similar to Hibernate then consider Ferma 这样的 OGM 框架。 Ferma 位于 Tinkerpop 之上,并使用注释来定义类似于 JPA 和 Hibernate 中的实体的 Java 类。不同之处在于 Ferma 和 Tinkerpop 是专门为图形数据库构建的,而不是试图将其塞入为传统关系数据库设计的框架中。

Ferma 和 Tinkerpop 的一个不错的特性是,虽然它们是为图形数据库构建的,但它们仍然可以在关系数据库上运行。 Tinkerpop 有多个驱动程序,使其能够在市场上几乎所有主要的图形数据库和关系数据库上运行。它还可以使用 Tinkerpop 中本机存在的内存驱动程序,因此根本不需要任何数据库支持。它是一个极其灵活的平台,在模块化和灵活的代码方面有很多好处。

Ferma 项目的描述供参考。

The Ferma project was originally created as an alternative to the TinkerPop2 Frames project. Which at the time lacked features needed by the community, and its performance was cripplingly slow. Today Ferma is a robust framework that takes on a role similar to an Object-relational Model (ORM) library for traditional databases. Ferma is often referred to as a Object-graph Model (OGM) library, and maps Java objects to elements in a graph such as a Vertex or an Edge. In short it allows a schema to be defined using java interfaces and classes which provides a level of abstraction for interacting with the underlying graph.

还有 Tinkerpop 项目的描述。

Apache TinkerPop™ is a graph computing framework for both graph databases (OLTP) and graph analytic systems (OLAP).

注:我是Ferma的作者之一