我可以这样使用Hibernate吗?

public class Employee{
  private String name;
  private String age;
  //public getters and setters
}

public class EmployeeDao{
  public List<Employee> getByFilter(Employee filter){
    //what whould I write here?
  }
}

public class Test{
  public static void main(String[] args){
    Employee employeeFilter = new Employee();
    employeeFilter.setAge(15);
    List<Employee> employeesOf15 = new EmployeeDao().getByFilter(employeeFilter);
  }
}

如果是,那怎么办?

===============>>#1 票数:0

是的,在您的getByFilter()方法中,您可以编写一个简单的HQL或本机SQL查询来查找所有年龄等于过滤器年龄的雇员,或使用Hibernate的Criteria Queries (例如:

return getSession().createCriteria(Employee.class).add(Restrictions.eq("age", filter.getAge())).list();

最好在通用的getByFilter()方法中添加另一个参数,以便您可以按任意字段(名称,年龄等)指定过滤条件,然后采取相应措施。

  ask by Mikhail Kopylov translate from so

未解决问题?本站智能推荐: