Hibernate Query API - 映射中键的属性标准

发布于 2024-12-01 23:17:29 字数 1184 浏览 6 评论 0原文

假设我有这样的模型:

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 来列出已给定用户的项目? 我尝试这样做:

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>

当使用当前实现时,得到:

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

任何帮助表示赞赏。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故乡的云 2024-12-08 23:17:29

我不确定您想要做什么(在建模为多对多的地图的关键属性上设置标准)是否可以使用 hibernate criteria API 来实现。

你尝试过吗:

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

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

?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文