1

我有Employee , Role , Department

  • Employee , Role多对多的关系。
  • Employee , Department多对一的关系。

    @ManyToOne(cascade = CascadeType.ALL)
        @JoinColumn(name = "fk_department_id")
        private Department department;
    
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "employee_role", joinColumns = { @JoinColumn(name = "employee_id") }, inverseJoinColumns = { @JoinColumn(name = "role_id") })
    private Set<Role> roles = new HashSet<Role>(0);
    

-要求:获取所有角色为 admin的用户:

所以这就是我想要做的:

List<Employee> employees = getCurrentSession()
                .createSQLQuery(
                        "select"
                                + e.id as id,e.first_name as firstName,e.password as password
                                + "from employee e,employee_role er,role r where e.employee_id=er.employee_id and er.role_id=r.role_id and r.name='ROLE_ADMIN' ")
                .setResultTransformer(Transformers.aliasToBean(Employee.class))
                .list();

问题:我可以像上面的查询一样轻松获取所有属性,除了部门属性(员工表中有列 fk_department_id)和角色属性(有一个连接表employee_role),任何想法如何通过查询获取它们并设置它们在 Bean 中使用ResultTransFormer还是我必须在单独的查询中执行此操作并分别设置每个属性?

4

0 回答 0