java - "Id cannot be zero"- GoogleJsonResponseException 400 错误请求

标签 java android google-app-engine jpa google-cloud-endpoints

尝试使用 GAE 中的端点检索列表时,我似乎遇到了错误。错误如下:

04-08 01:01:30.145: I/System.out(14650): com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
04-08 01:01:30.145: I/System.out(14650): {
04-08 01:01:30.145: I/System.out(14650):   "code": 400,
04-08 01:01:30.145: I/System.out(14650):   "errors": [
04-08 01:01:30.145: I/System.out(14650):     {
04-08 01:01:30.145: I/System.out(14650):       "domain": "global",
04-08 01:01:30.145: I/System.out(14650):       "message": "java.lang.IllegalArgumentException: id cannot be zero",
04-08 01:01:30.145: I/System.out(14650):       "reason": "badRequest"
04-08 01:01:30.145: I/System.out(14650):     }
04-08 01:01:30.145: I/System.out(14650):   ],
04-08 01:01:30.145: I/System.out(14650):   "message": "java.lang.IllegalArgumentException: id cannot be zero"
04-08 01:01:30.145: I/System.out(14650): }

这是我尝试在端点中使用的方法:

@SuppressWarnings({ "cast", "unchecked"})
public List<Comercio> listComercio() {
   EntityManager mgr = getEntityManager();
   List<Comercio> result= new ArrayList<Comercio>();

   try {
      Query query = mgr.createQuery("select c from Comercio c", Comercio.class);
      for (Object obj : (List<Object>) query.getResultList()) {
        result.add((Comercio) obj);
      }
      } finally {
        mgr.close();
      }
      return result;
}
}

这是实体类:

@Entity
public class Comercio{

@Id
private Long id;

private String title;

private String url;

private String NEWSTYPE;

public Comercio() {
}

public Long getId() {
    return id;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getUrl() {
    return url;
}

public void setUrl(String url) {
    this.url = url;
}

public void setNewstypeid(String newstypeid) {
    this.NEWSTYPE = newstypeid;
}

public String getNewstypeid() {
    return NEWSTYPE;
}

}

这是我的 android 项目中的 AsyncTask:

public class AsyncDatastoreArticles extends CloudAsyncTask {

AsyncDatastoreArticles(Home activity) {
   super(activity);
}

@Override
protected void doInBackground() throws IOException {
   // TODO Auto-generated method stub

   List<Comercio> list = endpoint.listComercio().execute().getItems();
   if (list != null) {
   //DO SOMETHING
   } 

}
}

我需要帮助。

提前致谢。

最佳答案

我猜是因为没有设置ID。您可以使用以下代码在您的实体类中设置自动生成的 ID。

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

另请确保在这种情况下不要在您的实体中保留任何 setId() 方法。

关于java - "Id cannot be zero"- GoogleJsonResponseException 400 错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15872096/

相关文章:

java - HTTP/1.1 400 对具有基本身份验证的 httpGet 的错误请求

ScrollView 中的 Android 边距顶部负值

google-app-engine - 我可以在 Google App Engine Standard(两种类型)中拥有 python27 和 python37 的应用程序版本吗

java - SWT 中从右到左的 Shell

java - 将字符串转换为 XML 文档而不更改结束标记

android - 如何通过代码启用指针定位?

python - BlobInfo 对象的 md5_hash 属性的用途是什么?

java - 如何使用 ProcessLifecycleOwner 捕捉生命周期事件?

java - 某些函数文本在 netbeans ide 上显示为横线

google-app-engine - 在已部署的应用引擎应用程序中,用户 ID 是否可以超过 64 位?