JasonK
JasonK

Reputation: 5294

Retrieving user object by ID

I still can't work with GAE's keys/ids. I keep getting the error: No entity was found matching the key: Key(Medewerker(5201690726760448)). The entities exist in the datastore, I checked this multiple times.

I'm trying to just simply get an user object with a certain ID. In my servlet I have the following code:

Long userId = Long.parseLong(req.getParameter("user"));
User user = userDao.getUser(userId);

The above code brings up the error. In userDaoOfyImpl.java I have the following method 'getUser':

public Gebruiker getGebruiker(Long id) {
    Gebruiker result = null;
    Gebruiker leerling = (Gebruiker) ofy.get(Leerling.class, id);
    Gebruiker medewerker = (Gebruiker) ofy.get(Medewerker.class, id);
    Gebruiker stagebedrijf = (Gebruiker)ofy.get(StageBedrijf.class, id);

    //Gebruiker instantie returnen
    if(leerling != null) {
        result = leerling;
    } else if(medewerker != null) {
        result = medewerker;
    } else if(stagebedrijf != null) {
        result = stagebedrijf;
    }

    return result;
}

The variables are dutch but I think you guys know the idea. The above method searches in different classes looking for a user that matches the ID and then returns it.

The problem is I get the error shown above and I'm really getting frustrated, what am I doing wrong guys? Is it the method or the way I use ID's or...?

Thanks in advance!

Upvotes: 2

Views: 159

Answers (1)

Mateusz
Mateusz

Reputation: 1224

here you can read for the get method:

Throws: NotFoundException - if the key does not exist in the datastore

use

 Gebruiker leerling = (Gebruiker) ofy.find(Leerling.class, id);

the find method doesn't throws NotFoundException when the key doesn't exist but null.

Upvotes: 1

Related Questions