spring 使用 MongoDB 休眠

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2153195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:59:00  来源:igfitidea点击:

Hibernate with MongoDB

springhibernatemongodbhibernate-ogm

提问by adwk

I'm looking for resources showing how to integrate MongoDB with Hibernate (preferably from within spring) so that I can switch between a RDBMS and a NoSql alternative: does anyone have experience doing this?

我正在寻找展示如何将 MongoDB 与 Hibernate 集成(最好是在 spring 中)的资源,以便我可以在 RDBMS 和 NoSql 替代方案之间切换:有人有这样做的经验吗?

回答by Jared

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.

你不能轻易做到这一点。Hibernate 的重点是将 Java 对象映射到关系数据库。尽管 Hibernate 抽象了很多细节,您仍然需要了解关系数据库如何处理诸如外键和主键之类的事情,以及您运行的查询对性能的影响。MongoDB 需要一种完全不同的设计数据库的方式,专注于对象而不是列和表。虽然您可能能够为 MongoDB 创建一个 Hibernate 方言,但创建一个既适用于关系数据库又适用于 NoSql 数据库的设计会给您一个在两者上都运行不佳的设计。

回答by Hymantrades

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

Hibernate OGM怎么样?它为No-SQL数据库提供 JPA 。

回答by Israel Zalmanov

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

如果您使用 Spring MongoTemplate(类似于 HibernateTemplate),迁移会更容易。
它的功能之一是支持 JPA 注释(尽管我不确定到什么程度)。
查看更多:http: //www.springsource.org/spring-data/mongodb

You'll need the following:

您将需要以下内容:

  1. Add spring-data-mongodb JAR to your project (available in maven central).
  2. Add mongo-java-driver JAR to your project (available in maven central).
  3. Use the provided MongoTemplate class in a similar manner to HibernateTemplate. E.g.: mongoTemplate.findById(id, MyClass.class);
    mongoTemplate.insert(myObject);
  1. 将 spring-data-mongodb JAR 添加到您的项目中(在 maven central 中可用)。
  2. 将 mongo-java-driver JAR 添加到您的项目中(在 maven central 中可用)。
  3. 以类似于 HibernateTemplate 的方式使用提供的 MongoTemplate 类。例如: mongoTemplate.findById(id, MyClass.class);
    mongoTemplate.insert(myObject);

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

这是一个带有代码的具体示例:use-spring-and-hibernate-with-mongodb

回答by piyush shanu

If you are using Java then you can use Hibernate OGMit provides Java Persistence support for NoSQLdatabases.

如果您使用的是 Java,那么您可以使用Hibernate OGM,它为NoSQL数据库提供 Java 持久性支持。

For more details visit http://hibernate.org/ogm/

有关更多详细信息,请访问http://hibernate.org/ogm/

回答by thomash

There is also kundera, which uses JPA-annotations to read/write your object from/to a mongodb. If you ara familiar with hibernate, it should be quite straightformard to use.

还有kundera,它使用 JPA 注释从/向 mongodb 读/写您的对象。如果您熟悉 hibernate,那么使用它应该非常简单。

I recently tried Morphia, which takes the same approach, but with its own annotations. It works fine

我最近尝试了Morphia,它采用相同的方法,但有自己的注释。它工作正常

回答by Easility

For the sake of completeness, PlayORMalso supports MongoDB now. PlayORM is an object NoSQL mapping solution so you can write POJO's and let it deal with all the details of marshalling/unmarshalling to MongoDB. Visit its documentationhere

为了完整起见,PlayORM现在也支持 MongoDB。PlayORM 是一个对象 NoSQL 映射解决方案,因此您可以编写 POJO 并让它处理编组/解组到 MongoDB 的所有细节。在此处访问其文档

回答by wbartussek

May this blog helps: http://drorbr.blogspot.com/2010/02/migrating-springhibernate-application.htmlHere Dror Bereznitsky describes nicely how to integrate a sping/hibernate based solution with mongodb.

这篇博客可能会 有所帮助:http: //drorbr.blogspot.com/2010/02/migrating-springhibernate-application.html 这里 Dror Bereznitsky 很好地描述了如何将基于 sping/hibernate 的解决方案与 mongodb 集成。

回答by Faisal Abid

Well just to give you an example, I am doing somehting simmilar. In ColdFusion, Hibernate is integrated and in order to save your Hibernate Object, you hvae to do EntitySave(Obj). However what we have done is build the Orm object, and then use a mongoDB Coldfusion component and just save the object by going mongo.Save(obj,collectionName).

好吧,只是举个例子,我正在做类似的事情。在 ColdFusion 中,集成了 Hibernate,为了保存您的 Hibernate 对象,您必须执行 EntitySave(Obj)。然而,我们所做的是构建 Orm 对象,然后使用 mongoDB Coldfusion 组件并通过 mongo.Save(obj,collectionName) 保存对象。

回答by crnisamuraj

I think Hibernate provides desired functionality. Take a look at this, found on their official website: Mixing several NoSQL datastores in one application, e.g. use Neo4j for your friendship graph and MongoDB for your blog posts. Or mix NoSQL and relational databases.

我认为 Hibernate 提供了所需的功能。看看这个,在他们的官方网站上找到:在一个应用程序中混合多个 NoSQL 数据存储,例如使用 Neo4j 作为您的友谊图和 MongoDB 作为您的博客文章。或者混合使用 NoSQL 和关系数据库。

reference

参考