Morphia(MongoDB)数据存储"得到"返回null

时间:2017-08-09 20:12:31

标签: mongodb morphia

所以我开始与Morphia合作,我遇到了一个奇怪的问题。

这是我的实体类

@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实际上并不是String。

2 个答案:

答案 0 :(得分:0)

我会将您的I字段从String更改为BSON ObjectId字段,MongoDB将在保存时自动分配。如果您以ObjectId作为参数进行调用,它应该可以工作。 Mongo强烈建议使用ObjectId作为您的ID字段。

我猜Morphia正在尝试将ObjectId编组到String Id字段中,并且某处有一个小错误。我会尝试拨打datastore.get(Example.class, new ObjectId(id))。

答案 1 :(得分:0)

Nic Cottrell 之后(谢谢!)回答我对这个问题有了新的看法。 由于我的@Id字段未由我分配,因此它在DB中自动分配为ObjectId。 因为我仍然想使用字符串,我只是在对象创建时分配了@Id字段。

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

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