Basic Extending Generic Classes Question
I'm only just getting started on converting my code to use Generics, and have some questions. Although this is Spring/Hibernate code, the problem just seems to be in extending the classes.
First, here is the GenericDao interface:
publicinterface GenericDao <T, IDextends Serializable>{
void save(T transientObject);
void delete(T persistentObject);
Collection<T> getAll();
T getById(ID id);
int countAll();
}
Here is the HibernateGenericDao class implementation:
publicclass HibernateGenericDao<T, IDextends Serializable>extends HibernateDaoSupportimplements GenericDao<T, ID>{
private Class<T> type;
public HibernateGenericDao(Class<T> type){
this.type = type;
}
/**
* Persist a transient object of type T
*
* @param transientObject
*/
publicvoid save(T transientObject){
try{
getHibernateTemplate().saveOrUpdate(transientObject);
}catch (DataAccessException e){
thrownew DaoException("errors.dao.object-could-not-be-saved", e);
}
}
/**
* "Unpersist" a persistent object of Type T
* @param persistentObject
*/
publicvoid delete(T persistentObject){
try{
getHibernateTemplate().delete(persistentObject);
}catch (DataAccessException e){
thrownew DaoException("errors.dao.object-could-not-be-deleted");
}
}
/**
* Return a collection of all objects of Type T
*
* @return a Collection of all "T" objects
*/
@SuppressWarnings("unchecked")
public Collection<T> getAll(){
String hql ="from " + type.getName();
return getHibernateTemplate().find(hql);
}
/**
* Return an object of type T with the given int id
*
* @param idan int/Integer (yay, autoboxing) representing the unique ID
* @returnan object of type T with the given unique ID, or null
*/
@SuppressWarnings("unchecked")
public T getById(ID id){
return (T) getHibernateTemplate().get(type, id);
}
/**
* Return a count of all of the objects of type T
*
* @return an int representing the count of objects of type T
*/
publicint countAll(){
// This ought to autobox to return an int *fingers crossed*
return (Integer) getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(org.hibernate.Session session)throws org.hibernate.HibernateException, SQLException{
String hql ="SELECT COUNT(*) FROM " + type.getName();
Query query = session.createQuery(hql);
return query.uniqueResult();
}
});
}
}
The problem occurs when I try to extend the DAO for specific objects. Here is the first interface that extends the GenericDao interface (UtcDao):
publicinterface UtcDaoextends GenericDao<UTC, Integer>{
Collection<UTC> getUTCs(int eventId);
}
I think the UtcDao is specified correctly, but I think the problem is in the definition of the UtcDao implementation:
publicclass HibernateUtcDao<UTC, Integer>extends HibernateGenericDao<UTC, Integer>implements UtcDao{
/**
* Returns all UTCs for a given event id
*
* Based on the "getUtcsByEvent" native query
*
* @param eventIdthe int ID of the Event to return the UTCs for
* @return a Collection with all UTCs inspected for a given Event
*/
@SuppressWarnings("unchecked")
public Collection<UTC> getUTCs(int eventId){
return getHibernateTemplate().findByNamedQuery("getUtcsByEvent", eventId);
}
}
I keep getting an error stating there is no default constructor in the GenericDao implementation.
I think I've made a simple mistake... can anyone point it out to me?
Jason

