在Google App Engine上使用JPA加载子实体

时间:2022-09-11 16:10:23

I am not able to get child entities to load once they are persisted on Google App Engine. I am certain that they are saving because I can see them in the datastore. For example if I have the following two entities.

我们无法在Google App Engine上保留子实体后加载它们。我确信他们正在保存,因为我可以在数据存储区中看到它们。例如,如果我有以下两个实体。

public class Parent implements Serializable{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
    private String key;
    @OneToMany(cascade=CascadeType.ALL)
    private List<Child> children = new ArrayList<Child>();

    //getters and setters
}

public class Child implements Serializable{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
    private String key;
    private String name;
    @ManyToOne
    private Parent parent;
    //getters and setters
}

I can save the parent and a child just fine using the following:

我可以使用以下内容保存父母和孩子:

Parent parent = new Parent();
Child child = new Child();
child.setName("Child Object");
parent.getChildren().add(child);
em.persist(parent);

However when I try to load the parent and then try to access the children (I know GAE lazy loads) I do not get the child records.

但是,当我尝试加载父项然后尝试访问子项(我知道GAE延迟加载)时,我没有得到子记录。

//parent already successfully loaded
parent.getChildren.size(); // this returns 0

I've looked at tutorial after tutorial and nothing has worked so far. I'm using version 1.3.3.1 of the SDK. I've seen the problem mentioned on various blogs and even the App Engine forums but the answer is always JDO related. Am I doing something wrong or has anyone else had this problem and solved it for JPA?

我在教程之后看过教程,到目前为止还没有任何工作。我正在使用SDK的1.3.3.1版。我已经看到了各种博客甚至App Engine论坛上提到的问题,但答案总是与JDO相关。我做错了什么或有其他人遇到过这个问题并为JPA解决了吗?

3 个解决方案

#1


2  

Don't close the entity manager and invoke the child property, this is not empty. The lazy find the childs only if the entity manager is open.

不要关闭实体管理器并调用子属性,这不是空的。只有在实体管理器处于打开状态时,懒惰才会找到孩子。

#2


1  

I have had the same issue before, as much as this seems as an odd strange answer, but nothing worked for me, until I changed the

我以前遇到过同样的问题,尽管这似乎是一个奇怪的奇怪答案,但对我来说没有任何作用,直到我改变了

@OneToMany(mappedBy="parent", cascade=CascadeType.ALL)

to

@OneToMany(mappedBy="parent", cascade=CascadeType.PERSIST)

Give it a shot and let me know

试一试,让我知道

#3


0  

Try adding mappedBy on the @OneToMany annotation like below:

尝试在@OneToMany注释上添加mappedBy,如下所示:

@OneToMany(mappedBy="parent", cascade=CascadeType.ALL)
private List<Child> children = new ArrayList<Child>();

Also, you should add below to the @ManyToOne annotation:

另外,您应该在@ManyToOne注释中添加以下内容:

@ManyToOne(fetch = FetchType.LAZY)

Also, I find it useful to use the Google Key and KeyFactory classes because then you can explicitly set the parent when creating the child key.

此外,我发现使用Google Key和KeyFactory类很有用,因为您可以在创建子键时显式设置父级。

this.key = KeyFactory.createKey(parentKey, getClass().getSimpleName(), name);

#1


2  

Don't close the entity manager and invoke the child property, this is not empty. The lazy find the childs only if the entity manager is open.

不要关闭实体管理器并调用子属性,这不是空的。只有在实体管理器处于打开状态时,懒惰才会找到孩子。

#2


1  

I have had the same issue before, as much as this seems as an odd strange answer, but nothing worked for me, until I changed the

我以前遇到过同样的问题,尽管这似乎是一个奇怪的奇怪答案,但对我来说没有任何作用,直到我改变了

@OneToMany(mappedBy="parent", cascade=CascadeType.ALL)

to

@OneToMany(mappedBy="parent", cascade=CascadeType.PERSIST)

Give it a shot and let me know

试一试,让我知道

#3


0  

Try adding mappedBy on the @OneToMany annotation like below:

尝试在@OneToMany注释上添加mappedBy,如下所示:

@OneToMany(mappedBy="parent", cascade=CascadeType.ALL)
private List<Child> children = new ArrayList<Child>();

Also, you should add below to the @ManyToOne annotation:

另外,您应该在@ManyToOne注释中添加以下内容:

@ManyToOne(fetch = FetchType.LAZY)

Also, I find it useful to use the Google Key and KeyFactory classes because then you can explicitly set the parent when creating the child key.

此外,我发现使用Google Key和KeyFactory类很有用,因为您可以在创建子键时显式设置父级。

this.key = KeyFactory.createKey(parentKey, getClass().getSimpleName(), name);