-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Criteria for cascading joins in multiple tables
PostPosted: Tue Jun 19, 2012 8:50 am 
Newbie

Joined: Fri Nov 19, 2010 4:02 am
Posts: 11
Hi,

I have four tables with sample data related with eachother as below

Employee
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
DEPT_ID |DEPT_NAME
22 |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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.