我有四个表,其中包含彼此相关的示例数据,如下所示
员工
EMP_ID|EMP_NAME
---------------
101 |John
EmployeeDtl
EMP_DTL_ID |EMP_SKILLS |EMP_ID
-----------------------------------
1001 |Java |101
1002 |SQL |101
EmpDeptDtl
EMP_DTL_ID |DEPT_ID
--------------------
1001 |22
1002 |33
部门
DEPT_ID |DEPT_NAME
----------------------
22 |XYZ
33 |PQR
下面是我对应的pojos
@Entity
public class Employee {
private static final long serialVersionUID = 1L;
@Id
@Column(name="EMP_ID")
private Long empId;
@Column(name="EMP_NAME")
private String empName;
//bi-directional many-to-one association to EmployeeDtl
@OneToMany(mappedBy="employee", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
private Set<EmployeeDtl> empDtls;
@Entity
@Table(name="EMP_DTL")
public class EmployeeDtl {
@Id
@Column(name="EMP_DTL_ID")
private Long empDtlId;
@Column(name="EMP_GRP")
private String employeeGrp;
//bi-directional many-to-one association to Employee
@ManyToOne
@JoinColumn(name="EMP_ID")
private Employee employee;
@Column(name="EMP_ID", insertable=false, updatable=false)
private Long empId;
//bi-directional many-to-one association to EmpDeptDtl
@OneToMany(mappedBy="id.empDtlId", cascade= CascadeType.ALL)
private Set<EmpDeptDtl> empDeptDtls;
@Entity
public class EmpDeptDtl {
@EmbeddedId
private EmpDeptDtlPK id;
@Column(name="EMP_DTL_ID", insertable = false, updatable = false)
private Long empDtlId;
//bi-directional many-to-one association to Dept
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="DEPT_ID", insertable = false, updatable = false)
private Dept deptId;
@Embeddable
public class EmpDeptDtlPK implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;
@Column(name="DEPT_ID")
private Long deptId;
//bi-directional many-to-one association to EmpDeptDtl
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="EMP_DTL_ID")
private EmployeeDtl employeeDtl;
@Entity
public class Dept {
@Id
@Column(name = "DEPT_ID")
private Long deptId;
@Column(name = "DEPT_NAME")
private String deptName;
// bi-directional many-to-one association to EmpDeptDtl
@OneToMany(mappedBy = "deptId")
private Set<EmpDeptDtl> empDeptDtls;
现在,谁能建议我如何使用 CRITERIA 检索给定 EMP_ID 的 Dept 记录列表。在上面给出的示例数据中,对于 EMP_ID=101,应返回 dept_ids 为 22 和 33 的部门记录。
我使用命名查询和本机 SQl 查询得到了解决方案。但我无法通过休眠标准得到它。有人可以帮我吗?
谢谢哈里什