在Spring DATA中通过MongDB API使用DocumentDB时如何使用"id"? [英] How to use 'id' when using DocumentDB via MongDB API in Spring DATA?

查看:55
本文介绍了在Spring DATA中通过MongDB API使用DocumentDB时如何使用"id"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用了将DocumentDB与MongoDB API结合使用(因为Spring Data不支持DocumentDB). 我可以使用所有Spring Data方法,但不能使用自定义ID.

下面是我的实体:

public class S{

    private String id;

    /* other fields here */

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    /* getters and setters for other fields */
}

这是DAO:

public interface SDao extends MongoRepository<S, String> {

}

现在我可以在代码中的任何地方做

s = new S();
s.setId("some-id-here");

该记录已成功以自定义ID some-id-here作为String(而不是ObjectId)保留在数据库中,但此后它抛出ClassCastException表示Long无法转换为Integer.

使用BigInteger作为ID时也是如此.

如果我未设置自定义ID,即我评论id的设置,如下所示:

s = new S();
// s.setId("some-id-here");

不会引发异常,但会使用数据库本身提供的随机ID(ObjectcId)保留记录.

我想保存带有自定义ID的记录,以便我可以在需要时轻松地对其进行更新. 当前,如果我必须更新一条记录,则需要使用未映射到_id的键来检索它,然后更新它,然后从数据库中删除旧记录,然后保留更新后的记录,我觉得这是绝对的效率低下,因为我无法使用_id.

我的问题是为什么我会得到ClassCastException,也提到了 LongInteger

的转换

DocumentDB是否在内部进行某些转换,从而引发此异常.如果是,该如何解决?这是一个错误吗?

解决方案

一种替代方法是让DocumentDB/ MongoDB默认为您创建这些IDs.在您的课程中,您可以拥有另一个可以用作自然ID的字段,并在该字段上创建唯一索引以进行提取优化. 请参考 https://docs.mongodb.com/manual/reference/方法/db.collection.createIndex/用于索引.

I have used both the ways of mapping _id as described in the Spring Docs here.

  • using @Id annotation
  • having a field with name id without any annotation

in my previous project where we used MongoDB as database and Spring Data for DAO operations. It worked without any problem for both String a well as for BigInteger.

Now we are using DocumentDB with MongoDB API(as Spring Data does not support DocumentDB). I am able to use all the Spring Data methods, but I am not able to use custom id.

Below is my entity:

public class S{

    private String id;

    /* other fields here */

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    /* getters and setters for other fields */
}

This is the DAO:

public interface SDao extends MongoRepository<S, String> {

}

Now if anywhere in my code I do:

s = new S();
s.setId("some-id-here");

The record gets successfully persisted in the DB with custom id some-id-here as String (not ObjectId), but after that it throws ClassCastException saying Long cannot be converted to Integer.

Same is the case when using BigInteger for id.

If I am not setting the custom id, i.e. I comment the setting of id as below:

s = new S();
// s.setId("some-id-here");

no exception is being thrown, but the record is being persisted with a random id provided by database itself as ObjectcId.

I want to save the record with custom id, so that I can easily update it when needed. Currently if I have to update a record, I need to retrieve it using a key which is not mapped to _id and then update it and then delete the old record from the DB and then persist the updated one, which I feel is absolutely inefficient as I am not able to make use of _id.

My question is why am I getting ClassCastException, that too mentioning Conversion of Long to Integer

Is DocumentDB internally doing some conversion which is throwing this exception. If yes, how to tackle it? Is this a bug?

解决方案

One alternative could be to let DocumentDB/ MongoDB create those IDs for you by default. In your class you can have another field which can serve as natural ID and create a unique index on that field for fetch optimization. Refer https://docs.mongodb.com/manual/reference/method/db.collection.createIndex/ for indexes.

这篇关于在Spring DATA中通过MongDB API使用DocumentDB时如何使用"id"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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