开发者

Join two tables in Hibernate with Criteria API and Annotation

I want to join 2 tables in MySQL with Hibernate Annotations and Criteria Like for example:

I have 2 tables, candidates and jobs, having 2 columns each:

  • candidates: candID & candName
  • jo开发者_Go百科bs: jobID & jobName

                            candidates                     jobs       
                  candID    candName          jobID          jobName
                      1          abc                       1               job1
                      2          xyz                       2               job2
    

i need to create a query in Criteria in hibernate as:

 select candName  ,jobName    from candidates as c ,jobs as j  
 where c.candID = j.jobID where candName = abc and jobName=job1

what will be the criteria query for that and most importantly what will i write in my annotation class ( as i am using spring annotations) and do i need to write anything in my applicantioncontext.xml...

Thanks

I will be really greatful if you can help me with that as i am struggling for last 3 days for it with no success

thanks


Assuming table per class Hierarchy, where Candidates and Jobs correspond to their database entities

public class Candidates{
//define Generative stretegy if this is primary key, and other JPA annotations, with cascade
  private Long CandId;
//getters and setters
//define other properties here

}
/*Like wise for Jobs class */

I donot check inside an IDE/Compiler but it should be somelike below

Criteria c1 = session.createCriteria(Candidates.class,candidate);
Criteria j1 = session.createCriteria(Jobs.class,jobs);
c1.setProjection(Property.forName(candName));
j1.setProjection(Property.forName(jobName));
c1.add(Restrictions.and(Property.eqName(candidate.candId,jobs.jobId)));
j1.add(Restrictions.and(jobs.jobName,"job1"));
c1.addCriterion(j1);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜