2017-08-09 114 views
0

所以我開始與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真串。

+0

檢查一下這個回報,而不是將其轉換爲String,使該類型的對象。 datastore.save(movie).getId() 它看起來像是在Java講中返回null ClassCastException。是否有可以查看或調試的日誌記錄可啓用? –

+0

@CalvinTaylor當添加id來觀看(不投射)結果是:「598b70468a141e56ac9e9203」和標記爲char []的內部值。 id instanceof String也返回「True」。 – Nimig

+0

@CalvinTaylor關於你的編輯:getId()不返回null和/或得到一個ClassCastException,返回的id匹配數據庫中的一個。在重新組合它們之前,我已經分離了代碼步驟,只有在驗證了該ID已正確返回後,db才被正確更新,並且該ID是一個字符串。 – Nimig

回答

0

我會將你的I字段從字符串更改爲一個BSON ObjectId字段,MongoDB將在保存時自動分配這個字段。如果你使用ObjectId作爲參數進行調用,它應該可以工作。強烈建議在Mongo中使用ObjectId作爲您的ID字段。

我在猜測Morphia正試圖將一個ObjectId編組到你的字符串ID字段中,並且在某處存在一個小錯誤。我會嘗試撥打datastore.get(Example.class, new ObjectId(id))。

+0

我之前沒有使用新的ObjectId(id),它沒有工作,但現在它確實:/ 你的解決方案的工作原理! 顯然Morphia創建了_id作爲一個ObjectId事件,儘管我的String被標記爲Id!無論是這個,還是他簡單地忽略了這種類型......另一種方式確實起作用,Morphia知道把數據庫中的Id設置爲我的字符串。 – Nimig