我想用 Hibernate Annotations 和 Criteria 连接 MySQL 中的 2 个表 比如:
我有 2 个表,candidates 和 jobs,每个表有 2 列:
工作:jobID & jobName
candidates jobs
candID candName jobID jobName
1 abc 1 job1
2 xyz 2 job2
我需要在休眠条件下创建一个查询:
select candName ,jobName from candidates as c ,jobs as j
where c.candID = j.jobID where candName = abc and jobName=job1
这将是什么标准查询,最重要的是我将在我的注释类中写什么(因为我正在使用 spring 注释)并且我需要在我的 applicationioncontext.xml 中写什么......
谢谢
如果你能帮助我,我将非常感激,因为我为此苦苦挣扎了 3 天,但没有成功
谢谢
假设每类层次结构表,其中候选人和工作对应于他们的数据库实体
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);