package jdo;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.orm.jdo.support.JdoDaoSupport;
import GenericDao;
import com.google.appengine.api.datastore.Key;
public class GenericDaoJdo <T, PK extends Serializable> extends JdoDaoSupport implements GenericDao<T, PK> {
/**
* Log variable for all child classes. Uses LogFactory.getLog(getClass()) from Commons Logging
*/
protected final Logger log = LoggerFactory.getLogger(getClass());
private Class<T> persistentClass;
/**
* Constructor that takes in a class for easy creation of DAO
*/
public GenericDaoJdo(Class<T> persistentClass) {
this.persistentClass = persistentClass;
}
public boolean exists(PK id) {
T entity = (T) getJdoTemplate().getObjectById(this.persistentClass, id);
return entity != null;
}
public T get(PK id) {
T entity = (T) getJdoTemplate().getObjectById(this.persistentClass, id);
if (entity == null) {
log.warn("Uh oh, '" + this.persistentClass + "' object with id '" + id + "' not found...");
throw new ObjectRetrievalFailureException(this.persistentClass, id);
}
return entity;
}
public T getByKey(Key id) {
T entity = (T) getJdoTemplate().getObjectById(this.persistentClass, id);
if (entity == null) {
log.warn("Uh oh, '" + this.persistentClass + "' object with id '" + id + "' not found...");
throw new ObjectRetrievalFailureException(this.persistentClass, id);
}
return entity;
}
public List<T> getAll() {
return new ArrayList<T>(getJdoTemplate().find(persistentClass));
}
public List<T> getAllDistinct() {
Collection result = new LinkedHashSet(getAll());
return new ArrayList(result);
}
public void remove(PK id) {
getJdoTemplate().deletePersistent(this.get(id));
}
public T save(T object) {
return (T) getJdoTemplate().makePersistent(object);
}
}
Friday, May 8, 2009
GenericDao w/ Spring, JDO, and GAE/J
In case anyone is interested. The only gotcha I've encountered so far, is that using the JdoCallback is preferred by GAE vs using some of the Spring defined implementations.
Subscribe to:
Post Comments (Atom)
Hi
ReplyDeleteCan you put the spring configuration of this settings?
Thanks
Hear, Hear! about what Anonymous #1 said.
ReplyDeleteEverytime I try extending JdoDaoSupport I get an error about not specifying the persistence manager factory -- even though I explicitly set a bean up to do that.
GenericDaoJdo is realy cool.
ReplyDeleteThere isn’t any limitation in types and count of passed arguments. By default the first selector part when calling remote service’s method is the method’s name, and following selector parts can be named as we want. We can however use setUsesFullSelectorName method on the proxy service to use the full selector parts as the remote service’s method name.
ReplyDelete