1
當我嘗試下面的代碼時,「result」包含null。Objectify - 查詢結果中的列表包含空值
Collection<Data> result = ObjectifyService.ofy().load().type(Data.class).list();
result.size()不爲零。
在「result」中包含null的原因是什麼?
當我嘗試下面的代碼時,「result」包含null。Objectify - 查詢結果中的列表包含空值
Collection<Data> result = ObjectifyService.ofy().load().type(Data.class).list();
result.size()不爲零。
在「result」中包含null的原因是什麼?
請檢查以下代碼描述Objectify中的所有基本操作。
讓所有對象列表中物化
List<T> list = ofy().load().type(clazz).list();
,你也可以找到在下面的代碼使用GenericDAO及其FPGA實現在物化一些額外的查詢。
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();
}
}
}
請檢查我的回答如下。你缺少ofy()和type()之間的加載方法。 –
@AnkurJain這是我的錯字。抱歉。我更新了我的問題。 – nurinamu
設置.cache(false)後,它工作正常。我不知道爲什麼它是解決方案。我需要更多地研究它。 – nurinamu