java - 使用 Objectify 4 对象化 GenericDao<T>

标签 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 | 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;

}

Objectify4 的瓶颈是它没有 DAOBase,因此迁移现有代码不太容易。

如何在使用 Objectify4 功能时获得此模式?

最佳答案

Objectify Google Group 中所述,只需删除extends DAOBase即可。

关于java - 使用 Objectify 4 对象化 GenericDao<T>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12578827/

相关文章:

java - 如何在 AppEngine 中使用 JPA 2 获取实体、在 GWT 中更新并保存?

java.lang.NoClassDefFoundError : com/googlecode/objectify/ObjectifyService

java - 使用 Java,接收 Json 消息,找到字段的子集,检查所述字段,然后映射到新的 Code 值并导出为 Json

java - Android 后台服务启动后立即自行关闭

java - Java Web 应用程序的高级日志记录

java - 对象化 : How to Migrate an Entity with a String ID to a Long ID

google-app-engine - Objectify - 如何@Load a List<Ref<?>>?

具有两组标签的 Java JSlider

java - Applet 和 Swing 在 Eclipse 中不显示组件

google-app-engine - 尝试登录本地 Google AppEngine 服务器时出现 503 错误