2012-02-02 33 views
14

我有兩個Hibernate數據對象。第一個是用戶(具有唯一的ID,用戶名等),第二個是可協作的類。在這兩者之間有一個n-to-m關係(用Sets實現)。這意味着,用戶可以在許多Collaborationable上工作,而Collaborationable可以有許多用戶。此外,Collaborationable只有一個用戶作爲所有者。Hibernate查詢:一個Set是否包含某個Object?

<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子句來檢查關鍵字。

在Hibernate中是否有像CONTAINS運算符那樣的東西?

例如:

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

否則,你知道如何與加盟解決這個問題?

回答

35

您正在查找的是elements關鍵字。

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

如果我使用標準什麼是限制類型? – jpprade 2014-03-28 15:08:34

相關問題