"Id cannot be null" - GoogleJsonResponseException 400 Bad Request

I seem to get an error when trying to get a list using an endpoint in GAE. The error is as follows:

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): } 

This is the method I'm trying to use at my endpoint:

 @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; } } 

This is the entity class:

 @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; } } 

This is AsyncTask from my Android project:

 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 } } } 

I need help.

Thanks in advance.

+4
source share
3 answers

I think the reason is that the ID is not set. You can use the code below to set the auto-generated identifier in an entity class.

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

Also, don't forget to save the setId () method in your entity in this case.

+1
source

It was used here and here as a Long , but just wanted to point it out again:

Use uppercase Long , because Long defaults to 0.

+1
source

According to the error message, there might be an entry in your database with an identification value of 0, which will result in this error from JPA. Since according to JPA, Id should not be zero.

Please check the database once.

0
source

All Articles