问题
I intend questions not to be a child since I had to manipulate it independently, and I don't want to persist the questions field, I would to fill it up by retrieving the questions manually. Here is the code.
Questionnaire.java
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Questionnaire{
//supposedly non-persistent
public List<Question> questions = new ArrayList<Question>();
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
public Long questionnaireID;
@Persistent
public String title;
@Persistent
private int items;
@Persistent
public String description;
Question.java
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Question{
//non-persistent as well
public ArrayList<Choice> choiceList = new ArrayList<Choice>();
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
public Long questionID;
@Persistent
public String text;
@Persistent
public long questionnaireID;
public Question(){
}
would spit out this error:
org.datanucleus.store.appengine.MetaDataValidator$DatastoreMetaDataException: Error in meta-data for com.ivanceras.server.Question.questionID: Cannot have a java.lang.Long primary key and be a child object (owning field is com.ivanceras.server.Questionnaire.questions).
回答1:
Adding a @NotPersistent
might help.
The GAE/J docs are totally misleading; they suggest that you need @Persistent
on every field and that is totally wrong. All fields have a default persistent flag ... things like String
, primitives, Collection
, List
, Set
, Map
are by default persistent so no need for @Persistent
on those. This point has been made to Google several times yet the docs still have this.
Use the DataNucleus docs if you want clear information as per the JDO spec
回答2:
Adding "transient" may help too
来源:https://stackoverflow.com/questions/1997580/is-there-a-way-to-have-a-non-persistent-field-in-gae-j-using-jdo