问题
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();)
回答1:
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();
来源:https://stackoverflow.com/questions/6165743/how-to-write-hibernate-criteria-query-for-two-different-tables