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"?
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"?
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();
}
}
}