Hi,
I have four tables with sample data related with eachother as below
Employee EMP_ID|
EMP_NAME101 |John
EmployeeDtl EMP_DTL_ID |
EMP_SKILLS |
EMP_ID1001 |Java |101
1002 |SQL |101
EmpDeptDtl EMP_DTL_ID |
DEPT_ID1001 |22
1002 |33
Dept DEPT_ID |
DEPT_NAME22 |XYZ
33 |PQR
Below are my corresponding pojos
Code:
@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;
Code:
@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;
Code:
@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;
Code:
@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;
Code:
@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;
Now, Can anyone suggest me on how to retrieve the list of Dept records for the given EMP_ID using CRITERIA. In the above sample data given, for the EMP_ID=101, the dept records with dept_ids 22 and 33 should be returned.
I got the solution using Named query and native SQl query. But I couln't get it through the hibernate CRITERIA. Can anyone please help me in this?
Thanks
Harish