Morphia (MongoDB) 数据存储 "get" returns 空

Morphia (MongoDB) Datastore "get" returns null

所以我开始使用 Morphia,但遇到了一个奇怪的问题。

这是我的实体class

@Entity("movies")
@Indexes(@Index(value = "Name", fields = @Field("Name")))
@Converters(LocalDateConverter.class)
public class MovieDetails implements Serializable
{
    @Id
    public String Id;
    public String Name;
    public String Description;
    public String ImageName;
    public LocalDate ReleaseDate;
    public String Director;
    public int Duration;
    public String Genres;
    public String Actors;

    public MovieDetails()
    {

    }

    public MovieDetails(String id, String name, String description, String imageName, String director, String actors, LocalDate releaseDate, String genres, int duration)
    {
        this (name, description, imageName, director, actors, releaseDate, genres, duration);
        Id = id;
    }

    public MovieDetails(String name, String description, String imageName, String director, String actors, LocalDate releaseDate, String genres, int duration)
    {
        Name = name;
        Description = description;
        ImageName = imageName;
        Director = director;
        Actors = actors;
        ReleaseDate = releaseDate;
        Genres = genres;
        Duration = duration;
    }
}

这是我的小测试:

final Morphia morphia = new Morphia();

// tell Morphia where to find your classes
// can be called multiple times with different packages or classes
morphia.mapPackage("nimrodpasha.cinema.objects");

// create the Datastore connecting to the default port on the local host
final Datastore datastore =
        morphia.createDatastore(SingleMongoClient.getInstance().getClient(),
                                Constants.DB.TICKET_DATABASE);
datastore.ensureIndexes();

    //region new movie
    MovieDetails movie = new MovieDetails("The Mask", "Stanley Ipkiss (Jim Carrey) is a bank clerk that is an incredibly nice man. Unfortunately," +
            " he is too nice for his own good and is a pushover when it comes to confrontations. After one of the worst days of his life, he finds a mask that depicts Loki, " +
            "the Norse night god of mischief. Now, when he puts it on, he becomes his inner, self: a cartoon romantic wild man. However, a small time crime boss, Dorian Tyrel (Peter Greene), " +
            "comes across this character dubbed The Mask by the media. After Ipkiss's alter ego indirectly kills his friend in crime," +
            " Tyrel now wants this green-faced goon destroyed.",
                                          "MASK.jpg", "Chuck Russell", "Jim Carrey as Stanley Ipkiss/The Mask,Cameron Diaz as Tina Carlyle,Amy Yasbeck as Peggy Brandt,Joely Fisher as Maggie", new LocalDate(1994, 2, 1), "Action,Comedy,CrimeAction,Family,Fantasy", 88);
    //endregion

// Clearing the db first
datastore.delete(datastore.createQuery(MovieDetails.class));

// Saving a new entity and getting the result saved id
String id = (String) datastore.save(movie).getId();

// This returns as null
MovieDetails movieRetrieved = datastore.get(MovieDetails.class, id);

// This returns with one item
List<MovieDetails> allMovies = datastore.createQuery(MovieDetails.class).asList();

当我使用

datastore.get(MovieDetails.class, id)

我得到 null

当我使用时:

datastore.createQuery(MovieDetails.class).asList();

我确实在数据库中看到了我的电影,在 get 函数中使用了 Id。

尝试了多种形式的 id...toString()、ObjectId(id)、Key(保存结果返回的值)。

数据库中的 ID(使用 Mongo Explorer 查看)确实显示为不是字符串的东西(蓝色),可疑: Mongo Explorer item picture

有什么想法吗?

编辑: * Id 确实是一个字符串,转换有效并且使用 watch + instanceof 进行了验证 编辑 2: * 以某种方式从 ObjectId 到 String 的转换通过了,而 Id 并不是真正的字符串。

我会将您的 I'd 字段从 String 更改为 BSON ObjectId 字段,MongoDB 将在保存时自动分配该字段。如果您随后使用 ObjectId 作为参数进行 get 调用,它应该可以工作。 Mongo.

强烈建议使用 ObjectId 作为您的 ID 字段

我猜测 Morphia 正试图将 ObjectId 编组到您的 String Id 字段中,并且某处存在一个小错误。我会尝试调用 datastore.get(Example.class, new ObjectId(id)).

Nic Cottrell(谢谢!)回答后,我对问题有了新的看法。 由于我的@Id 字段不是我分配的,它在数据库中自动分配为 ObjectId。 因为我仍然想使用 String,所以我只是在创建对象时分配了 @Id 字段。

Id = ObjectId.get().toString();

找到解决方案: MongoDB / Morphia saves technical id as ObjectId although it's a String in Java