Java Spring MongoDB ORM使用deleteById(id)时未删除文件。

huangapple go评论86阅读模式
英文:

Java Spring MongoDB ORM not deleting file with deleteById(id)

问题

我试图在数据库中删除一个条目

在 MongoDB 中,该条目如下所示:

_id
"11c4ee15-0048-4cc6-8a62-38e40389f2a8"
title
"删除我"
content
"这是第一课"

我正在使用这个存储库:

public interface LessonRepository extends MongoRepository<Lesson, UUID> {
}

为什么这个调用在没有找到 id 的情况下失败:

public void deleteLesson(UUID id) {
    lessonRepository.deleteById(id);
}

模型看起来是这样的:

@Data
public class Lesson {
    @Id
    private String id;
    private String title;
    private String content;
}

我猜想 id 没有正确定义。但将其定义为 UUID 会得到一个二进制对象?为什么它看起来不像一个 ObjectId?

英文:

I am trying to delete an entry in the database

The entry looks like this in the MongoDB:

> _id
> "11c4ee15-0048-4cc6-8a62-38e40389f2a8"
> title
> "DELETE ME"
> content
> "This is Lesson number 1"

I am using this repository:

public interface LessonRepository extends MongoRepository&lt;Lesson, UUID&gt; {
}

Why does this call fail without finding the id:


    public void deleteLesson(UUID id) {
        lessonRepository.deleteById(id);
    }

The model looks like this:

@Data
public class Lesson {
    @Id
    private String id;
    private String title;
    private String content;
}

My guess it that the id is not defined correctly. But defining it as a UUID gives me a binary object? Why doesn't it look like an ObjectId?

答案1

得分: 0

你尝试过修改你的存储库为:

public interface LessonRepository extends MongoRepository<Lesson, String> {
}
英文:

Have you tried modifying your repository to:

public interface LessonRepository extends MongoRepository&lt;Lesson, String&gt; {
}

huangapple
  • 本文由 发表于 2023年6月26日 03:16:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76552060.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定