使用 Criteria API 和 Annotation 连接 Hibernate 中的两个表

发布于 2024-11-08 13:06:29 字数 695 浏览 0 评论 0原文

我想用 Hibernate Annotations 和 Criteria 连接 MySQL 中的 2 个表 例如:

我有 2 个表,候选人和工作,每个表有 2 列:

  • 候选人:candID & candName
  • 职位:jobID &职位名称

     候选人职位       
                  candID candName 工作ID 工作名称
                      1 abc 1 工作1
                      2 xyz 2 工作2
    

我需要在 hibernate 的 Criteria 中创建一个查询,如下所示:

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

该查询的条件查询是什么,最重要的是我将在我的注释类中编写什么(因为我正在使用 spring 注释)以及我需要在我的 applicationioncontext 中编写任何内容吗.xml...

谢谢,

如果您能帮助我,我将非常感激,因为我在过去的三天里一直在努力,但没有成功,

谢谢

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
  • jobs: 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浊酒尽余欢 2024-11-15 13:06:29

假设每个类层次结构有一个表,其中候选人和作业对应于它们的数据库实体,

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 */

我不会在 IDE/编译器内进行检查,但它应该类似于下面的内容

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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文