-->
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: problem implementing many-to-many relationship
PostPosted: Thu Feb 09, 2006 6:54 am 
Newbie

Joined: Thu Feb 09, 2006 5:23 am
Posts: 1
Hi,

I am using Hibernate 3.0 & SQL Server 2000.

I have 3 tables Account (Id[PK], Name) , Role (Id[PK], Description), R_Account_Role (Account_Id[FK], Role_Id[FK]). Account and Role have a many-to-many relationship via R_Account_Role table.

Account.hbm.xml
Code:
<hibernate-mapping>
    <class name="Account" table="Account">
        <id name="id" column="ID" type="integer" >
            <generator class="identity"/>
        </id>
        <property name="name" column="Name" not-null="true" type="string" />
        <set name="roles" table="R_Account_Role" lazy="false" cascade="all" >
            <key column="Account_Id" not-null="true"/>
            <many-to-many column="Role_Id" class="Role" />
        </set>
    </class>
</hibernate-mapping>


Role.hbm.xml
Code:
<hibernate-mapping>
    <class name="Role" table="Role">
        <id name="id" column="ID" type="integer" >
            <generator class="identity"/>
        </id>
        <property name="description" column="Description" not-null="true" type="string" />
        <set name="accounts" table="R_Account_Role" lazy="false" cascade="all" >
            <key column="Role_Id" not-null="true"/>
            <many-to-many column="Account_Id" class="Account" />
        </set>
    </class>
</hibernate-mapping>


Java code to delete an account
Code:
Account account = (Account) session.load(Account.class, accountId);
session.delete(account);
session.flush();


If I execute this, then all the roles associated with this account also gets deleted from Role table.
Ideally, only the records from Account-Role relationship table should get deleted.

If I do not use "cascade=all" then exception is thrown when I try to delete Account, since accountId is foreign key in R_Account_Role table.

Basically, I want to be able to create, update, delete an Account (and records in Account-Role relationship table), keeping Role table relatively constant [i.e. number of roles are fixed]

I am confused whether to create another hbm.xml file for R_Account_Role table or above code is sufficient with some changes. Please help.

Thanx.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 09, 2006 8:51 am 
Newbie

Joined: Thu Feb 09, 2006 4:07 am
Posts: 3
There probably is an elegant solution. See others for that.

As a temporary workaround (I cannot try it out but you could): what about first removing all the roles (buy assigning it an empty Set or perhaps even simply null) and subsequently removing the account?

Code:
Account account = (Account) session.load(Account.class, accountId);
account.setRoles(new HashSet());     // first remove all roles for this account
session.delete(account);             // and next delete the account
session.flush();


Moreover, I notice that you've set the non-null attribute to "true" on the key of the associated entity. You may ask yourself if that's really required (in my implementation of many-to-many relationships this isn't the case and everything works fine).


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.