ObjectId 字段到 String 的正确映射 [英] Propper mapping of an ObjectId fields to String

查看:49
本文介绍了ObjectId 字段到 String 的正确映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对我的 RDBMS 世界和 MongoDB 的神秘海洋进行一些探索.我正在使用 Spring Data 来帮助我进行冒险.我需要在两个集合中的文档之间创建一个手动引用(我已经读过 DBRefs 很昂贵),我的 pojos 是这样的:

I'm doing a little bit of exploration out of my RDBMS world and into the mysterious seas of MongoDB. I'm using Spring Data to help me in my adventure. I need to create a manual reference between documents in two collections (I've read that DBRefs are expensive), my pojos are like this:

public class User {
    @Id
    private String id;
    private String name;
    private String password;
    ...
}



public class Game {
    @Id
    private String id;
    private String name;
    private String platform;
    private String userID;
    ...
}

userID 是现有用户的 id,它作为 ObjectId 存储在数据库中.检索Game文档时,userID字段正确映射到String值,但是我想让spring-data在持久化时做逆映射(String to ObjectId),不知道怎么做.

The userID is the id of an existing user, and it's stored as an ObjectId in the database. When I retrieve a Game document, the userID field is correctly mapped to a String value, but I want spring-data to do the inverse mapping when persisting (String to ObjectId), and I dont know how to do it.

有人能指出我正确的方向吗?我应该放弃这种方法并采用直接将此字段存储为字符串的简单解决方案吗?

Can somebody point me in the right direction with this, please? Should I give up on this approach and go with the easy solution storing this field directly as a String?

非常感谢!

推荐答案

默认情况下,在使用 spring-data-mongodb 时,Spring 使用 MappingMongoConverter 将域对象转换为 |来自文档.

By default when using spring-data-mongodb, Spring uses a MappingMongoConverter to convert domain objects to | from document.

id 属性的默认转换器策略如下:

The default converter policy for id property is as follows:

以下概述了将映射到_id"文档字段的字段:

The following outlines what field will be mapped to the '_id' document field:

使用@Id (org.springframework.data.annotation.Id) 注释的字段将映射到_id"字段.没有注释但名为 id 的字段将映射到_id"字段.下面概述了对映射到 _id 文档字段的属性进行的类型转换(如果有).

A field annotated with @Id (org.springframework.data.annotation.Id) will be mapped to the '_id' field. A field without an annotation but named id will be mapped to the '_id' field. The following outlines what type conversion, if any, will be done on the property mapped to the _id document field.

如果名为 'id' 的字段在 Java 类中被声明为 String 或 BigInteger,如果可能,它将被转换为并存储为 ObjectId.ObjectId 作为字段类型也是有效的.

If a field named 'id' is declared as a String or BigInteger in the Java class it will be converted to and stored as an ObjectId if possible. ObjectId as a field type is also valid.

默认情况下,不会对任何其他标准属性进行此转换.因此,如果您希望将 userID 字段保存为 ObjectId,只需更改字段类型:private ObjectId userID;

This conversion is not done by default to any other standard property. So if you want userID field to be saved as ObjectId simply change the field type: private ObjectId userID;

如果您想将 Userid 属性保存为 MongoDB 中的字符串,并非相反的情况不那么简单,您必须提供自己的映射转换器覆盖 writeInternal 方法(顾名思义,它暗示您不应该)或在转换过程中进一步覆盖其他一些 Spring MongoDB 管道 bean

Not that the oposite case is less simple if you want to save the id property of User as String in MongoDB, you will have to supply your own mapping converter which overrides the writeInternal method (by it's name it implies you shouldn't) or override some other Spring MongoDB plumbing bean further down the conversion process

这篇关于ObjectId 字段到 String 的正确映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆