当我尝试下面的代码时,“result”包含null。
Collection<Data> result = ObjectifyService.ofy().load().type(Data.class).list();
result.size()不为零。
在“结果”中包含null的原因是什么?
答案 0 :(得分:1)
请检查以下代码,描述Objectify中的所有基本操作。
获取Objectify中所有对象的列表
List<T> list = ofy().load().type(clazz).list();
您还可以在下面的代码中使用GenericDAO及其实现在Objectify中找到一些其他查询。
GenericDAO接口
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实施
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();
}
}
}