com.banda.serverbase.persistence
Class GenericHibernate4DAO<T,PK extends Serializable>

java.lang.Object
  extended by com.banda.serverbase.persistence.GenericHibernate4DAO<T,PK>
Type Parameters:
T - a type variable
PK - the primary key for that type
All Implemented Interfaces:
GenericDAO<T,PK>

public class GenericHibernate4DAO<T,PK extends Serializable>
extends Object
implements GenericDAO<T,PK>

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.

Since:
2012
Author:
© Peter Banda

Field Summary
protected  org.apache.commons.logging.Log log
          Log variable for all child classes.
 
Constructor Summary
protected GenericHibernate4DAO(Class<? extends T> persistentClass)
          Constructor that takes in a class to see which type of entity to persist
 
Method Summary
 void clear()
          Clears session.
protected static
<T,PK extends Serializable>
GenericHibernate4DAO<T,PK>
createDaoInstance(Class<T> clazz)
          For Spring configuration purpose only.
 void evict(T object)
          Evicts given object from session.
 boolean exists(PK id)
          Checks for existence of an object with given key.
 void flush()
          Flushes session.
 Collection<T> get(Collection<PK> ids)
          Generic method to get objects based on class and identifiers.
 T get(PK id)
          Generic method to get an object based on class and identifier.
 List<T> getAll()
          Generic method used to get all objects of a particular type.
 List<T> getByExample(T object)
          Gets objects from the database that match given example.
 T getByUniqueExample(T object)
          Gets object from the database that match given example.
 PK getKey(T object)
          Generic method to get the identifier of a given object.
 T getLast()
          Generic method to get the last object.
 PK getNextPK(PK id)
          Get primary key larger than given one
 PK getPreviousPK(PK id)
          Get primary key smaller than given one
 org.hibernate.SessionFactory getSessionFactory()
           
 void remove(Collection<T> objects)
          Generic method to delete a given object
 void remove(PK id)
          Generic method to delete an object with given id
 void remove(T object)
          Generic method to delete a given object
<C extends Collection<T>>
C
save(C objects)
          Generic method to save collection of objects - handles insert, update and remove.
 T save(T object)
          Generic method to save an object - handles insert, update and remove.
 void setKey(T object, PK key)
          Generic method to set the identifier for a given object.
 void setSessionFactory(org.hibernate.SessionFactory sessionFactory)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected final org.apache.commons.logging.Log log
Log variable for all child classes. Uses LogFactory.getLog(getClass()) from Commons Logging

Constructor Detail

GenericHibernate4DAO

protected GenericHibernate4DAO(Class<? extends T> persistentClass)
Constructor that takes in a class to see which type of entity to persist

Parameters:
persistentClass - the class type you'd like to persist
keyExtractor -
Method Detail

getAll

public List<T> getAll()
Generic method used to get all objects of a particular type.

Specified by:
getAll in interface GenericDAO<T,PK extends Serializable>
Returns:
List of populated objects

get

public T get(PK id)
Description copied from interface: GenericDAO
Generic method to get an object based on class and identifier.

Specified by:
get in interface GenericDAO<T,PK extends Serializable>
Parameters:
id - The identifier (primary key) of the object to get
Returns:
The populated object with given key
See Also:
com.pge.smartgrid.serverbase.dao.GenericDAO#get(java.io.Serializable)

get

public Collection<T> get(Collection<PK> ids)
Description copied from interface: GenericDAO
Generic method to get objects based on class and identifiers.

Specified by:
get in interface GenericDAO<T,PK extends Serializable>
Parameters:
ids - The identifiers (primary keys) of the objects to get
Returns:
The populated objects with given keys
See Also:
com.pge.smartgrid.serverbase.dao.GenericDAO#get(Collection)

getByExample

public List<T> getByExample(T object)
Description copied from interface: GenericDAO
Gets objects from the database that match given example.

Specified by:
getByExample in interface GenericDAO<T,PK extends Serializable>
Parameters:
object - The template object to match (not null columns)
Returns:
The loaded objects matching given template from the database
See Also:
com.pge.smartgrid.serverbase.dao.GenericDAO#getByExample(java.lang.Object)

getByUniqueExample

public T getByUniqueExample(T object)
Description copied from interface: GenericDAO
Gets object from the database that match given example. There is maximum single object matching given (unique) criteria.

Specified by:
getByUniqueExample in interface GenericDAO<T,PK extends Serializable>
Parameters:
object - The template object to match (not null columns)
Returns:
The loaded object matching given template from the database (if any)
See Also:
com.pge.smartgrid.serverbase.dao.GenericDAO#getByExample(java.lang.Object)

exists

public boolean exists(PK id)
Description copied from interface: GenericDAO
Checks for existence of an object with given key.

Specified by:
exists in interface GenericDAO<T,PK extends Serializable>
Parameters:
id - The id of the entity
Returns:
The result of the test
See Also:
com.pge.smartgrid.serverbase.dao.GenericDAO#exists(java.io.Serializable)

save

public T save(T object)
Description copied from interface: GenericDAO
Generic method to save an object - handles insert, update and remove.

Specified by:
save in interface GenericDAO<T,PK extends Serializable>
Parameters:
object - The object to save
Returns:
The persisted object (if not removed)
See Also:
com.pge.smartgrid.serverbase.dao.GenericDAO#save(java.lang.Object)

remove

public void remove(T object)
Description copied from interface: GenericDAO
Generic method to delete a given object

Specified by:
remove in interface GenericDAO<T,PK extends Serializable>
Parameters:
object - The object to remove
See Also:
com.pge.smartgrid.serverbase.dao.GenericDAO#remove(java.lang.Object)

remove

public void remove(Collection<T> objects)
Description copied from interface: GenericDAO
Generic method to delete a given object

Specified by:
remove in interface GenericDAO<T,PK extends Serializable>
See Also:
com.pge.smartgrid.serverbase.dao.GenericDAO#remove(java.util.Collection)

remove

public void remove(PK id)
Description copied from interface: GenericDAO
Generic method to delete an object with given id

Specified by:
remove in interface GenericDAO<T,PK extends Serializable>
Parameters:
id - The identifier (primary key) of the object to remove
See Also:
com.pge.smartgrid.serverbase.dao.GenericDAO#remove(java.io.Serializable)

save

public <C extends Collection<T>> C save(C objects)
Description copied from interface: GenericDAO
Generic method to save collection of objects - handles insert, update and remove.

Specified by:
save in interface GenericDAO<T,PK extends Serializable>
Type Parameters:
C - Type of collection
Parameters:
objects - The objects to save
See Also:
com.pge.smartgrid.serverbase.dao.GenericDAO#save(java.util.Collection)

clear

public void clear()
Description copied from interface: GenericDAO
Clears session.

Specified by:
clear in interface GenericDAO<T,PK extends Serializable>
See Also:
com.pge.smartgrid.serverbase.dao.GenericDAO#clear()

flush

public void flush()
Description copied from interface: GenericDAO
Flushes session.

Specified by:
flush in interface GenericDAO<T,PK extends Serializable>
See Also:
com.pge.smartgrid.serverbase.dao.GenericDAO#flush()

evict

public void evict(T object)
Description copied from interface: GenericDAO
Evicts given object from session.

Specified by:
evict in interface GenericDAO<T,PK extends Serializable>
See Also:
com.pge.smartgrid.serverbase.dao.GenericDAO#evict(java.lang.Object)

getKey

public PK getKey(T object)
Description copied from interface: GenericDAO
Generic method to get the identifier of a given object.

Specified by:
getKey in interface GenericDAO<T,PK extends Serializable>
Parameters:
object - The object to get PK for
Returns:
The identifier (primary key) of a given object
See Also:
com.pge.smartgrid.serverbase.dao.GenericDAO#getKey(java.lang.Object)

setKey

public void setKey(T object,
                   PK key)
Description copied from interface: GenericDAO
Generic method to set the identifier for a given object.

Specified by:
setKey in interface GenericDAO<T,PK extends Serializable>
Parameters:
object - The object to get PK for
key - The identifier (primary key) of a given object
See Also:
com.pge.smartgrid.serverbase.dao.GenericDAO#setKey(java.lang.Object, java.io.Serializable)

getLast

public T getLast()
Description copied from interface: GenericDAO
Generic method to get the last object.

Specified by:
getLast in interface GenericDAO<T,PK extends Serializable>
Returns:
The object with maximal id

getNextPK

public PK getNextPK(PK id)
Description copied from interface: GenericDAO
Get primary key larger than given one

Specified by:
getNextPK in interface GenericDAO<T,PK extends Serializable>
Returns:
The next primary key

getPreviousPK

public PK getPreviousPK(PK id)
Description copied from interface: GenericDAO
Get primary key smaller than given one

Specified by:
getPreviousPK in interface GenericDAO<T,PK extends Serializable>
Returns:
The previous primary key

getSessionFactory

public org.hibernate.SessionFactory getSessionFactory()

setSessionFactory

public void setSessionFactory(org.hibernate.SessionFactory sessionFactory)

createDaoInstance

protected static <T,PK extends Serializable> GenericHibernate4DAO<T,PK> createDaoInstance(Class<T> clazz)
For Spring configuration purpose only. DO NOT USE IT DIRECTLY!!!

Parameters:
clazz -
Returns:


Copyright © 2013. All Rights Reserved.