“Id不能为零” - GoogleJsonResponseException 400 Bad Request

时间:2013-04-08 05:49:57

标签: 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
   } 

}
}

我需要帮助。

提前致谢。

3 个答案:

答案 0 :(得分:1)

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

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

此外,请确保在您的实体中不要保留任何setId()方法。

答案 1 :(得分:1)

这个主题在Long中贯穿本主题,但只想再指出一次:

使用大写Long,因为long默认为零..

答案 2 :(得分:0)

根据错误消息,您的数据库中可能有Id值为0的记录,这将导致JPA出现此错误。因为根据JPA,Id不应该为零。

请检查一次数据库。