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>
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.
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?
WHERE clause is used to narrow the specific objects that are returned from the database. It works the same as the SQL WHERE clause but operates with class properties rather than fields of a table.
Some of the commonly supported clauses in HQL are: HQL From: HQL From is same as select clause in SQL, from Employee is same as select * from Employee . We can also create alias such as from Employee emp or from Employee as emp . HQL Join : HQL supports inner join, left outer join, right outer join and full join.
HQL queries are case insensitive; however, the names of Java classes and properties are case-sensitive HQL is used to execute queries against database.
Hibernate Query Language (HQL) is an object-oriented query language, similar to SQL, but instead of operating on tables and columns, HQL works with persistent objects and their properties. HQL queries are translated by Hibernate into conventional SQL queries, which in turns perform action on database.
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%'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With