Java ObjectifyGenericDao<;T>;具有客观性4
我想利用Objectify4的新功能,但我的应用程序是构建的,正在使用版本3。我的应用程序主要基于Java ObjectifyGenericDao<;T>;具有客观性4,java,google-app-engine,objectify,Java,Google App Engine,Objectify,我想利用Objectify4的新功能,但我的应用程序是构建的,正在使用版本3。我的应用程序主要基于ObjectifyGenericDao模式,Objectify4设计模式与此有很大不同: ObjectifyGenericDao.java public class ObjectifyGenericDao<T> extends DAOBase { static final int BAD_MODIFIERS = Modifier.FINAL | Modifier.STATIC
ObjectifyGenericDao
模式,Objectify4设计模式与此有很大不同:
ObjectifyGenericDao.java
public class ObjectifyGenericDao<T> extends DAOBase
{
static final int BAD_MODIFIERS = Modifier.FINAL | Modifier.STATIC | Modifier.TRANSIENT;
static
{
// Register all your entity classes here
}
protected Class<T> clazz;
/**
* We've got to get the associated domain class somehow
*
* @param clazz
*/
protected ObjectifyGenericDao(Class<T> clazz)
{
this.clazz = clazz;
}
public ObjectifyGenericDao(ObjectifyOpts opts) {
super(opts);
//this.clazz = clazz;
}
public Key<T> put(T entity)
{
return ofy().put(entity);
}
// TODO This code was modified
// and need to be tested
public List<Key<T>> putAll(Iterable<T> entities)
{
Map<Key<T>, T> map = ofy().put(entities);
return new ArrayList<Key<T>>(map.keySet());
//return ofy().put(entities);
}
public void delete(T entity)
{
ofy().delete(entity);
}
public void deleteKey(Key<T> entityKey)
{
ofy().delete(entityKey);
}
public void deleteAll(Iterable<T> entities)
{
ofy().delete(entities);
}
public void deleteKeys(Iterable<Key<T>> keys)
{
ofy().delete(keys);
}
public T get(Long id) throws EntityNotFoundException
{
return ofy().get(this.clazz, id);
}
public T get(String id) throws EntityNotFoundException
{
return ofy().get(this.clazz, id);
}
public T get(Key<T> key) throws EntityNotFoundException
{
return ofy().get(key);
}
/**
* Convenience method to get all objects matching a single property
*
* @param propName
* @param propValue
* @return T matching Object
*/
public T getByProperty(String propName, Object propValue)
{
Query<T> q = ofy().query(clazz);
q.filter(propName, propValue);
return q.get();
}
public List<T> listByProperty(String propName, Object propValue)
{
Query<T> q = ofy().query(clazz);
q.filter(propName, propValue);
return asList(q.fetch());
}
public List<Key<T>> listKeysByProperty(String propName, Object propValue)
{
Query<T> q = ofy().query(clazz);
q.filter(propName, propValue);
return asKeyList(q.fetchKeys());
}
public T getByExample(T exampleObj)
{
Query<T> queryByExample = buildQueryByExample(exampleObj);
Iterable<T> iterableResults = queryByExample.fetch();
Iterator<T> i = iterableResults.iterator();
T obj = i.next();
if (i.hasNext())
throw new RuntimeException("Too many results");
return obj;
}
public List<T> listByExample(T exampleObj)
{
Query<T> queryByExample = buildQueryByExample(exampleObj);
return asList(queryByExample.fetch());
}
private List<T> asList(Iterable<T> iterable)
{
ArrayList<T> list = new ArrayList<T>();
for (T t : iterable)
{
list.add(t);
}
return list;
}
private List<Key<T>> asKeyList(Iterable<Key<T>> iterableKeys)
{
ArrayList<Key<T>> keys = new ArrayList<Key<T>>();
for (Key<T> key : iterableKeys)
{
keys.add(key);
}
return keys;
}
private Query<T> buildQueryByExample(T exampleObj)
{
Query<T> q = ofy().query(clazz);
// Add all non-null properties to query filter
for (Field field : clazz.getDeclaredFields())
{
// Ignore transient, embedded, array, and collection properties
if (field.isAnnotationPresent(Transient.class)
|| (field.isAnnotationPresent(Embedded.class))
|| (field.getType().isArray())
|| (Collection.class.isAssignableFrom(field.getType()))
|| ((field.getModifiers() & BAD_MODIFIERS) != 0))
continue;
field.setAccessible(true);
Object value;
try
{
value = field.get(exampleObj);
}
catch (IllegalArgumentException e)
{
throw new RuntimeException(e);
}
catch (IllegalAccessException e)
{
throw new RuntimeException(e);
}
if (value != null)
{
q.filter(field.getName(), value);
}
}
return q;
}
// Added, but may not be really useful
public Query<T> query(String filter, String value) {
Query<T> q = ofy().query(clazz).filter(filter, value);
return q;
}
public类ObjectifyGenericDao扩展了DAOBase
{
static final int BAD_MODIFIERS=Modifier.final | Modifier.static | Modifier.TRANSIENT;
静止的
{
//在此处注册所有实体类
}
保护类clazz;
/**
*我们必须以某种方式获取相关的域类
*
*@param-clazz
*/
受保护的ObjectifyGenericDao(类clazz)
{
this.clazz=clazz;
}
公共ObjectifyGenericDao(ObjectifyOpts opts){
超级(opts);
//this.clazz=clazz;
}
公钥put(T实体)
{
返回y().put(实体);
}
//TODO此代码已修改
//需要测试
公共列表putAll(可编辑实体)
{
Map Map=ofy().put(实体);
返回新的ArrayList(map.keySet());
//返回y().put(实体);
}
公共作废删除(T实体)
{
删除(实体);
}
public void deleteKey(Key entityKey)
{
ofy().delete(entityKey);
}
public void deleteAll(可删除的实体)
{
删除(实体);
}
公共无效删除密钥(可删除密钥)
{
删除(键);
}
公共T get(长id)抛出EntityNotFoundException
{
返回y().get(this.clazz,id);
}
公共T get(字符串id)引发EntityNotFoundException
{
返回y().get(this.clazz,id);
}
public T get(Key Key)抛出EntityNotFoundException
{
返回y().get(键);
}
/**
*获取与单个属性匹配的所有对象的方便方法
*
*@param propName
*@param-propValue
*@return T匹配对象
*/
公共T getByProperty(字符串propName、对象propValue)
{
Query q=ofy().Query(clazz);
q、 过滤器(propName、propValue);
返回q.get();
}
公共列表listByProperty(字符串propName、对象propValue)
{
Query q=ofy().Query(clazz);
q、 过滤器(propName、propValue);
返回asList(q.fetch());
}
公共列表listKeysByProperty(字符串propName、对象propValue)
{
Query q=ofy().Query(clazz);
q、 过滤器(propName、propValue);
返回asKeyList(q.fetchKeys());
}
公共T getByExample(T exampleObj)
{
queryquerybyexample=buildQueryByExample(exampleObj);
Iterable IterablerResults=queryByExample.fetch();
迭代器i=iterableResults.Iterator();
T obj=i.next();
if(i.hasNext())
抛出新的RuntimeException(“结果太多”);
返回obj;
}
公共列表listByExample(T exampleObj)
{
queryquerybyexample=buildQueryByExample(exampleObj);
返回asList(queryByExample.fetch());
}
私有列表asList(Iterable Iterable)
{
ArrayList=新建ArrayList();
for(T:iterable)
{
列表。添加(t);
}
退货清单;
}
私有列表asKeyList(Iterable iterableKeys)
{
ArrayList键=新的ArrayList();
for(键:iterableKeys)
{
key.add(key);
}
返回键;
}
私有查询buildQueryByExample(T exampleObj)
{
Query q=ofy().Query(clazz);
//将所有非空属性添加到查询筛选器
for(字段:clazz.getDeclaredFields())
{
//忽略瞬态、嵌入、数组和集合属性
if(字段isAnnotationPresent(瞬态类)
||(field.isAnnotationPresent(Embedded.class))
||(field.getType().isArray())
||(Collection.class.isAssignableFrom(field.getType()))
||((field.getModifiers()&BAD_修饰符)!=0))
继续;
字段。setAccessible(true);
目标价值;
尝试
{
value=field.get(例如obj);
}
捕获(IllegalArgumentException e)
{
抛出新的运行时异常(e);
}
捕获(非法访问例外e)
{
抛出新的运行时异常(e);
}
if(值!=null)
{
q、 过滤器(field.getName(),值);
}
}
返回q;
}
//添加,但可能不是真正有用
公共查询(字符串筛选器、字符串值){
Query q=ofy().Query(clazz).filter(filter,value);
返回q;
}
Objectify4的瓶颈在于它没有DAOBase,因此迁移现有代码不是很容易
如何在使用Objectify4功能时使用此模式?您可以在此处获取OfyService的代码: 在ObjectifyGenericDao中添加静态导入,然后可以使用以下方法:
public Key<T> save(T entity){
return ofy().save().entity(entity).now();
}
public void delete(T entity){
ofy().delete().entity(entity);
}
public T get(Long id){
return ofy().load().type(clazz).id(id).get();
}
公钥保存(T实体){
返回y().save().entity(entity).now();
}
公共作废删除(T实体){
ofy().delete().entity(实体);
}
公共不获取(长id){
返回y().load().type(clazz.id(id.get());
}
等等…您可以在此处获取OfyService的代码: 在ObjectifyGenericDao中添加静态导入,然后可以使用以下方法:
public Key<T> save(T entity){
return ofy().save().entity(entity).now();
}
public void delete(T entity){
ofy().delete().entity(entity);
}
public T get(Long id){
return ofy().load().type(clazz).id(id).get();
}
公钥保存(T实体){
返回y().save().entity(entity).now();
}
公共作废删除(T实体){
ofy().delete().entity(实体);
}
公共不获取(长id){
返回y().load().type(clazz.id(id.get());
}
依此类推…如上所述,只需删除扩展DAOBase
如上所述,只需删除扩展DAOBase