views:

165

answers:

2

I have two tables "Group" and "Customer" and of course two entities "Group" and "Customer". And i have another table which is referencing both "CustomerGroupMember" table.

I use CustomerGroupMember table for many-to-many mapping.

Customer.hbm.xml

<!--Many to many-->
    <bag name="CustomerGroups" table="CustomerGroupMember" cascade="all" lazy="true">
      <key column="CustomerId" />
      <many-to-many class="CustomerGroup" column="CustomerGroupId" />
    </bag>

Group.hbm.xml

<bag name="Members" table="CustomerGroupMember" cascade="all" lazy="true">
      <key column="CustomerGroupId" />
      <many-to-many class="Customer" column="CustomerId" />
</bag>

I haven't created an entity and mapping for "CustomerGroupMember" table.

My question is how can i delete a CustomerGroupMember from CustomerGroupMember table ? Do i need to create an entity for CustomerGroupMember in order to delete CustomerGroupMember or there is another way ?

Thank you very much.

+1  A: 

To delete a relationship item between those tables, you should somehow be able to reference the exact row in the junction table which is not possible in your current mapping. Yes, you have to create an entity and mapping for the CustomerGroupMember table. Without a mapping, how can you tell which row you want to delete?

Mehrdad Afshari
Just what i thought, thank you very much Mehrdad, again :)I was just curious if there is another way. Thank you
Barbaros Alp
A: 

By the mapping above when i delete a Group it is going to delete all the Customers in it. But i just want it to delete GroupMember not the Customer.

Barbaros Alp
This should be a comment. I think I hadn't understood your question correctly. I updated the answer.
Mehrdad Afshari