Search code examples
mongodbmorphia

Morphia (MongoDB) Datastore "get" returns null


So I started working with Morphia and I'm encountering a weird problem.

Here's my entity 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;
    }
}

Here's my little test:

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();

When I use

datastore.get(MovieDetails.class, id)

I get null

When I use:

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

I do see my movie in the DB, with the Id used in the get function.

Tried the id in many variations... toString(), ObjectId(id), Key (The value returned from the save result).

The Id in the DB (viewed with Mongo Explorer) does show as something which isn't string (blue colored), suspicious: Mongo Explorer item picture

Any ideas?

Edit: * the Id is indeed a string, the cast works and it was verified using watch + instanceof Edit 2: * Somehow the cast from ObjectId to String passed and the Id wasnt really a String.


Solution

  • After Nic Cottrell (Thanks!) answer I've had a new perspective of the problem. As my @Id field was not assigned by me it was automaticaly assigned in the DB as ObjectId. As I still want to use String I've simply assigned the @Id field on object creation.

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

    found the solution on: MongoDB / Morphia saves technical id as ObjectId although it's a String in Java