- This topic has 3 replies, 2 voices, and was last updated 19 years, 8 months ago by STiPP.
-
AuthorPosts
-
STiPPMemberHi,
i don´t understand, how to do the following with the mappings created by myEclipseIDE.
Let me explain with an simple example:
2 Tables: User an Role
every Table has an PK (userId and roleId)Every user can have many, but at least one role.
Thats realised with table User_Roles.
In that table are only 2 Rows:
-PK,FK: userId
-PK,FK: roleIdI think thats clear so far.
My problem are the generated mappings and classes for that.
There are 4 classes: user, role, userRole and userRoleKey.My Questions:
How can i get all users of an explicit role and reverse?
How can i update the role-relationships of an user?My attempt is the following, but i think that i wouldnt work:
public List getRolesOfUser(User user) { List users = (List)getHibernateTemplate().load(UserRolesKey.class,user.getUserId()); return users; }
(I use Spring with Hibernate)
Can anybody help me to understand this and give an short “How-To”?
I know, i still can write an HSQL-Query, but thats not the finest way.Thanks.
STiPPMemberSorry, there is an error in the codesnippet above.
Thats correct:public List getRolesOfUser(User user) { List roles = (List)getHibernateTemplate().load(UserRolesKey.class,user.getUserId()); return roles; }
And here are my mappingfiles:
User:<class name="User" table="USER"> <id name="userId" column="USER_ID" type="java.lang.Integer"> <generator class="native"/> </id> <property name="username" column="USERNAME" type="java.lang.String" not-null="true" /> <property name="passwd" column="PASSWD" type="java.lang.String" not-null="true" /> <property name="firstname" column="FIRSTNAME" type="java.lang.String" /> <property name="lastname" column="LASTNAME" type="java.lang.String" /> <property name="fon" column="FON" type="java.lang.Integer" /> <property name="email" column="EMAIL" type="java.lang.String" /> </class>
Roles:
<class name="Roles" table="ROLES"> <id name="roleId" column="ROLE_ID" type="java.lang.Integer"> <generator class="native"/> </id> <property name="rolename" column="ROLENAME" type="java.lang.String" /> <property name="roledesc" column="ROLEDESC" type="java.lang.String" /> </class>
UserRoles:
<class name="UserRoles" table="USER_ROLES"> <composite-id name="id" class="UserRolesKey"> <key-many-to-one name="Roles" column="ROLE_ID" class="Roles"/> <key-many-to-one name="User" column="USER_ID" class="User"/> </composite-id> </class>
UserRolesKey.class: (stripped)
public class UserRolesKey implements Serializable { /** The cached hash code value for this instance. Settting to 0 triggers re-calculation. */ private volatile int hashValue = 0; /** The value of the ROLE_ID component of this composite id. */ private Roles roles; /** The value of the USER_ID component of this composite id. */ private User user;
support-jeffMemberFor the case of many-to-many you would typically have a collection mapping with a many-to-many element, e.g. in the mapping for User:
<set table=”user_role” name=”roles”>
<key column=”user_id” />
<many-to-many column=”role_id” class=”Role”/>
</set>and likewise in the Role, an opposite m2m.
Unfortunately, ME does not do m2m yet. This will be coming in a future release. For the time being, you can modify the generated source and mappings as suits your need. Just beware that re-generating will remove your modifications.
STiPPMemberThanks, thats what i want to know.
-
AuthorPosts