Session: How to Get Entity ID

Entities does not have to contain an ID property or field. In the case of such an entity, and a need for knowing under what ID it is stored on the server, the getDocumentId method was created.


String getDocumentId(Object entity)
entity Object Instance of an entity for which an ID will be returned
Return Value
String Returns the ID for a specified entity. The method may return null if entity is null, isn't tracked, or the ID will be generated on the server.


public class Comment {
    private String author;
    private String message;

    public String getAuthor() {
        return author;

    public void setAuthor(String author) { = author;

    public String getMessage() {
        return message;

    public void setMessage(String message) {
        this.message = message;

String commentId = session
    .getDocumentId(comment);// e.g. comments/1-A