facebook

Access & Mapping of many2many Relationships

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #226159 Reply

    STiPP
    Member

    Hi,

    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: roleId

    I 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.

    #226160 Reply

    STiPP
    Member

    Sorry, 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;
    
    #226194 Reply

    support-jeff
    Member

    For 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.

    #226195 Reply

    STiPP
    Member

    Thanks, thats what i want to know.

Viewing 4 posts - 1 through 4 (of 4 total)
Reply To: Access & Mapping of many2many Relationships

You must be logged in to post in the forum log in