链接的Entity类中的JPQL连接条件

时间:2015-05-21 10:07:36

标签: java jpa jpql

我正在使用以下SQL加入员工,user_projects和project_master

SELECT DISTINCT usr.user_number,
                  emp.emp_name
    FROM employees emp
         LEFT JOIN user_projects usr
            ON (emp.user_number = usr.user_number)
         JOIN project_master mast
            ON (usr.project_id = mast.project_id)
   WHERE     mast.active = 'Y'
         AND emp.user_number = 'SMITH'

在Employee实体中,我将以下JPQL定义为namedQuery

@NamedQuery(name = "Employee.findProjects", query = " select DISTINCT u.userNumber,e.empName" +
" from Employee e LEFT JOIN e.userProjectsList u  where e.userNumber='SMITH' ") 

不确定如何链接UserProjects和ProjectMaster以创建where条件 mast.active = 'Y' in Employee entity's findProjects namedQuery 如何在Employee Entity类中加入UserProjects和ProjectMaster?

实体

实体员工

@Table(name = "EMPLOYEES")
public class Employee implements Serializable {

@Id
@Column(name = "USER_NUMBER", nullable = false)
private String userNumber;

@Column(name = "EMP_NAME")
private String empName;

@OneToMany(mappedBy = "employee")
private List<UserProjects> userProjectsList;

实体UserProjects

@Table(name = "USER_PROJECTS")
public class UserProjects implements Serializable {

@Id
@Column(name="PROJECT_ID", nullable = false, insertable = false, 
    updatable = false)
private String projectId;

@Id
@Column(name="USER_NUMBER", nullable = false, insertable = false, updatable = false)
private String userNumber;

@ManyToOne
@JoinColumn(name = "PROJECT_ID", referencedColumnName = "PROJECT_ID")
private ProjectMaster projectMaster;

实体ProjectMaster

@Table(name = "PROJECTMASTER")
public class ProjectMaster implements Serializable {

@Id
@Column(name="PROJECT_ID", nullable = false)
private String projectId;

@Column(name="PROJECT_DESCRIPTION")
private String projectDesc;

@Column(name="ACTIVE")
private String active;

1 个答案:

答案 0 :(得分:1)

select DISTINCT u.userNumber,e.empName from Employee e LEFT JOIN  
    e.userProjectsList u  JOIN u.projectMaster pm
    where e.userNumber='SMITH' AND pm.active='Y'

这不起作用吗?