简体   繁体   English

使用 Spring-data mongodb 在 Java 中使用 ObjectId 作为字符串(手册参考)

[英]Using ObjectId as String in Java (Manual reference) with spring-data mongodb

In MongoDB documentation they suggest to use ObjecId for manual references.在 MongoDB 文档中,他们建议使用 ObjecId 进行手动引用。 please see https://docs.mongodb.com/manual/reference/database-references/#document-references请参阅https://docs.mongodb.com/manual/reference/database-references/#document-references

original_id = ObjectId()

db.places.insert({
    "_id": original_id,
    "name": "Broadway Center",
    "url": "bc.example.net"
})

db.people.insert({
    "name": "Erin",
    "places_id": original_id,
    "url":  "bc.example.net/Erin"
})

I'm using spring-data-mongodb and what I'm looking for is to have a People class defined like this:我正在使用spring-data-mongodb并且我正在寻找的是具有如下定义的People类:

@Document
public class People {

    private String name;
    @Reference // or any Annotation to convert an ObjectId to a String
    private String placesId; 
    private String url;
}

How to have a "places_id" as ObjectId in mongoDB but mapped to a String in our POJO ?如何将“places_id”作为 mongoDB 中的 ObjectId 但映射到我们的 POJO 中的字符串?

I was expecting to have an annotation like @Reference but it seems to not be implemented.我期待有一个像@Reference这样的注释,但它似乎没有实现。

I don't understand why we don't have this kind of annotation in spring-data-mongodb.我不明白为什么我们在 spring-data-mongodb 中没有这种注释。 I don't want to implement an explicit converter like suggested in spring documentation for all documents that use manual references.我不想为所有使用手动引用的文档实现像spring 文档中建议的显式转换器。 Maybe it's not the right approach.也许这不是正确的方法。

Did I miss something ?我错过了什么吗?

UPDATE :更新:

I like the idea to have a POJO using only String instead of ObjectId.我喜欢让 POJO 只使用 String 而不是 ObjectId 的想法。 Let's say I've got a class Place like this :假设我有一个这样的类Place

@Document
public class Place {
  @Id
  private String id;
  private String name;
}

place.getId() will be a String but people.getPlaceId() will be an ObjectId. place.getId()将是一个字符串,而people.getPlaceId()将是一个 ObjectId。 I want to avoid this unnecessary mapping.我想避免这种不必要的映射。

If you want to make a real reference to an other object in your database, use the @DBRef annotation which is provided by Spring Data .如果要真正引用数据库中的其他对象,请使用Spring Data提供的@DBRef注释。

Your updated code could look like the following:您更新后的代码可能如下所示:

@Document
public class People {

    private String name;

    @DBRef
    private Place place; 
    private String url;
}

Spring Data will then automatically map a Place object to your People object.然后 Spring Data 会自动Place对象映射到People对象。 Internally this is done with a reference to the unique ObjectId .在内部,这是通过引用唯一的ObjectId Try this code and have a look at your mongo database.试试这个代码,看看你的 mongo 数据库。

For more information have a look at: MongoDb with java foreign key有关更多信息,请查看: 带有 java 外键的 MongoDb

Why don't you leave the field as ObjectId?为什么不将该字段保留为 ObjectId?

@Document
public class People {

    private String name;
    private ObjectId placesId; 
    private String url;
}

If you want to query by this field you can do this:如果要按此字段查询,可以执行以下操作:

  • For lists对于列表

    List<String> ids // the ids as strings List<ObjectId> objIds = ids .stream() .map(i -> new ObjectId(i)) .collect(Collectors.toList());
  • For single String对于单个字符串

    String id // single id ObjectId objId = new ObjectId(id);

The solution would be:解决方案是:

import org.springframework.data.mongodb.core.mapping.Field;
import org.springframework.data.mongodb.core.mapping.FieldType;

public class People {

    @Field(targetType = FieldType.OBJECT_ID)
    private String placesId; 
}

This will map POJO string to ObjectId in MongoDB.这会将 POJO 字符串映射到 MongoDB 中的 ObjectId。

I have a solution very simple:我有一个非常简单的解决方案:

@JsonSerialize(using= ToStringSerializer.class)
private ObjectId brandId;
...

put that on the attribute that is Object Id, and the ObjectId gets and inserts like string把它放在对象 ID 的属性上,对象 ID 像字符串一样获取和插入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM