2011-05-29 53 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