most people should be familiar with making a Generic DAO for Spring + hibernate. The reference is from here http://www.ibm.com/developerworks/java/library/j-genericdao/index.html but there is an improvement to it on Single DAO & generic CRUD methods (JPA/Hibernate + Spring)
This improvement is the detection of the type as it is part of the superclass, instead of using the constructor to tell which class it is
public GenericDaoJpaImpl() {
ParameterizedType genericSuperclass = (ParameterizedType) getClass()
.getGenericSuperclass();
this.entityClass = (Class<T>) genericSuperclass
.getActualTypeArguments()[0];
}
HOWEVER, this cast will fail with Guice. To be injected, the interface and the class need to be binded in a module like this
bind(TestDao.class).to(TestDaoImpl.class);
And by doing so the constructor trick for our GenericDAO won't work because of the following:
getClass().getGenericSuperclass() = java.lang.Class
getClass().getName() = com.gwtplatform.samples.basic.server.dao.TestDaoImpl$$EnhancerByGuice$$5fe0d6fd
contrary to what a Spring + Hibernate returns
getClass().getGenericSuperclass() = sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl
getClass().getName() = com.gwtplatform.samples.basic.server.dao.TestDaoImpl
I'm just using a super constructor in my extended DAOs now, but still would like to obtain the type instead of providing it, any ideas?