加入在Hibernate中的两个表与标准API和注解 [英] Join two tables in Hibernate with Criteria API and Annotation
问题描述
我想加入MySQL中2表中使用Hibernate注解和标准
例如像
我是每一个2列,考生2表考生和工作:坦率和放大器; candName作业:作业ID&放大器;作业名
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
我需要在Hibernate作为选择candName创造条件的查询,作业名称从候选人C,工作为J,其中c.candID = j.jobID,其中candName = ABC和作业名= JOB1
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
这将是对于最重要的是什么将我在注释类写的(如我使用Spring注解),做我需要写什么我applicantioncontext.xml ...
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...
谢谢
我将非常感激,如果你能帮助我,当我奋力最近3天,它没有成功。
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
感谢
推荐答案
假设每类层次结构,其中考生乔布斯对应的数据库实体表
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 */
我DONOT检查一个IDE /编译器内,但它应该低于
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);
这篇关于加入在Hibernate中的两个表与标准API和注解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!