Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Below is a Generic DAO Class from one of my work, where I have implemented almost all basic queries in Objectify. It is actually a Generic Implementation of basic Objectify functions using Template Classes.</p> <pre><code>package com.myweb.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.common.exception.DatabaseException; import com.myweb.model.User; public abstract class AbstractGenericDaoImpl implements GenericDao{ static { ObjectifyService.register(User.class); } @Override public &lt;T&gt; void create(T t) { ofy().save().entity(t).now(); } @Override public &lt;T&gt; String createWithKey(T t) { Key&lt;T&gt; key = ofy().save().entity(t).now(); return key.getString(); } @Override public &lt;T&gt; Long createWithID(T t) { Key&lt;T&gt; key = ofy().save().entity(t).now(); return key.getId(); } @Override public &lt;T&gt; void update(Class&lt;T&gt; 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 &lt;T&gt; void update(Class&lt;T&gt; 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 &lt;T&gt; T getById(Class&lt;T&gt; 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 &lt;T&gt; T getByKey(Class&lt;T&gt; 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 &lt;T&gt; List&lt;T&gt; list(Class&lt;T&gt; clazz) { List&lt;T&gt; list = ofy().load().type(clazz).list(); return list; } @Override public &lt;T&gt; void delete(Class&lt;T&gt; 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 &lt;T&gt; void deleteByKey(Class&lt;T&gt; 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(); } } } </code></pre> <p>Below is a method which uses filter query example in Objectify. </p> <pre><code>public User getUserByEmail(String email) { return ofy().load().type(User.class).filter("email", email).first().get(); } </code></pre> <p>Listing fields with Filter</p> <pre><code>public List&lt;User&gt; getUsers(String f1, String f2) { return ofy().load().type(User.class).filter("f1", f1).filter("f2", f2).list(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload