如何为两个不同的表编写hibernate标准查询 [英] how to write hibernate criteria query for two different tables
问题描述
我有这两个类
@Table(name =candidateinfo)
类CandidateInfo
....
@OneToMany
CandidateResume候选人;
....
}
@Table(name =candidateResume) p>
Class CandidateResume {
....
@ManyToOne
候选人候选人;
.......
现在我想在以下标准中添加两个不同类别的限制(如上所述):
为此,我有
Criteria crit = session.createCriteria(CandidateResumeInfo.class);
crit.add(Restrictions.eq(resumeSearchable,1)); // resumeSearchable在CandidateResume中
crit.createCriteria(candidate)
.add(Restrictions.eq(userid,1)); strong> // userid在CandidateInfo Class中
- rslist = crit.list();
(); it.hasNext();)
你能解释一下你究竟在寻找什么,但是如果我正确地理解了你想要搜索所有结果,其中canditate id = 1和candidateresume。可搜索= 1;
然后你需要做如下的查询:
String query =from Candidate c join c.candidate resume where c.userid =:userid and resume.resumeSearchable =:searchable;
Query q = session.createQuery(query);
q.addInteger(userid,1);
q.addInteger(searchable,1);
清单< Candidate> = q.list();
I have these two classes
@Table(name="candidateinfo")
Class CandidateInfo{
.... @OneToMany
CandidateResume candidate; ....
}
@Table(name="candidateResume")
Class CandidateResume{ ....
@ManyToOne
CandidateInfo candidates;
.......
}
Now i want to add two restrictoins from 2 different classes(as above) in the below criteria
for this i have
Criteria crit = session.createCriteria(CandidateResumeInfo.class);
crit.add(Restrictions.eq("resumeSearchable", 1));// resumeSearchable is in CandidateResume
crit.createCriteria("candidate")
.add(Restrictions.eq("userid",1)); // userid is in CandidateInfo Class
List rsList = crit.list(); // At this line it goes to exception and not giving any error
for(Iterator it=rsList.iterator(); it.hasNext();)
Could you please explain what exactly are you trying to search but if I understand you correctly you want to search for all results where canditate id = 1 and candidateresume. searchable = 1;
Then you need to make something like the following following query:
String query= "from Candidate c join c.candidate resume where c.userid = :userid and resume.resumeSearchable =: searchable";
Query q = session.createQuery(query);
q.addInteger("userid",1);
q.addInteger("searchable",1);
List<Candidate> = q.list();
这篇关于如何为两个不同的表编写hibernate标准查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!