簡體   English   中英

按鍵查詢數據存儲區時獲得Null

[英]Got Null when querying datastore by key

我有兩個模型,Book和Chapter,以一對多的關系。 我手動創建Book和Chapter的鍵。 為了堅持,我創建了一個書籍對象,然后向其添加章節實例,然后堅持書。 這很好,因為我在數據存儲區中看到它們。 現在,當我嘗試按鍵從數據存儲區中獲取章節時,我得到一個空對象。

以下是數據存儲區中鍵的外觀:

Under Book: name/id = 123    chapters = [Book(123)/Chapter("abc")]
Under Chapter: name/id = abc

我創建了我的密鑰,用於創建和獲取對象,使用

Key key = KeyFactory.createKey(Chapter.class.getSimpleName(), chapterId);

我的提取代碼是這樣的:

Key key = KeyFactory.createKey(Chapter.class.getSimpleName(), chapterId);
Chapter chp = mgr.find(Chapter.class, key);//chp is always null (yes in debug mode as well)

更新:

我在Book上嘗試相同的東西,它工作正常。 所以問題在於章節。 也許是因為我通過Book保存了章節(但我在上面提到的數據存儲區中都看到了)。

所以問題是:有沒有辦法獨立檢索章節(通過其鍵),如果是的話,請提供代碼片段。

更新源代碼:

@Entity
public class Book implements java.io.Serializable{
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Key key;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private List<Chapter> Chapters = new ArrayList<Chapter>();

    public List<Chapter> getChapters() {
        return Chapters;
    }

    public void setChapters(List<Chapter> Chapters) {
        this.Chapters = Chapters;
    }

    public Book(long num, List<Chapter> Chapters) {
        super();
        Key key = KeyFactory.createKey(Book.class.getSimpleName(), num);
        this.key = key;
        this.Chapters = Chapters;
    }

    public Book(long num) {
        super();
        Key key = KeyFactory.createKey(Book.class.getSimpleName(), num);
        this.key = key;
    }

    public Book() {
    }

    public Key getKey() {
        return key;
    }

    public void setKey(Key key) {
        this.key = key;
    }

}



@Entity
public class Chapter implements java.io.Serializable{
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Key key;

    private String content;

    public Chapter(String ChapterId, String content) {
        super();
        Key key = KeyFactory.createKey(Chapter.class.getSimpleName(), ChapterId);
        this.key = key;
        this.content = content;

    }


    public Key getKey() {
        return key;
    }

    public void setKey(Key key) {
        this.key = key;
    }

    public String getContent() {
        return content;
    }

    public void set content(String content) {
        this.content = content;
    }


}

添加代碼:

Book bk = new Book(num);
        Chapter chp = new Chapter(ChapterId, content);
        bk.getChapters().add(chp);
        bookDao.put(bk);

mgr.persist(bk);

我沒有留下任何投票,但你應該提供更多的周圍代碼。 在您給出的代碼中,事情看起來很好,但如果您在事務中創建了書/章(未顯示),則該章可能將該書指定為父項,並且您在手動時未指定父項創建章節鍵。

您必須始終包含父實體密鑰以檢索子實體。 以下是如何創建包含父級的密鑰:

Key keyBook = KeyFactory.createKey(Book.class.getSimpleName(),
    BOOK_ID); 

Key keyChapter = KeyFactory.createKey(keyBook,
    Chapter.class.getSimpleName(), CHAPTER_ID); 

Chapter chp = mgr.find(Chapter.class, keyChapter);

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM