如何为Spring数据中的类配置MongoDb集合名称 [英] How To Configure MongoDb Collection Name For a Class in Spring Data
问题描述
我的MongoDB数据库中有一个名为 Products
的集合,在我的Java中由接口 IProductPrice
表示码。以下存储库声明导致Spring Date查看集合 db.collection:Intelliprice.iProductPrice
。
I have a collection called Products
in my MongoDB database, which is represented by the interface IProductPrice
in my Java code. The following repository declaration causes Spring Date to look to the collection db.collection: Intelliprice.iProductPrice
.
我想要它配置它来查看 db.collection:Intelliprice.Products
使用外部配置而不是放一个 @Collection(..)$
IProductPrice
上的c $ c>注释。这可能吗?我该怎么办?
I want it to configure it to look in db.collection: Intelliprice.Products
using an external configuration rather than putting an @Collection(..)
annotation on IProductPrice
. Is this possible? How can I do this?
public interface ProductsRepository extends
MongoRepository<IProductPrice, String> {
}
推荐答案
唯一可行的方法目前实现这一点是通过使用集合
属性使用 @Document
注释您的域类来定义集合实例的名称这个班级应该坚持到。
The only way you can currently achieve this is by annotating your domain class with @Document
using the collection
property to define the name of the collection instances of this class shall be persisted to.
然而,有一个 JIRA问题开放,建议添加一个可插入的命名策略来配置类,集合和属性名称以更全局的方式处理。请随意评论您的用例并进行投票。
However, there's a JIRA issue open that suggests adding a pluggable naming strategy to configure the ways class, collection and property names are handled in a more global way. Feel free to comment your use case and vote it up.
这篇关于如何为Spring数据中的类配置MongoDb集合名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!