google cloud datastore - trouble getting one-to-many relationship to work on app-engine and JPA -


i have one-to-many relationship between book , chapter. able create book object , add chapters (i in datastore , see creation). however, after fetch book if try loop through chapters, error

javax.jdo.jdodetachedfieldaccessexception: have attempted access field "chapters" yet field not detached when detached object. either dont access field, or detach when detaching object. 

after research, found blog says place @basic on getchapters method. when that, new error:

java.lang.illegalstateexception: field "book.chapters" contains persistable object isnt persistent, field doesnt allow cascade-persist! 

i have been trying sorts of things, latest of models is

@entity public class account {     @id     @generatedvalue(strategy = generationtype.identity)     private key key;      @onetomany(mappedby = "book", cascade = cascadetype.all)     private list<chapter> chapters = new arraylist<chapter>(); }   @entity public class chapter {     @id     @generatedvalue(strategy = generationtype.identity)     private key key;      @manytoone(fetch = fetchtype.eager)//already tried without annotation , fetchtype.lazy     private book book; } 

you need declare cascade type on book attribute, jpa knows when performing operations on chapter entity.

@entity public class chapter {     @id     @generatedvalue(strategy = generationtype.identity)     private key key;      @manytoone(cascade = cascadetype.all) // can add fetch type if needed     private book book; } 

here description of cascade types.


Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -