android - 插入新实体时 id 不能为零错误

标签 android google-app-engine jpa

我正在尝试使用 JPA 在 App Engine 数据存储区中插入一个新实体。我收到以下错误。有人可以建议可能是错的吗。

 java.lang.IllegalArgumentException: id cannot be zero
at com.google.appengine.api.datastore.KeyFactory.createKey(KeyFactory.java:52)
at com.google.appengine.api.datastore.KeyFactory.createKey(KeyFactory.java:47)
at com.google.appengine.api.datastore.KeyFactory.createKey(KeyFactory.java:35)
at com.google.appengine.datanucleus.EntityUtils.intOrLongToInternalKey(EntityUtils.java:381)
at com.google.appengine.datanucleus.EntityUtils.idToInternalKey(EntityUtils.java:220)
at com.google.appengine.datanucleus.EntityUtils.idToInternalKey(EntityUtils.java:208)
at com.google.appengine.datanucleus.DatastoreIdentityKeyTranslator.getKey(DatastoreIdentityKeyTranslator.java:32)

这是我的实体类

   //imports
   @Entity
   public class QuizTable {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String quizName;
private String quizContent;

public QuizTable(){}

public Long getId() {return id;}
public String getQuizName() {return quizName;}
public String getQuizContent() {return quizContent;}

public void setId(Long id) { this.id = id;}
public void setQuizName(String quizName) {this.quizName=quizName;}
public void setQuizContent(String quizContent) {this.quizContent=quizContent;}

 }

最佳答案

id 是自动生成的(因为你想要),它不应该有 setter 方法。删除自动生成并根据需要手动设置。

关于android - 插入新实体时 id 不能为零错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16901141/

相关文章:

tomcat - JNDI 查找使用 lookUp 工作正常,但在 JPA 的 persistence.xml 中使用时却不行

android - API 19 ScrollView 没有动量

java - 如何从字符串打印时间?

java - 如何知道用户何时完成输入?

java - 配置 appengine-web.xml

java - 使用用户事务在 Tomcat 中实现 Hibernate 持久化

android - 使用 App Engine Python 的 C2DM 返回 401 错误

java - 是否可以通过 Google App Engine 中的光标确定您在结果集中的位置?

python - 哪些验证码解决方案可以与 App Engine 一起使用?

database - JPQL View 返回重复项