【问题标题】:id cannot be zero error while inserting new entity插入新实体时 id 不能为零错误
【发布时间】:2013-06-03 16:08:06
【问题描述】:

我正在尝试使用 JPA 在应用引擎数据存储中插入一个新实体。我收到以下错误。有人可以建议可能出了什么问题。

 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是0,但是不能。

标签: android google-app-engine jpa


【解决方案1】:

id 是自动生成的(因为你想要),它不应该有一个 setter 方法。如果您愿意,请删除自动生成并手动设置。

【讨论】:

  • 注释了实体中的 setId() 方法。还是同样的问题。
【解决方案2】:

我使用 JPA,它运行良好。我不确定你的问题,但你可以试试这个:

  1. 如果将生成类型更改为GenerationType.IDENTITY,会怎样?
  2. 如果您使用原生 long 而不是 Long,会怎样?

【讨论】:

  • native long 会更糟——因为它默认为 0。
猜你喜欢
  • 2015-04-27
  • 2020-11-14
  • 1970-01-01
  • 2013-03-30
  • 1970-01-01
  • 2012-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多