Hi, i've a problem with realize ternary relation...
I use a very simple example for explain my problem:
DB is composed of four table:
- users (userId,name)
- groups (groupId,description)
- role (roleId,description)
- u_g_r (fk_userId,fk_groupId,fk_roleId)
my problem is that i would have a method on Group class that return the users of this group and theyr role (only roleId that is present in ternary association table... ) ....i would that is realized by hibernate with only query as: select users.userId,users.name,u_g_r.fk_roleId from u_g_r inner
join users on users.userId = u_g_r.fk_userId where u_g_r.fk_groupId = ?
...i try to use (in mapping of group)
<map name="users" table="`u_g_r`" cascade="none">
<key column="`fk_groupId`" not-null="true"/>
<map-key-many-to-many column="`fk_roleId`"class="it.test.Role"/>
<many-to-many column="`fk_userId`" class="it.test.User"/>
</map>
...or composite elements:
<set name="users" table="`u_g_r`">
<key column="`fk_groupId`" not-null="true"/>
<composite-element class="it.test.UserRole">
<property column="`fk_roleId`" name="roleId"/>
<many-to-one name="user" class="it.unitn.cs.hibernate.User" column="`fk_userId`"/>
</composite-element>
</set>
...but this realize n° query for retrieve user information (one for each user in u_g_r )...
I would that group contains an collection of UserRole; this class has two properties user and roleId... how realize this with Hibernate (populated this class with only query and not with n° query for each user!)?
Thanks in advance!
|