I have generic class, and I want to create a List or cast by List. I only have the class type. What I want to get is:
public class CustomerDao extends BaseDao<Customer> implements ICustomerDao {
private Class<Customer> entityClass;
public CustomerDao(Class<Customer> entityClass) {
super(entityClass);
this.entityClass = entityClass;
}
public void getCustomers() {
List<***> list = (List<***>)getSessionFactory().getCurrentSession().createCriteria(entityClass).list();
}
}
Now how can I use the field entityClass to create the List<***> list? Is it possible?
I can use List<Customer> list = getSessionFactory().getCurrentSession().createCriteria(entityClass).list();, but I am want to learn different way.
Seems you are trying to implement a type-specific DAO (for your Customer entity), derived from an abstract, generic Base DAO. If indeed that is what you are trying to accomplish then there is no need for generics at all:
Its been awhile since I used the Hibernate API’s, but I dont think you need the typecast to
List<Customer>either.