日期:2014-05-19 浏览次数:20665 次
@Entity public class Employee implements Serializable{ private Set<Department> departments = new HashSet<Department>();//所属部门 @ManyToMany(cascade=CascadeType.REFRESH, fetch=FetchType.EAGER) @JoinTable(name="employee_department", joinColumns=@JoinColumn(name="username"), inverseJoinColumns=@JoinColumn(name="department_id")) public Set<Department> getDepartments() { return departments; } public void setDepartments(Set<Department> departments) { this.departments = departments; } public void addDepartment(Department department){ if(!this.departments.contains(department)) this.departments.add(department); } public void removeDepartment(Department department){ if(this.departments.contains(department)) this.departments.remove(department); } }
@Entity public class Department implements Serializable{ private Set<Employee> employees = new HashSet<Employee>();//员工 @ManyToMany(mappedBy="departments", cascade=CascadeType.REFRESH) public Set<Employee> getEmployees() { return employees; } public void setEmployees(Set<Employee> employees) { this.employees = employees; }
public String list(){ StringBuffer hql = new StringBuffer(); hql.append("o.visible=?"); List pr = new ArrayList(); pr.add(true); if("true".equals(ef.getQuery())) { if(ef.getDepartment()!=null && !"".equals(ef.getDepartment())) { System.out.println(ef.getDepartment()); hql.append(" and o.departments in (select d from Department d where id =?)");//问题是这句hql怎么写 pr.add(ef.getDepartment());//ef.getDepartment()接收回来的部门id,初始化问号 } } pl = new PageList<Employee>(Integer.valueOf(ReadMessageConnection.getMessageConnection("employeePageSize")), ef.getFirstIndex()); pl.setQueryResult(employeeManager.getScrollData(Employee.class, pl.getFirstindex(), pl .getMaxresult(), hql.toString(), pr.toArray())); return "list"; }