So here is my problem. I have two beans (one extends the other), and I have one DAO class for each to access their tables (using Hibernate). In most cases the queries will be identical so I also have an abstract DAO class (extended by the other two DAOs) which contains the majority of the queries. I had the methods returning beans of the super class which, if need be, I would cast to the subclass. To avoid this casting I want to switch to generics. I changed over the abstract class with no compiliation erros, but I've hit a bump. In the two beans I have a static String field "entityName " which I access to for the Hibernate calls (Bean.entityNam e). When I converted to generics the reference changed to T.entityName which now always references the super class. What I need is to access whichever class' entityName I happen to be using.
Example:
No matter what I set as AbstractBeanDAO <type> it always references SuperBean.entit yName. There are times I need SubBean.entityN ame so I can access the correct table. How can I do this without having to duplicate code in the two DAO subclasses?
Example:
Code:
class AbstractBeanDAO <T extends SuperBean> { public T get(Integer id){ // this where I have the problem Criteria criteria = session.createCriteria(T.entityName); // set some criteria return criteria.uniqueResult(); } }
Comment