本文介绍了Hibernate 查询:Set 是否包含某个 Object?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 Hibernate 数据对象.第一个是用户(具有唯一 ID、用户名等),第二个是 Collaborateable 类.在这两者之间有一个 n 到 m 的关系(用 Sets 实现).这意味着,一个用户在许多 Collaborateable 上工作,而一个 Collaborateable 有很多用户.此外,Collaborateable 只有一个用户作为所有者.

I have two Hibernate data object. The first is a User (with unique id, username etc.) and the second is the class Collaborateable. Between this two there is a n-to-m relation (implementet with Sets). That means, a User works on many Collaborateables and a Collaborateable has many users. In addition a Collaborateable has exactly one User as owner.

<class name="CollaborateableImpl" table="Collaborateable">
<id name="id" type="int" column="id">
    <generator class="increment" />
</id>

<property name="name" column="name" type="string" not-null="true" />
<property name="keywords" column="keywords" type="string"/>

<!-- Collaborateable has a Registered User as owner -->
<many-to-one name="owner" class="UserImpl" fetch="select">
        <column name="User_id_owner" not-null="true" />
</many-to-one>

<!-- Users that collaborate on this Collaborateable -->
<set name="users" table="CollaborateOn" inverse="false">
        <key column="Collaborateable_id" />
        <many-to-many column="User_id" class="UserImpl" />
</set>

我想实现一个 Hibernate 查询,它搜索以某个用户为所有者的 Collaborateables,或者在 Collaborateable.users 集中包含相同的某个用户.此外,还应该有一个简单的 WHERE 子句来检查关键字.

i would like to implement a Hibernate query, that searches for Collaborateables that have a certain user as owner OR containing the same certain User in the Collaborateable.users Set. In addition, there should also be a simple WHERE clause to check for Keywords.

Hibernate 中有类似 CONTAINS 运算符的东西吗?

Is there something like a CONTAINS operator in Hibernate?

例如:

FROM CollaborateableImpl WHERE (owner = :user OR users CONTAINS :user) AND keywords like '%:searchString%'

否则,你知道如何通过连接来解决这个问题吗?

Otherwise, do you know how to solve this problem with a join?

推荐答案

您正在寻找 elements 关键字.

You're looking for the elements keyword.

select c
FROM CollaborateableImpl c
WHERE (
    c.owner = :user
    OR :user in elements(c.users)
)
AND c.keywords like '%:searchString%'

这篇关于Hibernate 查询:Set 是否包含某个 Object?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-15 01:00