简体   繁体   English

Hibernate Query API-映射中键属性的条件

[英]Hibernate Query API - criteria on property of a key in map

Let's say I have such model: 假设我有这样的模型:

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

mapped in hbm like this: 像这样在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>
...

How can I user Hibernate Criteria to list projects that have given user? 如何使用“休眠条件”列出已给用户的项目? I tried with this: 我尝试了这个:

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

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

Of course user is mapped: 当然,用户映射为:

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

When using current implementation a get: 使用当前实现时,将获得:

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

Any help appreciated. 任何帮助表示赞赏。

I am not sure what you are trying to do (setting up a criteria on key property of map that's modelled as many-to-many) is achievable using hibernate criteria API. 我不确定要使用休眠标准API来实现什么(在建模为多对多的地图的关键属性上设置标准)。

Have you tried: 你有没有尝试过:

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

?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM