通过密钥查询数据存储库时空了 [英] Got Null when querying datastore by key
问题描述
我有两种模式,书和章,采用一对多的关系。我手动创建Book和Chapter的键。坚持下来,我创建一个书对象,然后添加一个章节的实例,然后坚持书。这很好,因为我在数据存储中看到它们。现在,当我试图通过键从数据存储中获取一章时,我得到一个空对象。
以下是键在数据存储中的外观:
在Book:name / id = 123章节= [Book(123)/ Chapter(abc)]
章节:name / id = abc
我创建了我的密钥,用于创建和提取对象,使用
Key key = KeyFactory.createKey(Chapter.class.getSimpleName(),chapterId);
我的代码是这样的:
Key key = KeyFactory.createKey(Chapter.class.getSimpleName(),chapterId);
章节chp = mgr.find(Chapter.class,key); // chp始终为空(在调试模式下也是)
更新:
我在Book上尝试了同样的事情,它工作正常。所以问题在于章节。也许这是因为我通过Book保存了Chapter(但是我在上面提到的数据存储中都看到了)。
所以问题是:有没有一种方法可以独立检索章节通过它的关键字),如果是的话,一个代码片段请。
更新源代码:
@Entity
public class Book实现java.io.Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
私钥键;
@OneToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
private List< Chapter>章节= new ArrayList< Chapter>();
公共列表< Chapter> getChapters(){
返回章节;
}
public void setChapters(List&Chapter&Chapters){
this.Chapters = Chapters;
public Book(long num,List< Chapter"章节){
super();
Key key = KeyFactory.createKey(Book.class.getSimpleName(),num);
this.key = key;
this.Chapters =章节;
}
public Book(long num){
super();
Key key = KeyFactory.createKey(Book.class.getSimpleName(),num);
this.key = key;
$ b $ public Book(){
}
public key getKey(){
return key;
}
public void setKey(Key key){
this.key = key;
}
}
@实体
公共类实现java.io.Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
私钥键;
私人字符串内容;
$ b $ public Chapter(String ChapterId,String content){
super();
Key key = KeyFactory.createKey(Chapter.class.getSimpleName(),ChapterId);
this.key = key;
this.content = content;
}
公钥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);
章节chp =新章节(章节标识,内容);
bk.getChapters()。add(chp);
bookDao.put(bk);
mgr.persist(bk);
我没有留下任何投票,但是您应该提供更多的周边代码。在您提供的代码中,大多数内容看起来都很好,但如果您在事务中创建书籍/章节(未显示),则该章节可能会将该书籍指定为父级,而您在手动指定时没有指定父级创建章节键。
I have two models, Book and Chapter, in a one-to-many relationship. I manually create the keys for both Book and Chapter. To persist, I create a book object then add an instance of chapter to it and then persist book. This works fine, as I see them in the datastore. Now when I try to fetch a chapter from the datastore by key, I get a null object.
Here is how the keys look in the datastore:
Under Book: name/id = 123 chapters = [Book(123)/Chapter("abc")]
Under Chapter: name/id = abc
I created my keys, both for creating and fetching objects, using
Key key = KeyFactory.createKey(Chapter.class.getSimpleName(), chapterId);
My fetching code is this:
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)
UPDATE:
I try the same thing on Book and it works fine. So the problem is with Chapter. Perhaps it's because I saved Chapter through Book (but I see both in the datastore as mentioned above).
So the question is: Is there a way to retrieve chapter independently (by its key), if yes a code snippet please.
UPDATE source code:
@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;
}
}
Code for adding:
Book bk = new Book(num);
Chapter chp = new Chapter(ChapterId, content);
bk.getChapters().add(chp);
bookDao.put(bk);
mgr.persist(bk);
I did not leave any vote, but you should provide more of the surrounding code. Things mostly look fine in the code you gave, but if you created the book/chapter in a transaction (which isn't shown), the chapter may have the book specified as a parent, and you didn't specify a parent when manually creating the chapter key.
这篇关于通过密钥查询数据存储库时空了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!