2011-08-30 38 views
0

上的一個鍵的屬性標準,比方說,我有這樣的模式:Hibernate查詢API - 在地圖

public class ProjectModel { 
     ... 
     private Map<UserModel, ProjectUserRelations> usersRelations = new HashMap<UserModel, ProjectUserRelations>(); 
} 

在HBM這樣映射:

... 
<map name="usersRelations" cascade="save-update" table="PROJECT_MEMBERS"> 
    <key column="project_id" /> 
    <map-key-many-to-many column="user_id" class="UserModel"/> 
    <many-to-many column="properties_id" class="ProjectUserRelations"/> 
</map> 
... 

我怎麼能用戶Hibernate的標準列出項目給了用戶? 我試過這個:

Criteria hbCriteria = session.createCriteria(ProjectModel.class);

if(criteria.getUserId() != null) { 
    hbCriteria.createCriteria("usersRelations").add(Restrictions.eq("userId", criteria.getUserId())); 
} 

當然用戶映射:

<class name="UserModel" 
    table="USER"> 
    <id name="objectId" column="objectId" type="java.lang.Long"> 
</class> 

當使用當前實現一個GET:

org.hibernate.QueryException: could not resolve property: usersRelations.objectId of: ProjectModel 

任何幫助表示讚賞。

回答

0

我不確定你正在嘗試做什麼(在多對多建模的地圖的關鍵屬性上設置標準)可以使用hibernate標準API實現。

你試過:

hbCriteria.createCriteria("usersRelations").add(Restrictions.eq("userModel.userId", criteria.getUserId())); 

+0

不,你的建議是在**價值的財產** Map的設置標準,我想設置一個**地圖 –

+0

我想的usermodel是地圖中的關鍵的關鍵**物業標準是什麼?你想找到所有具有特定UserModel的用戶關係,對嗎? –

+0

是的,確切地說。但是你寫的是「usersRelations」中屬性名爲「userModel」的標準 - 值。至少這是hibernate在異常時所說的複製粘貼的內容。我沒有理由不相信他:) –