Is there a way to have a non-persistent field in G

2019-08-28 14:55发布

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).

2条回答
何必那么认真
2楼-- · 2019-08-28 15:23

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

查看更多
时光不老,我们不散
3楼-- · 2019-08-28 15:44

Adding "transient" may help too

查看更多
登录 后发表回答