Upload JPA child objects to Google App Engine

I cannot get child entities to load as soon as they are saved in Google App Engine. I am sure that they save, because I can see them in the data warehouse. For example, if I have the following two objects.

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 child only with 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 that GAE is lazy loading), I do not get the child entries.

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

I looked at the textbook after the textbook, and so far nothing has worked. I am using version 1.3.3.1 of the SDK. I have seen the issue mentioned in various blogs and even App Engine forums, but the answer is always JDO related. Am I doing something wrong, or did someone else have this problem and solve it for JPA?

+4
source share
3 answers

Do not close the object manager or call the child property, this is not empty. Land finds children only if the entity manager is open.

+2
source

I had the same problem before as far as this seems like a weird strange answer, but nothing worked for me until I changed

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

to

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

Take a picture and let me know

+1
source

Try adding mappedBy to the @OneToMany annotation as shown below:

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

In addition, you should add below to the @ManyToOne annotation:

 @ManyToOne(fetch = FetchType.LAZY) 

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

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

Source: https://habr.com/ru/post/1309881/


All Articles