Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package io.dropwizard.hibernate;
  
  import org.hibernate.*;
  
  import java.util.List;
  
  import static com.google.common.base.Preconditions.checkNotNull;

An abstract base class for Hibernate DAO classes.

Parameters:
<E> the class which this DAO manages
 
 public class AbstractDAO<E> {
     private final SessionFactory sessionFactory;
     private final Class<?> entityClass;

    
Creates a new DAO with a given session provider.

Parameters:
sessionFactory a session provider
 
     public AbstractDAO(SessionFactory sessionFactory) {
         this. = checkNotNull(sessionFactory);
         this. = Generics.getTypeParameter(getClass());
     }

    
Returns the current org.hibernate.Session.

Returns:
the current session
 
     protected Session currentSession() {
         return .getCurrentSession();
     }

    
 
     protected Criteria criteria() {
         return currentSession().createCriteria();
     }

    
Returns a named org.hibernate.Query.

Parameters:
queryName the name of the query
Returns:
the named query
See also:
org.hibernate.SharedSessionContract.getNamedQuery(java.lang.String)
 
     protected Query namedQuery(String queryNamethrows HibernateException {
         return currentSession().getNamedQuery(checkNotNull(queryName));
     }

    
Returns the entity class managed by this DAO.

Returns:
the entity class managed by this DAO
 
     @SuppressWarnings("unchecked")
     public Class<E> getEntityClass() {
         return (Class<E>) ;
     }

    
Convenience method to return a single instance that matches the criteria, or null if the criteria returns no results.

Parameters:
criteria the org.hibernate.Criteria query to run
Returns:
the single result or null
Throws:
org.hibernate.HibernateException if there is more than one matching result
See also:
org.hibernate.Criteria.uniqueResult()
 
     @SuppressWarnings("unchecked")
     protected E uniqueResult(Criteria criteriathrows HibernateException {
         return (E) checkNotNull(criteria).uniqueResult();
     }

    
Convenience method to return a single instance that matches the query, or null if the query returns no results.

Parameters:
query the query to run
Returns:
the single result or null
Throws:
org.hibernate.HibernateException if there is more than one matching result
See also:
org.hibernate.Query.uniqueResult()
 
     @SuppressWarnings("unchecked")
     protected E uniqueResult(Query querythrows HibernateException {
         return (E) checkNotNull(query).uniqueResult();
     }

    
Get the results of a org.hibernate.Criteria query.

Parameters:
criteria the org.hibernate.Criteria query to run
Returns:
the list of matched query results
See also:
org.hibernate.Criteria.list()
    @SuppressWarnings("unchecked")
    protected List<E> list(Criteria criteriathrows HibernateException {
        return checkNotNull(criteria).list();
    }

    
Get the results of a query.

Parameters:
query the query to run
Returns:
the list of matched query results
See also:
org.hibernate.Query.list()
    @SuppressWarnings("unchecked")
    protected List<E> list(Query querythrows HibernateException {
        return checkNotNull(query).list();
    }

    
Return the persistent instance of <E> with the given identifier, or null if there is no such persistent instance. (If the instance, or a proxy for the instance, is already associated with the session, return that instance or proxy.)

Parameters:
id an identifier
Returns:
a persistent instance or null
Throws:
org.hibernate.HibernateException
See also:
org.hibernate.Session.get(java.lang.Class,java.io.Serializable)
    @SuppressWarnings("unchecked")
    protected E get(Serializable id) {
        return (E) currentSession().get(checkNotNull(id));
    }

    
Either save or update the given instance, depending upon resolution of the unsaved-value checks (see the manual for discussion of unsaved-value checking).

This operation cascades to associated instances if the association is mapped with cascade="save-update".

Parameters:
entity a transient or detached instance containing new or updated state
Throws:
org.hibernate.HibernateException
See also:
org.hibernate.Session.saveOrUpdate(java.lang.Object)
    protected E persist(E entitythrows HibernateException {
        currentSession().saveOrUpdate(checkNotNull(entity));
        return entity;
    }

    
Force initialization of a proxy or persistent collection.

Note: This only ensures initialization of a proxy object or collection; it is not guaranteed that the elements INSIDE the collection will be initialized/materialized.

Parameters:
proxy a persistable object, proxy, persistent collection or null
Throws:
org.hibernate.HibernateException if we can't initialize the proxy at this time, eg. the org.hibernate.Session was closed
    protected <T> T initialize(T proxythrows HibernateException {
        if (!Hibernate.isInitialized(proxy)) {
            Hibernate.initialize(proxy);
        }
        return proxy;
    }
New to GrepCode? Check out our FAQ X