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:
Posts (Atom)