为什么核心数据存储类型的数组内容不存储在应用程序引擎中?
我遇到以下问题:
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;
}
}
结束语。我意识到我可以使用列表等来代替,如果我不明白这一点,那确实是我的备份计划。但是,我想知道为什么这不起作用,所以我希望任何关于我切换到对象而不是数组的建议都附有关于为什么数组不起作用的解释;)谢谢。
Ex animo, - 亚历山大·英林
I have the following issue:
A java object contains two arrays of core datastore types (com.google.appengine.api.datastore.Text and java.util.Date), plus an int (for storing the current populated position in the arrays) and some other fields.
I believe the documentation states that arrays of core datatypes should be ok (cf. http://code.google.com/appengine/docs/java/datastore/jdo/dataclasses.html, under the "Class and Field Annotations").
The object is updated using a method named "updateAnswer". When this method is called, the object is indeed updated (the int is incremented and stored correctly), but the arrays never store anything but nulls.
I would greatly appreciate it if someone could point out where my mistake lies.
Here is the object (and its parent, for completeness):
@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;
}
}
The parent:
@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;
}
}
A closing note. I realize I could use Lists etc instead, and if I do not figure this out that is indeed my backup plan. But, I would like to realize why this isn't working, so I'd love any suggestions that I switch to objects instead of arrays to be accompanied with an explanation as to why the arrays are not working ;) Thank you.
Ex animo, - Alexander Yngling
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以,睡了一觉后我突然想到......这是一个数组索引。我正在使用JDO。我是个白痴;)
我想过删除这个问题,但以防万一其他人搜索这个问题,这就是问题:
http://www.datanucleus.org/products/datanucleus/jdo/orm/arrays.html
JDO 无法知道数组索引已被已更新,因此您必须再次设置该字段,或使用以下命令告诉 JDO 更新数据库:
So, after some sleep it occurred to me... it's an array index. I'm using JDO. I'm an idiot ;)
I thought about deleting this question, but just in case someone else searches for this, here's the problem:
http://www.datanucleus.org/products/datanucleus/jdo/orm/arrays.html
JDO cannot know that an array index has been updated, so you have to either set the field again, or use the following to tell JDO to update the database: