1
votes

When I try below code, "result" contains null.

    Collection<Data> result = ObjectifyService.ofy().load().type(Data.class).list();

result.size() is not zero.

What is the reason for containing null in "result"?

1
Please check my answer below. You are missing a load method between ofy() and type().Ankur Jain
@AnkurJain That's my typo. sorry. I updated my question.nurinamu
After set .cache(false) it is working fine. I don't know exactly why it is the solution. I need to look into it more.nurinamu
This looks more like a problem understanding how java Collections work. Is result.size() the same as the number of not-null elements in the list? The list can have null elements if the list's capacity is greater than its size. But that should not be a problem... Iterate result with an Iterator and you will not need to be concerned about it: List<Data> all = ofy().load().type(Data.class).list(); Iterator<Data> it = all.iterator(); while(it.hasNext()){Data data = it.next();}manubot
Yes. That is my curious. It's ridiculous! I didn't know Collection can contain null!nurinamu

1 Answers

1
votes

Please check the following code describes all the basic operations in Objectify.

Getting List of all Objects in Objectify

List<T> list = ofy().load().type(clazz).list();

Also you can find some additional queries in Objectify using a GenericDAO and its Implemention in the code below.

GenericDAO Interface

package com.myweb.webservices.common.dao;

import java.util.List;

import com.myweb.webservices.common.exception.DatabaseException;

public interface GenericDao {
public <T> void create(T t);
public <T> String createWithKey(T t);
public <T> Long createWithID(T t);
public <T> void update(Class<T> clazz, Long id, T t) throws DatabaseException;
public <T> void update(Class<T> clazz, String key, T t) throws DatabaseException;
public <T> T getById(Class<T> clazz, Long id) throws DatabaseException;
public <T> T getByKey(Class<T> clazz, String key) throws DatabaseException;
public <T> List<T> list(Class<T> clazz);
public <T> void delete(Class<T> clazz, Long id) throws DatabaseException;
public <T> void deleteByKey(Class<T> clazz, String key) throws DatabaseException;   
}

GenericDAO Implementation

package com.myweb.webservices.common.dao;

import static com.googlecode.objectify.ObjectifyService.ofy;

import java.util.List;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.ObjectifyService;
import com.myweb.webservices.common.exception.DatabaseException;
import com.myweb.webservices.model.User;

public abstract class AbstractGenericDaoImpl implements GenericDao{

static {
    ObjectifyService.register(User.class);
}

@Override
public <T> void create(T t) {
    ofy().save().entity(t).now();
}

@Override
public <T> String createWithKey(T t) {
    Key<T> key =  ofy().save().entity(t).now();
    return key.getString();
}

@Override
public <T> Long createWithID(T t) {
    Key<T> key =  ofy().save().entity(t).now();
    return key.getId();
}

@Override
public <T> void update(Class<T> clazz, Long id, T t) throws DatabaseException {
    if (id == null) {
        throw new DatabaseException("ID cannot be null");
    }
    T tnew = ofy().load().type(clazz).id(id).get();
    ofy().save().entity(tnew).now();
}

@Override
public <T> void update(Class<T> clazz, String key, T t) throws DatabaseException {
    if (key == null) {
        throw new DatabaseException("ID cannot be null");
    }
    T tnew = ofy().load().type(clazz).id(key).get();
    ofy().save().entity(tnew).now();

}

@Override
public <T> T getById(Class<T> clazz, Long id) throws DatabaseException {
    if (id == null) {
        throw new DatabaseException("ID cannot be null");
    }
    return ofy().load().type(clazz).id(id).get();
}

@Override
public <T> T getByKey(Class<T> clazz, String key) throws DatabaseException {
    if (key == null) {
        throw new DatabaseException("ID cannot be null");
    }
    return ofy().load().type(clazz).id(key).get();
}

@Override
public <T> List<T> list(Class<T> clazz) {
    List<T> list = ofy().load().type(clazz).list();
    return list;
}

@Override
public <T> void delete(Class<T> clazz, Long id) throws DatabaseException {
    if (id == null) {
        throw new DatabaseException("ID cannot be null");
    }
    T t = ofy().load().type(clazz).id(id).get();
    if(t != null){
        ofy().delete().entity(t).now();
    }
}

@Override
public <T> void deleteByKey(Class<T> clazz, String key) throws DatabaseException {
    if (key == null) {
        throw new DatabaseException("ID cannot be null");
    }
    T t = ofy().load().type(clazz).id(key).get();
    if(t != null){
        ofy().delete().entity(t).now();
    }
}
}