Refactor GeneralDao and GenericDao.

This commit is contained in:
robin shine 2013-07-03 10:04:58 +08:00
parent 2d32ed69ea
commit 680f8e9118
5 changed files with 43 additions and 82 deletions

View File

@ -2,12 +2,8 @@ package com.pmease.commons.persistence.dao;
import java.util.List;
import javax.annotation.Nullable;
import org.hibernate.HibernateException;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import com.pmease.commons.persistence.AbstractEntity;
@ -85,35 +81,9 @@ public interface GeneralDao {
Object find(DetachedCriteria detachedCriteria);
/**
* Search specified entity with specified hibernate criterions and orders.
* @param entityClass
* Class of the entity to search.
* @param criterions
* Hibernate criterions to restrict search result. No restrictions will be set
* if pass a null value or empty array.
* @param orders
* Hibernate orders to order search result. No orders will be set if pass a
* null value or empty array.
* @param firstResult
* First result of the query. Set to 0 if this value should be ignored.
* @param maxResults
* Max number of returned entities. Set to 0 if no limit of number of returned
* entities should be set.
* Count entities of specified class matching specified {@link DetachedCriteria}
* @return
* List of matched entities.
* Number of entities matching specified {@link DetachedCriteria}
*/
<T extends AbstractEntity> List<T> search(Class<T> entityClass, @Nullable Criterion[] criterions,
@Nullable Order[] orders, int firstResult, int maxResults);
/**
* Count entities of specified class matching specified hibernate criterions.
* @param entityClass
* Class of entity to count.
* @param criterias
* Hibernate criterions to restrict entities to be counted. No retrictions will be
* set if pass a null value or empty array.
* @return
* Number of entities matching specified criterions.
*/
<T extends AbstractEntity> int count(Class<T> entityClass, @Nullable Criterion[] criterias);
<T extends AbstractEntity> int count(DetachedCriteria detachedCriteria);
}

View File

@ -5,9 +5,7 @@ import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import com.google.inject.Inject;
@ -98,32 +96,8 @@ public class GeneralDaoImpl implements GeneralDao {
}
@Override
public <T extends AbstractEntity> List<T> search(Class<T> entityClass, Criterion[] criterions,
Order[] orders, int firstResult, int maxResults) {
Criteria criteria = getSession().createCriteria(entityClass);
if (criterions != null) {
for (int i=0; i<criterions.length; i++)
criteria.add(criterions[i]);
}
if (orders != null) {
for (int i=0; i<orders.length; i++)
criteria.addOrder(orders[i]);
}
if (firstResult != 0)
criteria.setFirstResult(firstResult);
if (maxResults != 0)
criteria.setMaxResults(maxResults);
return criteria.list();
}
@Override
public <T extends AbstractEntity> int count(Class<T> entityClass, Criterion[] criterions) {
Criteria criteria = getSession().createCriteria(entityClass);
if (criterions != null) {
for (int i=0; i<criterions.length; i++)
criteria.add(criterions[i]);
}
public <T extends AbstractEntity> int count(DetachedCriteria detachedCriteria) {
Criteria criteria = detachedCriteria.getExecutableCriteria(getSession());
criteria.setProjection(Projections.rowCount());
return (Integer) criteria.uniqueResult();
}

View File

@ -29,7 +29,7 @@ public interface GenericDao<T extends AbstractEntity> {
* @throws a
* HibernateException if no matching entity is found
*/
public T getReference(Long entityId);
public T load(Long entityId);
/**
* If the id of the entity is null or zero, add it to the datastore and
@ -52,6 +52,7 @@ public interface GenericDao<T extends AbstractEntity> {
/**
* Delete entity of specified identifier without actually loading the entity.
*
* @param entityClass
* class of the entity
* @param entityId
@ -60,25 +61,24 @@ public interface GenericDao<T extends AbstractEntity> {
void deleteById(Long entityId);
/**
* Search entity with specified hibernate criterions and orders.
* Search entity with specified criterions and orders.
*
* @param criterions
* Hibernate criterions to restrict search result. No restrictions will be set
* if pass a null value or empty array.
* Hibernate criterions to be used for search. Use null if no criterions.
* @param orders
* Hibernate orders to order search result. No orders will be set if pass a
* null value or empty array.
* Orders to be used for search. Use null for default order.
* @param firstResult
* First result of the query. Set to 0 if this value should be ignored.
* @param maxResults
* Max number of returned entities. Set to 0 if no limit of number of returned
* entities should be set.
* Max number of returned results. Set to 0 if no limit of the max results should be set.
* @return
* List of matched entities.
* List of entity matching specified criterions in specified orders.
*/
List<T> search(@Nullable Criterion[] criterions, @Nullable Order[] orders, int firstResult, int maxResults);
/**
* Count entity matching specified hibernate criterions.
*
* @param entityClass
* Class of entity to count.
* @param criterias

View File

@ -3,9 +3,9 @@ package com.pmease.commons.persistence.dao;
import java.util.List;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import com.google.inject.Inject;
import com.pmease.commons.persistence.AbstractEntity;
import com.pmease.commons.util.ClassUtils;
@ -30,7 +30,7 @@ public class GenericDaoImpl<T extends AbstractEntity> implements GenericDao<T> {
}
@Override
public T getReference(Long entityId) {
public T load(Long entityId) {
return generalDao.load(entityClass, entityId);
}
@ -50,13 +50,31 @@ public class GenericDaoImpl<T extends AbstractEntity> implements GenericDao<T> {
}
@Override
public List<T> search(Criterion[] criterions, Order[] orders, int firstResult, int maxResults) {
return generalDao.search(entityClass, criterions, orders, firstResult, maxResults);
public int count(Criterion[] criterions) {
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(entityClass);
if (criterions != null) {
for (Criterion criterion: criterions)
detachedCriteria.add(criterion);
}
return generalDao.count(detachedCriteria);
}
@SuppressWarnings("unchecked")
@Override
public int count(Criterion[] criterions) {
return generalDao.count(entityClass, criterions);
public List<T> search(Criterion[] criterions, Order[] orders, int firstResult, int maxResults) {
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(entityClass);
if (criterions != null) {
for (Criterion criterion: criterions)
detachedCriteria.add(criterion);
}
if (orders != null) {
for (Order order: orders)
detachedCriteria.addOrder(order);
}
return (List<T>) generalDao.search(detachedCriteria, firstResult, maxResults);
}
}

View File

@ -5,7 +5,6 @@ import java.util.Collection;
import java.util.Properties;
import javax.inject.Inject;
import javax.inject.Provider;
import org.apache.shiro.authc.credential.PasswordService;
import org.eclipse.jetty.server.Server;
@ -37,14 +36,14 @@ public class Product extends AbstractPlugin {
public static final String PRODUCT_NAME = "Gitop";
private final Provider<GeneralDao> generalDaoProvider;
private final GeneralDao generalDao;
private final PasswordService passwordService;
@Inject
public Product(Provider<GeneralDao> generalDaoProvider, PasswordService passwordService) {
public Product(GeneralDao generalDao, PasswordService passwordService) {
serverProps = FileUtils.loadProperties(new File(Bootstrap.getConfDir(), "server.properties"));
this.generalDaoProvider = generalDaoProvider;
this.generalDao = generalDao;
this.passwordService = passwordService;
}
@ -118,12 +117,12 @@ public class Product extends AbstractPlugin {
public void postStartDependents() {
DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
criteria.add(Restrictions.eq("loginName", "admin"));
if (generalDaoProvider.get().find(criteria) == null) {
if (generalDao.find(criteria) == null) {
User user = new User();
user.setLoginName("admin");
user.setPasswordHash(passwordService.encryptPassword("12345"));
user.setFullName("Administrator");
generalDaoProvider.get().save(user);
generalDao.save(user);
}
logger.info(PRODUCT_NAME + " has been started successfully.");