简体   繁体   English

MongoDB / Morphia将技术ID保存为ObjectId,尽管它是Java中的String

[英]MongoDB / Morphia saves technical id as ObjectId although it's a String in Java

I've got two kinds of documents in my MongoDB: clients and codes. 我的MongoDB中有两种文档:客户端和代码。 Each code refers to one client. 每个代码都指一个客户端。 Clients have to be stored explicitly by an administrator, therefore I have to store them separate and cannot put them into a code document. 客户端必须由管理员显式存储,因此我必须将它们分开存储,并且不能将它们放入代码文档中。

code -> client

Now MongoDB / Morphia saves technical ids of clients as ObjectId, whereas codes refer to clients with a technical id of type String. 现在,MongoDB / Morphia将客户端的技术ID保存为ObjectId,而代码则引用技术ID为String类型的客户端。 I am able to search a code by a given client id, but at the runtime I'll get a error message, because Morphia cannot inject the client. 我能够通过给定的客户端ID搜索代码,但在运行时我会收到错误消息,因为Morphia无法注入客户端。 I assume it's because of the different id types. 我认为这是因为不同的id类型。

code { client.$id: String }
client { _id: ObjectId }

Any ideas how to fix this? 任何想法如何解决这一问题?

Exception 例外

com.google.code.morphia.mapping.MappingException: The reference({ "$ref" : "clients", "$id" : "123456789abcdef" }) could not be fetched for org.example.Code.client com.google.code.morphia.mapping.MappingException:无法为org.example.Code.client获取引用({“$ ref”:“clients”,“$ id”:“123456789abcdef”})

On the internet I found that exception message. 在互联网上我发现了异常消息。 It was suggested to use ObjectId instead of String in the model, but I have the requirement to use String. 有人建议在模型中使用ObjectId而不是String,但我需要使用String。 This is not my own project. 这不是我自己的项目。

Entities: 实体:

@Entity("codes")
public class Code implements Comparable<Code> {
    @Id
    private String id;

    @Reference
    private Client client;

    [...]
}

@Entity("clients")
public class Client {
    @Id
    private String id;
}

Storing: 储存:

To store the objects I use com.google.code.morphia.dao.DAO.save(T entity) . 要存储对象,我使用com.google.code.morphia.dao.DAO.save(T entity)

Search: 搜索:

public class CodeRepository extends BasicDAO<Code, String> {
    [... constructor ...]

    @Override
    public Code findByCode(String type, String clientId, String code) {
        return findOne(createQuery()
                .field("type")
                .equal(type)
                .field("value")
                .equal(code)
                .field("client")
                .equal(new Key<Client>(Client.class, clientId)));
    }
}

not sure if this is solved yet. 不确定这是否已经解决。 I had the same problem. 我有同样的问题。 The solution for me was to set the id myself. 我的解决方案是自己设置id。

@Id
private String id = new ObjectId().toString();

Now you can treat the id field like any other string field. 现在,您可以将id字段视为任何其他字符串字段。

Hope this helps. 希望这可以帮助。

I did it slightly differently so i could use the id as path param in REST requests. 我的做法略有不同,所以我可以在REST请求中使用id作为路径参数。

@Id
private String id = new ObjectId().toHexString();

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

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