Hibernate查询:一个Set是否包含某个Object? [英] Hibernate query: does a Set contains a certain Object?
问题描述
我有两个Hibernate数据对象。第一个是用户(具有唯一的ID,用户名等),第二个是可协作的类。在这两者之间有一个n-to-m关系(用Sets实现)。这意味着,用户可以在许多Collaborationable上工作,而Collaborationable可以有许多用户。此外,Collaborationable只有一个用户作为所有者。
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查询,用于搜索具有特定用户作为所有者的Collaborationable或包含Collaborationable.users集中的相同特定用户的Collaborationable。另外,还应该有一个简单的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?
例如:
For example:
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 关键字。
select c
FROM CollaborateableImpl c
WHERE (
c.owner = :user
OR :user in elements(c.users)
)
AND c.keywords like '%:searchString%'
这篇关于Hibernate查询:一个Set是否包含某个Object?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!