java - HibernateTemplate query with list -
hibernate beginner here, basic question.
say have 2 hibernate objects: employee , department in one-to-many relationship. 1 department can have many employees.
i have list of department objects , want to retrieve employees in departments. example, in sql it's easy:
select * employee employee.deptid in (1, 2, 6, 19); what correct , proper way in hibernatetemplate find? example i've seen seems hopelessly verbose , involves creating list of departmentids , overriding executefind(). can directly using list<department> instead of creating list of object ids?
//verbose example list employees = gethibernatetemplate().executefind(new hibernatecallback<list>() { @override public list doinhibernate(session session) throws hibernateexception, sqlexception { query query = session.createquery( "select employee" + "from employee e " + "where e.deptid in (:ids) " ); query.setparameterlist("ids", ids); return query.list(); } });
assuming have department property in employee can run query:
list<departement> departments = ... list employees = gethibernatetemplate().executefind(new hibernatecallback<list>() { @override public list doinhibernate(session session) throws hibernateexception, sqlexception { query query = session.createquery( "select employee" + "from employee e " + "where e.department in (:departments) " ); query.setparameterlist("departments", departments); return query.list(); } });
Comments
Post a Comment