I have an entity like this:
@Entity
@XmlRootElement
public class Article {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private com.google.appengine.api.datastore.Key key;
private String content;
private String title;
private Date created;
private Date lastUpdate;
private boolean isActive;
/* public getter and setters */
}
Now I have a rest webserivce which returns the articles:
@SuppressWarnings({ "unchecked", "unused" })
@GET
@Path("/{user}")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public List<Article> getArtilceListByUser(@PathParam("user") String userName) {
List<Article> articles = null;
EntityManager em = EMF.get().createEntityManager();
try {
articles = (List<Article>) em.createQuery("SELECT FROM Article WHERE createdBy = :user AND isActive = true")
.setParameter("user", userName)
.getResultList();
// lazy load is activated, but em is closed (find a better solution)
for (Article article : articles) ;
} finally {
em.close();
}
return articles;
}
The problem is, that the object of com.google.appengine.api.datastore.Key cannot get serialized to json or xml, but I need the id of the articles… So have I to use a long-type for the id or to add an extra field, which wraps the key? Do you have better solutions for this problem?
com.google.appengine.api.datastore.KeyFactory.keyToString
and then
com.google.appengine.api.datastore.KeyFactory.stringToKey
These create and consume “websafe” keys for just this purpose.