I am converting aplication that use SQL queries to Hibernate.
Having problem with query that has 5 tables and two query parameters.
I have tables
Code:
-------
Users
id
-------
---------
Groups
id
---------
--------
Items
id
--------
---------------------
User_group
user_id | group_id
---------------------
---------------------
Items
id | type
----------------------
--------------------------------------------
Item_access
item_id | user_id | group_id | access_level
---------------------------------------------
The task is: to select "Item"s that are linked to "User".
Link table is "item_access", and user may have non-direct access to item through "Group" membership in "user_group" table.
In old system there was query like this:
Code:
select item.id FROM users,groups,user_group, items, item_access WHERE
users.id=user_group.user_id, group.id=user_group.group_id,
(users.id=item_access.user_id OR group.id=item_access.group_id)
AND item_access.access_level=X , item.type=Y
Query also has two parameters for items, type of item and access evel of item.
With hibernate the way to do it is to map User class.
So in Java call should be
Code:
User.getItems(itemType,itemAccessLevel);
So far I add <set> to mapping, something like this:
Code:
<class name="Users">
....
<set name="items" table="items_access">
<key column="user_id"/>
<many-to-many column="item_id" class="Items" />
</set>
...
How can I add Item_access link table and user_group link table to this mapping?
How can I introduce parameters for Items table in query?
I couldn't find any examples of mapping with more than 3 tables and query parameters. Is there any ?
Thank you.