how to write hibernate criteria query for two different tables

最后都变了- 提交于 2019-12-11 06:59:57

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!