-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Mapping Many to Many with an Object and Convenience Methods
PostPosted: Tue Mar 30, 2004 5:21 pm 
Newbie

Joined: Tue Mar 30, 2004 4:52 pm
Posts: 1
I am trying to map a many-to-many relationship using a mapping object. The object names are:

Account
Group
AccountGroupAssignment

I need the mapping object because there are properties I need to store that are related to the account's assignment to the group.

I can create a getGroupAssociations() function on both the Account and Group objects just fine. What i want to do (and cant get working) is create convenience methods like getGroups() and getAccounts() on the Account and Group objects respectively.

When I add the following to the GroupBean.hbm.xml file, it alters the primary keys on the AccountGroupAssignment table, changing the PK from the id column to a composite key between (group_id, account_id):

Code:
        <set name="members" table="account_group">
            <key column="group_id"/>
            <many-to-many class="AccountBean" column="account_id" />
        </set>


This altering of PK seems to confuse Hibernate later when it is trying to edit/update the collection.


Below are my mapping files:

Code:
<hibernate-mapping package="net.cg.ioweu.model">
    <class name="AccountBean" table="accounts">

        <cache usage="read-write"/>

        <id name="id" column="id" unsaved-value="null">
            <generator class="native"/>
        </id>

        ...

        <set name="groupAssignments" table="account_group" inverse="true">
            <key column="account_id"/>
            <one-to-many class="AccountGroupAssignmentBean"/>
        </set>

        ...
    </class>
</hibernate-mapping>


<hibernate-mapping package="net.cg.ioweu.model">
    <class name="AccountGroupAssignmentBean" table="account_group">

        <id name="id" column="id" unsaved-value="null">
            <generator class="native"/>
        </id>

         ...

        <many-to-one name="account" column="account_id" class="AccountBean"/>
        <many-to-one name="group" column="group_id" class="GroupBean"/>

    </class>

</hibernate-mapping>


<hibernate-mapping package="net.cg.ioweu.model">
    <class name="GroupBean" table="groups">

        <cache usage="read-write"/>

        <id name="id" column="id" unsaved-value="null">
            <generator class="native"/>
        </id>

        ...

        <set name="members" table="account_group">
            <key column="group_id"/>
            <many-to-many class="AccountBean" column="account_id" />
        </set>

        <set name="groupAssignments" table="account_group" cascade="save-update" >
            <key column="group_id"/>
            <one-to-many class="AccountGroupAssignmentBean"/>
        </set>

        ...

    </class>
</hibernate-mapping>


Any help you can provide would be greatly appreciated.

I am working on WinXP, MySQL 3.23.xx

Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 31, 2004 10:01 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Do what you want on an unmapped getter that will call the appropriate mapped relationship

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.