2013-08-26 29 views
0

我有一個連接查詢,我想把它作爲休眠條件放在我的程序中。Hibernate標準的設置投影結果爲空

select a.accountName,a.username,b.name from Account as a , User as b 
where a.id=b.accountId and b.name like '%abc%' and a.username like '%def%' 

我的標準是這樣的:

Criteria userCriteria = session.createCriteria(User.class); 
DetachedCriteria accountCriteria = DetachedCriteria.forClass(Account.class); 
accountCriteria .add(Restrictions.like("name", "%" + def+ "%"));  
accountCriteria.setProjection(Projections.property("id")); 
userCriteria.add(Property.forName("accountId").in(accountCriteria));  
userCriteria.add(Restrictions.eq("username", "%" + abc+ "%")); 

List tmpList = userCriteria.setProjection(Projections.rowCount()).list();    
userCriteria.setMaxResults(pageSize); 
result = userCriteria.list(); 

列表tmpList被顯示爲空。我可以在之前列出所有數據:

List tmpList = userCriteria.setProjection(Projections.rowCount()).list(); 

這條線的問題是什麼?

查詢在where子句中
+0

的HQL使用account.username和user.name,而條件使用account.name和user.username。你爲什麼不在這兩個實體之間建立關聯?如果您的目標是將給定的HQL轉換爲標準,您將需要一個。限制也不同:我們不知道abc和def值是什麼。 –

回答

0

你告訴我們,你寫道:

b.name like '%abc%' 
a.username like '%def%' 

,但在代碼中你的寫作:

Restrictions.like("name", "%" + def+ "%") 
Restrictions.eq("username", "%" + abc+ "%") 

如果我沒看錯這將創建一個像這樣的地方:

a.name like '%def%' 
b.username == '%abc%' 

Sarajog