所以我開始與Morphia一起工作,遇到一個奇怪的問題。Morphia(MongoDB)Datastore「get」返回null
這裏是我的實體類
@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)
我得到空
當我使用:
datastore.createQuery(MovieDetails.class).asList();
我確實在數據庫中看到了我的影片,並在get函數中使用了Id。
嘗試了很多變化中的id ... toString(),ObjectId(id),Key(從保存結果返回的值)。
中的id DB(與蒙戈Explorer查看)確實顯示的東西是不是字符串(藍色),可疑: Mongo Explorer item picture
任何想法?
編輯: *的標識確實是一個字符串,演員的作品,用手錶+的instanceof 編輯2可以證實: *不知怎的,劇組從的ObjectId到字符串傳遞和ID wasnt真串。
檢查一下這個回報,而不是將其轉換爲String,使該類型的對象。 datastore.save(movie).getId() 它看起來像是在Java講中返回null ClassCastException。是否有可以查看或調試的日誌記錄可啓用? –
@CalvinTaylor當添加id來觀看(不投射)結果是:「598b70468a141e56ac9e9203」和標記爲char []的內部值。 id instanceof String也返回「True」。 – Nimig
@CalvinTaylor關於你的編輯:getId()不返回null和/或得到一個ClassCastException,返回的id匹配數據庫中的一個。在重新組合它們之前,我已經分離了代碼步驟,只有在驗證了該ID已正確返回後,db才被正確更新,並且該ID是一個字符串。 – Nimig