Make use of Guava Optional utility for GeneralDao and GenericDao.

This commit is contained in:
robin shine 2013-07-03 13:13:20 +08:00
parent 8c836c5486
commit 419ad4cb9b
4 changed files with 9 additions and 5 deletions

View File

@ -5,6 +5,7 @@ import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.criterion.DetachedCriteria;
import com.google.common.base.Optional;
import com.pmease.commons.persistence.AbstractEntity;
public interface GeneralDao {
@ -19,7 +20,7 @@ public interface GeneralDao {
* @return
* found entity object, null if not found
*/
<T extends AbstractEntity> T find(Class<T> entityClass, Long entityId);
<T extends AbstractEntity> Optional<T> find(Class<T> entityClass, Long entityId);
/**
* Get a reference to the entity with the specified type and id from data store.

View File

@ -8,6 +8,7 @@ import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import com.google.common.base.Optional;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
@ -30,8 +31,8 @@ public class GeneralDaoImpl implements GeneralDao {
@Transactional
@Override
public <T extends AbstractEntity> T find(Class<T> entityClass, Long entityId) {
return (T) getSession().get(unproxy(entityClass), entityId);
public <T extends AbstractEntity> Optional<T> find(Class<T> entityClass, Long entityId) {
return Optional.fromNullable((T) getSession().get(unproxy(entityClass), entityId));
}
@Transactional

View File

@ -7,6 +7,7 @@ import javax.annotation.Nullable;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import com.google.common.base.Optional;
import com.pmease.commons.persistence.AbstractEntity;
@ -15,7 +16,7 @@ public interface GenericDao<T extends AbstractEntity> {
* Get the entity with the specified type and id from the datastore.
* If none is found, return null.
*/
public T find(Long entityId);
public Optional<T> find(Long entityId);
/**
* Get a reference to the entity with the specified type and id from the

View File

@ -6,6 +6,7 @@ import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import com.google.common.base.Optional;
import com.google.inject.Inject;
import com.pmease.commons.persistence.AbstractEntity;
import com.pmease.commons.util.ClassUtils;
@ -25,7 +26,7 @@ public class GenericDaoImpl<T extends AbstractEntity> implements GenericDao<T> {
}
@Override
public T find(Long entityId) {
public Optional<T> find(Long entityId) {
return generalDao.find(entityClass, entityId);
}