All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sakaiproject.genericdao.hibernate.HibernateGeneralGenericDao Maven / Gradle / Ivy

Go to download

Generic Dao is a Java package which allows a developer to skip writing DAOs for their persistence objects when they are using Spring and/or Hibernate. The package was originally created by Aaron Zeckoski for the Evaluation System project but was repackaged to make it distributable by request. It is used in the RSF framework (http://www2.caret.cam.ac.uk/rsfwiki/). Note about the BeanUtils provided dependency: BeanUtils is not required if you are not using it in your project. Note about the Hibernate provided dependency: Hibernate is not required if you are not using it in your project.

The newest version!
/******************************************************************************
 * HibernateGeneralGenericDao.java - created by [email protected]
 * 
 * Copyright (c) 2006, 2007, 2008
 * Licensed under the Apache License, Version 2
 * 
 * A copy of the Apache License, Version 2 has been included in this 
 * distribution and is available at: http://www.apache.org/licenses/LICENSE-2.0.txt
 * 
 * Contributors:
 * Aaron Zeckoski ([email protected]) - primary
 * 
 *****************************************************************************/

package org.sakaiproject.genericdao.hibernate;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.criterion.DetachedCriteria;
import org.sakaiproject.genericdao.api.GeneralGenericDao;

/**
 * A Hibernate (http://hibernate.org/) based implementation of GeneralGenericDao
 * which can be extended to add more specialized DAO methods.
 * 

* See the overview for installation/usage tips. * * @author Aaron Zeckoski ([email protected]) */ public class HibernateGeneralGenericDao extends HibernateBasicGenericDao implements GeneralGenericDao { // public void deleteSet(Set entities) { // checkEntitySet(entities); // // TODO - reattach non-persistent objects // getHibernateTemplate().deleteAll(entities); // } @SuppressWarnings("unchecked") public List findAll(Class entityClass, int firstResult, int maxResults) { DetachedCriteria criteria = DetachedCriteria.forClass(checkClass(entityClass)); List items = (List) getHibernateTemplate().findByCriteria(criteria, firstResult, maxResults); return items; } // OVERRIDES /** * MUST override this method */ protected int baseCountAll(Class type) { return count(START_QUERY + " " + checkClass(type).getName()); } /** * MUST override this method */ protected int baseSaveSet(Class type, Set entities) { for (T t : entities) { getHibernateTemplate().saveOrUpdate(t); } return entities.size(); } /** * MUST override this method */ protected int baseDeleteSet(Class type, Serializable[] ids) { Set entities = new HashSet(); for (int i = 0; i < ids.length; i++) { Object object = baseFindById(type, ids[i]); if (object != null) { entities.add(object); } } for (Object object : entities) { getHibernateTemplate().delete(object); } return entities.size(); /** This will not flush the item from the session so it is hopeless -AZ StringBuilder sb = new StringBuilder(); for (int i = 0; i < ids.length; i++) { Object id = ids[i]; if (id != null) { if (i > 0) { sb.append(','); } sb.append('?'); } } String hql = "delete from "+type.getName()+" entity where entity.id in (" + sb + ")"; int deletes = getHibernateTemplate().bulkUpdate(hql, ids); return deletes; ***/ } // COMMON CODE public List findAll(Class type) { return findAll(type, 0, 0); } public int countAll(Class type) { checkClass(type); int count = 0; // check the cache first boolean usedCache = false; String searchCacheName = getSearchCacheName(type); String cacheKey = "countAll::" + type.getName(); if (getCacheProvider().exists(searchCacheName, cacheKey)) { Integer iCount = (Integer) getCacheProvider().get(searchCacheName, cacheKey); if (iCount != null) { count = iCount.intValue(); usedCache = true; } } if (! usedCache) { count = baseCountAll(type); // cache the id results for the search getCacheProvider().put(searchCacheName, cacheKey, Integer.valueOf(count)); } return count; } public void deleteSet(Class type, Serializable[] ids) { checkClass(type); if (ids.length > 0) { String operation = "deleteSet"; beforeWrite(operation, type, ids, null); int changes = baseDeleteSet(type, ids); afterWrite(operation, type, ids, null, changes); // clear all removed items from the cache String cacheName = getCacheName(type); for (int i = 0; i < ids.length; i++) { if (ids[i] != null) { String key = ids[i].toString(); getCacheProvider().remove(cacheName, key); } } // clear the search caches getCacheProvider().clear(getSearchCacheName(type)); } } public void saveSet(Set entities) { if (entities == null || entities.isEmpty()) { System.out.println("WARN: Empty list of entities for saveSet, nothing to do..."); } else { Class type = checkEntitySet(entities); String operation = "saveSet"; beforeWrite(operation, type, null, entities.toArray()); int changes = baseSaveSet(type, entities); afterWrite(operation, type, null, entities.toArray(), changes); // clear all saved items from the cache String cacheName = getCacheName(type); for (T t : entities) { Object id = baseGetIdValue(t); if (id != null) { String key = id.toString(); getCacheProvider().remove(cacheName, key); } } // clear the search caches getCacheProvider().clear(getSearchCacheName(type)); } } public void deleteSet(Set entities) { if (entities.size() > 0) { Class type = checkEntitySet(entities); List ids = new ArrayList(); for (T t : entities) { Object id = baseGetIdValue(t); if (id != null) { ids.add(id); } } deleteSet(type, ids.toArray(new Serializable[ids.size()])); } } @SuppressWarnings("unchecked") public void saveMixedSet(Set[] entitySets) { for (int i=0; i checkEntitySet(Set entities) { Class entityClass = null; Iterator it = entities.iterator(); while(it.hasNext()) { Object entity = it.next(); if (entityClass == null) { entityClass = (Class) findClass(entity); } if (! checkClass(entityClass).isInstance(entity)) { throw new IllegalArgumentException("Entity set item " + entity.toString() + " is not of type: " + entityClass + ", the type is: " + entity.getClass() + " (All items must be of consistent persistent type)"); } } return entityClass; } }