2011-05-29 55 views
1

我有这两个类如何编写休眠条件查询为两个不同的表

@Table(名称= “candidateinfo”)

类CandidateInfo {

.... @ OneToMany

CandidateResume candidate; ....

}

@Table(名称= “candidateResume”)

类CandidateResume { ....

@ManyToOne

CandidateInfo候选人;

.......

}

现在我想从2个不同类别在下面的条件

此我有

添加两个restrictoins(如上)

Criteria crit = session.createCriteria(CandidateResumeInfo.class);

crit.add(Restrictions.eq( “resumeSearchable”,1)); // resumeSearchable是CandidateResume

crit.createCriteria( “候选”)

。新增(Restrictions.eq( “参数userid”,1)); // userid is in CandidateInfo类

List rsList = crit.list(); //在这一行它转到例外,而不是让任何错误

为(迭代它= rsList.iterator(); it.hasNext();)

回答

0

能否请您解释一下究竟是你想什么搜索,但如果我正确理解你,你想搜索所有结果,其中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); 
List<Candidate> = q.list(); 
+0

谢谢,是的,你是对的,我想搜索所有结果,其中canditate ID = 1和candidateresume。可搜索= 1; 但问题是,候选人id是在一个表即ie CandidateInfo和可搜索是在其他表即ieResultResumeInfo,所以如何编译器在您的上述解决方案将知道哪个属性是从哪个表(类) – junaidp 2011-05-29 07:06:29

+0

这是休眠问题不是你的。只要你正确地映射所有的类,所有的表对你来说都是透明的。你只使用类。 http://www.java2s.com/Code/Java/Hibernate/Relation-One-to-Many.htm – 2011-05-29 08:00:02

+0

谢谢,你提到的链接不使用注释,而我使用注释,你有任何想法如何使用映射在此场景的注释中,正如我使用的类CandidateResumeInfo {@OneToOne CandidateInfo candidate;我只需要做什么?谢谢 – junaidp 2011-05-30 02:51:57