如何为 Spring Data 中的类配置 MongoDb 集合名称

How To Configure MongoDb Collection Name For a Class in Spring Data(如何为 Spring Data 中的类配置 MongoDb 集合名称)

本文介绍了如何为 Spring Data 中的类配置 MongoDb 集合名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 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.这可能吗?我该怎么做?

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> {
}

推荐答案

目前唯一的方法是使用 collection@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 Data 中的类配置 MongoDb 集合名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何为 Spring Data 中的类配置 MongoDb 集合名称

基础教程推荐