Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8587147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:29:53+00:00 2026-06-11T22:29:53+00:00

I want to leverage on the new features of Objectify4 however my application is

  • 0

I want to leverage on the new features of Objectify4 however my application is build and is working with version 3. My application largely builds upon the ObjectifyGenericDao pattern and that the Objectify4 design pattern is quite different from this:

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;

}

The bottleneck with Objectify4 is that it does not have DAOBase so it not very easy to migrate existing codes.

How can I have this pattern while using Objectify4 features?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T22:29:55+00:00Added an answer on June 11, 2026 at 10:29 pm

    As mentioned on the Objectify Google Group, just drop the extends DAOBase.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to leverage the Scala's Actor Framework while I develop the user interface
I'm trying to create a bookmark extension in Chrome and I want to leverage
If I want to work with an object and leverage LINQ to SQL what
I'm new to lambda expressions and looking to leverage the syntax to set the
I want to build a gmail style search app for my site with configurable
I have a strong preference for working in code, leverage IntelliSense and opening up
We're planning to create a web application where users can build custom forms, choosing
I want to leverage get_lock() function of mysql as a global lock, but it
I want to start a new Open source project regarding natural math and Matlab,
I'm pretty new to iOS development, and I want to figure out if there's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.