使用反射重写java方法

使用反射重写java方法,java,hibernate,reflection,Java,Hibernate,Reflection,我试图重写下面的java方法,该方法返回一个对象列表(hibenrate域对象),使其更通用,只需编写一次,就可以将任何对象传递给它 public List<GsCountry> getCountry() { Session session = hibernateUtil.getSessionFactory().openSession(); Transaction tx = session.beginTransaction(); tx.begin();

我试图重写下面的java方法,该方法返回一个对象列表(hibenrate域对象),使其更通用,只需编写一次,就可以将任何对象传递给它

public List<GsCountry> getCountry() {   
    Session session = hibernateUtil.getSessionFactory().openSession();
    Transaction tx = session.beginTransaction();
    tx.begin();
    List<GsCountry> countryList = new ArrayList<GsCountry>();
    Query query = session.createQuery("from GsCountry");
    countryList = (List<GsCountry>) query.list();
    return countryList;
}
public List getCountry(){
Session Session=hibernateUtil.getSessionFactory().openSession();
事务tx=会话.beginTransaction();
tx.begin();
List countryList=new ArrayList();
Query=session.createQuery(“来自GsCountry”);
countryList=(List)query.List();
返回国家列表;
}
如何才能自由返回作为参数传递的类型列表?

//使方法名更通用
//making the method name more generic
public List<E> getData() {   
    Session session = hibernateUtil.getSessionFactory().openSession();
    Transaction tx = session.beginTransaction();
    tx.begin();
    List<E> result = new ArrayList<E>();

    // try to add a model final static field which could retrieve the 
    // correct value of the model. 
    Query query = session.createQuery("from " + E.model);
    result = (List<E>) query.list();
    return result;
}
公共列表getData(){ Session Session=hibernateUtil.getSessionFactory().openSession(); 事务tx=会话.beginTransaction(); tx.begin(); 列表结果=新建ArrayList(); //尝试添加一个模型最终静态字段,该字段可以检索 //模型的正确值。 Query=session.createQuery(“from”+E.model); result=(List)query.List(); 返回结果; }
这是一个代码示例,从中,您会发现它很有用

public class GenericDaoHibernateImpl <T, PK extends Serializable>
    implements GenericDao<T, PK>, FinderExecutor {
    private Class<T> type;

    public GenericDaoHibernateImpl(Class<T> type) {
        this.type = type;
    }

    public PK create(T o) {
        return (PK) getSession().save(o);
    }

    public T read(PK id) {
        return (T) getSession().get(type, id);
    }

    public void update(T o) {
        getSession().update(o);
    }

    public void delete(T o) {
        getSession().delete(o);
    }

    // Not showing implementations of getSession() and setSessionFactory()
}
公共类GenericDaoHibernateImpl
实现GenericDao、FinderExecutor{
私人阶级类型;
公共GenericDaoHibernateImpl(类类型){
this.type=type;
}
公共PK创建(TO){
返回(PK)getSession().save(o);
}
公共T读(主键id){
return(T)getSession().get(type,id);
}
公开作废更新(TO){
getSession().update(o);
}
公共作废删除(TO){
getSession()。删除(o);
}
//未显示getSession()和setSessionFactory()的实现
}
很好,但缺少两个细节

a) 需要一个类参数来实现泛型返回类型
b) 没有像
E.model
这样的构造,而是使用
clazz.getSimpleName()

公共列表getData(类clazz){
//在此处插入Jinesh Parekh的答案
//但是用clazz.getSimpleName()替换E.model
}

如果您想获得E.model,则需要添加类型为
Class
的参数。由于类型擦除,单独使用E对您没有帮助。
E.model
不起作用,除非
E
被声明为
E扩展了SomeClass
,其中
SomeClass
声明了一个公共
model
属性。当我看到没有使用此模式时,我总是感到惊讶:-)(+1)
public List<E> getData(Class<E> clazz) {
    // insert Jinesh Parekh's answer here
    // but replace E.model with clazz.getSimpleName()
}