插入新实体时,ID 不能为零错误。

3

我正在尝试使用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;}

 }

1
你的ID是0,但它不应该是。 - njzk2
2个回答

0

id是自动生成的(因为您想要这样),它不应该有一个setter方法。如果您喜欢,可以删除自动生成并手动设置。


在实体中注释掉了 setId() 方法。问题仍然存在。 - user1938357

0

我使用JPA,它运行良好。我不确定你的问题是什么,但你可以尝试以下方法:

  1. 如果你将生成类型更改为GenerationType.IDENTITY会怎样?
  2. 如果你使用本地的long而不是Long会怎样?

本地长整型会更糟,因为它默认为0。 - icyerasor

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接