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

org.appfuse.dao.hibernate.GenericDaoHibernate Maven / Gradle / Ivy

There is a newer version: 3.5.0
Show newest version
package org.appfuse.dao.hibernate;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.appfuse.dao.GenericDao;
import org.appfuse.dao.SearchException;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.orm.ObjectRetrievalFailureException;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.util.Version;
import org.hibernate.HibernateException;
import org.hibernate.IdentifierLoadAccess;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;

/**
 * This class serves as the Base class for all other DAOs - namely to hold
 * common CRUD methods that they might all use. You should only need to extend
 * this class when your require custom CRUD logic.
 * 

*

To register this class in your Spring context file, use the following XML. *

 *      <bean id="fooDao" class="org.appfuse.dao.hibernate.GenericDaoHibernate">
 *          <constructor-arg value="org.appfuse.model.Foo"/>
 *      </bean>
 * 
* * @author Bryan Noll * Updated by jgarcia: update hibernate3 to hibernate4 * @author jgarcia (update: added full text search + reindexing) * @param a type variable * @param the primary key for that type */ public class GenericDaoHibernate implements GenericDao { /** * Log variable for all child classes. Uses LogFactory.getLog(getClass()) from Commons Logging */ protected final Log log = LogFactory.getLog(getClass()); private Class persistentClass; @Resource private SessionFactory sessionFactory; private Analyzer defaultAnalyzer; /** * Constructor that takes in a class to see which type of entity to persist. * Use this constructor when subclassing. * * @param persistentClass the class type you'd like to persist */ public GenericDaoHibernate(final Class persistentClass) { this.persistentClass = persistentClass; defaultAnalyzer = new StandardAnalyzer(Version.LUCENE_35); } /** * Constructor that takes in a class and sessionFactory for easy creation of DAO. * * @param persistentClass the class type you'd like to persist * @param sessionFactory the pre-configured Hibernate SessionFactory */ public GenericDaoHibernate(final Class persistentClass, SessionFactory sessionFactory) { this.persistentClass = persistentClass; this.sessionFactory = sessionFactory; defaultAnalyzer = new StandardAnalyzer(Version.LUCENE_35); } public SessionFactory getSessionFactory() { return this.sessionFactory; } public Session getSession() throws HibernateException { Session sess = getSessionFactory().getCurrentSession(); if (sess == null) { sess = getSessionFactory().openSession(); } return sess; } @Autowired @Required public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } /** * {@inheritDoc} */ @SuppressWarnings("unchecked") public List getAll() { Session sess = getSession(); return sess.createCriteria(persistentClass).list(); } /** * {@inheritDoc} */ @SuppressWarnings("unchecked") public List getAllDistinct() { Collection result = new LinkedHashSet(getAll()); return new ArrayList(result); } /** * {@inheritDoc} */ public List search(String searchTerm) throws SearchException { Session sess = getSession(); FullTextSession txtSession = Search.getFullTextSession(sess); org.apache.lucene.search.Query qry; try { qry = HibernateSearchTools.generateQuery(searchTerm, this.persistentClass, sess, defaultAnalyzer); } catch (ParseException ex) { throw new SearchException(ex); } org.hibernate.search.FullTextQuery hibQuery = txtSession.createFullTextQuery(qry, this.persistentClass); return hibQuery.list(); } /** * {@inheritDoc} */ @SuppressWarnings("unchecked") public T get(PK id) { Session sess = getSession(); IdentifierLoadAccess byId = sess.byId(persistentClass); T entity = (T) byId.load(id); if (entity == null) { log.warn("Uh oh, '" + this.persistentClass + "' object with id '" + id + "' not found..."); throw new ObjectRetrievalFailureException(this.persistentClass, id); } return entity; } /** * {@inheritDoc} */ @SuppressWarnings("unchecked") public boolean exists(PK id) { Session sess = getSession(); IdentifierLoadAccess byId = sess.byId(persistentClass); T entity = (T) byId.load(id); return entity != null; } /** * {@inheritDoc} */ @SuppressWarnings("unchecked") public T save(T object) { Session sess = getSession(); return (T) sess.merge(object); } /** * {@inheritDoc} */ public void remove(T object) { Session sess = getSession(); sess.delete(object); } /** * {@inheritDoc} */ public void remove(PK id) { Session sess = getSession(); IdentifierLoadAccess byId = sess.byId(persistentClass); T entity = (T) byId.load(id); sess.delete(entity); } /** * {@inheritDoc} */ @SuppressWarnings("unchecked") public List findByNamedQuery(String queryName, Map queryParams) { Session sess = getSession(); Query namedQuery = sess.getNamedQuery(queryName); for (String s : queryParams.keySet()) { namedQuery.setParameter(s, queryParams.get(s)); } return namedQuery.list(); } /** * {@inheritDoc} */ public void reindex() { HibernateSearchTools.reindex(persistentClass, getSessionFactory().getCurrentSession()); } /** * {@inheritDoc} */ public void reindexAll(boolean async) { HibernateSearchTools.reindexAll(async, getSessionFactory().getCurrentSession()); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy