以下代码是由hibernate 3开发的。我想将其迁移到hibernate 5. getHibernateTemplate()和HibernateCallback显示错误如何修复此错误。
public List<Employee> getAllSalaryPersons () {
List<Employee> empSalList= super.getHibernateTemplate().execute(new HibernateCallback<List<Employee>>(){
public List<Employee> doInHibernate(Session session) throws HibernateException {
Criteria criteria = session.createCriteria(Salary.class, "sclass")
.createAlias("salary", "salary")
.add(Restrictions.le("sclass.salary", 25000))
.add(Restrictions.gt("sclass.salary", 99000))
.setProjection(
Projections.projectionList().addOrder( Order.asc("sclass.experiance") );
criteria.setResultTransformer( new AliasToBeanResultTransformer(Employee.class));
return criteria.list();
}
});
return empSalList;
}
答案 0 :(得分:0)
我会完全避免HibernateTemplate
(其包名称随每个主要的Hibernate版本而变化)而是使用SessionFactory
。
这样,当您最终升级到Hibernate 6或7时,您不必再次更改代码......
@Autowired
private SessionFactory sessionFactory;
public List<Employee> getAllSalaryPersons () {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Salary.class, "sclass")
.createAlias("salary", "salary")
.add(Restrictions.le("sclass.salary", 25000))
.add(Restrictions.gt("sclass.salary", 99000))
.setProjection(Projections.projectionList())
.addOrder(Order.asc("sclass.experiance")) ;
criteria.setResultTransformer( new AliasToBeanResultTransformer(Employee.class));
return criteria.list();
}