【问题标题】:Why are array contents of a core datastore type not stored in app engine?为什么核心数据存储类型的数组内容未存储在应用引擎中?
【发布时间】:2012-02-02 06:59:37
【问题描述】:

我有以下问题:

Java 对象包含两个核心数据存储类型数组(com.google.appengine.api.datastore.Text 和 java.util.Date),以及一个 int(用于存储数组中的当前填充位置)和其他一些字段。

我相信文档指出核心数据类型的数组应该没问题(参见“类和字段注释”下的http://code.google.com/appengine/docs/java/datastore/jdo/dataclasses.html)。

使用名为“updateAnswer”的方法更新对象。调用此方法时,对象确实会更新(int 递增并正确存储),但数组从不存储空值。

如果有人能指出我的错误在哪里,我将不胜感激。

这里是对象(以及它的父对象,为了完整起见):

@PersistenceCapable
public class TextualAnswer extends Answer {

    @Persistent
    private Text textAnswer;

    @Persistent
    private Date date;

    @Persistent
    private int pos;

    @Persistent
    private Text texts[];

    @Persistent
    private Date dates[];

    public TextualAnswer(Key question, Key user, Date date) {
        super(question, user, 0);
        this.textAnswer = null;
        this.date = date;
        pos = 0;
        texts = new Text[20];
        dates = new Date[20];
    }

    public String getTextAnswer() {
        return (textAnswer != null ? textAnswer.getValue() : null);
    }

    public Date getDate() {
        return date;
    }

    public void updateAnswer(String textAnswer, Date date) {
        if (texts.length == pos) { // expand?
            Text ttemp[] = texts;
            texts = new Text[pos * 2];
            System.arraycopy(ttemp, 0, texts, 0, pos);

            Date dtemp[] = dates;
            dates = new Date[pos * 2];
            System.arraycopy(dtemp, 0, dates, 0, pos);
        }
        texts[pos] = this.textAnswer;
        dates[pos] = this.date;
        pos++;

        this.textAnswer = (textAnswer != null ? new Text(textAnswer) : null);
        this.date = date;
    }
}

父母:

@PersistenceCapable
@Inheritance(strategy = InheritanceStrategy.SUBCLASS_TABLE)
public abstract class Answer {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;

    @Persistent
    private Key question;

    @Persistent
     private Key user;

    @Persistent
    private double score;

    @Persistent
    private boolean last;

    @Persistent
    private Text comment;

    public Answer(Key question, Key user, double score) {
        this.question = question;
        this.user = user;
        this.score = score;
        last = false;
        comment = null;
    }

    public Key getKey() {
        return key;
    }

    public Key getQuestion() {
        return question;
    }

    public Key getUser() {
        return user;
    }

    public double getScore() {
        return score;
    }

    public boolean isLast() {
        return last;
    }

    public String getComment() {
        return comment != null ? comment.getValue() : null;
    }

    public void setScore(double score) {
        this.score = score;
    }

    public void setLast(boolean last) {
        this.last = last;
    }

    public void setComment(String comment) {
        this.comment = comment != null ? new Text(comment) : null;
    }
}

结束语。我意识到我可以改用 Lists 等,如果我不明白这确实是我的备份计划。但是,我想知道为什么这不起作用,所以我喜欢任何关于我切换到对象而不是数组的建议,并附上关于数组为什么不起作用的解释;)谢谢。

Ex animo, - Alexander Yngling

【问题讨论】:

    标签: java google-app-engine google-cloud-datastore


    【解决方案1】:

    所以,经过一段时间的睡眠,我突然想到……这是一个数组索引。我正在使用 JDO。我是个白痴;)

    我想删除这个问题,但以防其他人搜索这个问题:

    http://www.datanucleus.org/products/datanucleus/jdo/orm/arrays.html

    JDO 无法知道数组索引已被更新,因此您必须重新设置该字段,或者使用以下命令告诉 JDO 更新数据库:

    JDOHelper.makeDirty(obj, "fieldName");
    

    【讨论】:

      猜你喜欢
      • 2011-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多