Adelin Adelin - 2 months ago 13
Java Question

Unable to get object from datastore using objectify

I am working on a simple expenses manager that will be deployed on google appengine. I am using objectify as Appengine ORM. now the problem is that I am unable to get a simple Object from datastore. The Session here is always null !! But when I check it out in localhost datastore I can see that it is there !

@Override
public String findEmailBySessionId(String sid) {
Session session = datastore.load().type(Session.class).id(sid).now();
if (session != null && (session.getDate().after(new Date()) || session.isToBeRemembered())) {
return session.getEmail();
} // this is always null !
return null;
}





@Entity
public class Session {


private String email;

@Id
private String sessionId;

private Date date;

private boolean toBeRemembered;

@Parent
private Key<User> parent;
.........
}





@Entity
public class User {

@Id
private String email;

private String name;
private String password;
private Date dateOfBirth;
private String hashSalt;

public User() {
}

Answer

Ok, I've got the answer and it is very silly. It is actually very stupid.

In google documentation there is a restriction on query that says:

*Filtering on unindexed properties returns no results !!!! * link For objectify to index a property that property should be annotated with @Index annotation. In the code above I forgot to put @Index annotation on date ! And this was the problem. After I put the annotation back everything returned to normal.