简体   繁体   中英

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:

...
<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);

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.

Have you tried:

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

?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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