My hierarchy is:
Record is a super class, t开发者_JS百科here are many classes that extends Record, say WellnessRecord. There is another class Note. Record owns a List.
Now I want to makePersistent(WellnessRecord) using GAE for Java, and after reading this in GAE's document about Owned One-to-Many Relationships: http://code.google.com/appengine/docs/java/datastore/jdo/relationships.html#Owned_One_to_Many_Relationships
I wrote this in Note.java:
@Persistent
private Record record //only for mapping to the owner class
and this in Record.java:
@Persistent(mappedBy = "record", defaultFetchGroup = "true")
@Element(dependent = "true")
protected LinkedList<Note> notes = new LinkedList<Note>();
But it doesn't work as I expected!
I can addNote() by adding a Note to WellnessRecord's variable notes, and makePersistence(WellnessRecord)
I can editNote() by get the particular WellnessRecord via getObjectById() and when I edit the notes, GAE will track the modification and save it automatically.
But when I want to deleteNote(), there would always be exceptions.
I debug the program and found that it went well in getting the WellnessRecord from database and delete the Note
in Record
's notes
, however, the exceptions came out when the modification is saved into the database!
I tracked the execution and found that the "record" variable in Note is still null even if it has been makePersistent().
In my project, there is also a Item class which has List, I write the @-annotations similarly, but addRecord(), editRecord(), deleteRecord() goes well. So the problem should be the GAE's support for polymorphism!
My hypothesis for the problem is that: When addNote() or editNote(), I find the Record first and modify Note via Record, since Record can easily find the Note, it goes well.
But when deleteNote(), after delete the object in memory, GAE tracks the modification and want to delete the Note in the database, but it couldn't find the Owner Record via Note since Note's "record" is null.
That's my problem in detail.
In a word: 3 classes: A,B, and C. B extends A, A has a List, how to make them persistent in GAE Java???
Any suggestions are appreciated!
精彩评论