Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Hibernate GenericDAO using Class-Mate
class Device {}
class Router extends Device {}
class GenericDAO<T, ID extends Serializable> {
protected Class<T> persistentClass;
protected Class<ID> idClass;
private GenericDAO() {
List <ResolvedType> typeParameters = new TypeResolver().resolve(this.getClass()).typeParametersFor(GenericDAO.class);
this.persistentClass = (Class<T>) typeParameters.get(0).getErasedType();
this.idClass = (Class<ID>) typeParameters.get(1).getErasedType();
}
}
class DeviceDAO<T extends Device> extends GenericDAO<T, Long> {}
class RouterDAO extends DeviceDAO<Router> {}
@Test
public void resolvesEntityType () {
RouterDAO routerDAO = new RouterDAO();
assert routerDAO.persistentClass == Router.class;
assert routerDAO.idClass == Long.class;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.