How to map an association between independent entities


#1

Hi,

How can I express the relationship between two independent entities in Hibernate?

There are fix number of Tabs and a Role class contains a collection of one or more tabs.
Both tab and role class independent i.e creation of Role does not create a tab entity and
vice verse.

I have seen one net are of dependent entities e,g product - item or item - image ect where
one-to-many or many-to-one hibernate examples make sense. But somehow it does not seem to
work for relationships as between Role and Tab as explained above.

Can someone pls guide?

Thanks in Advance
Abhi


#2

If both Tabs and Role are independent, then it means you need a many-to-many association with a join table: role_tabs.

To map the many-to-many database table relationship, you have two options:

  1. You can use the @ManyToMany annotation as explained in this article.
  2. You can map the join table as an entity and set the two FK as @ManyToOne associations that are used to build a composite identifier. For more details, check out this article.

#4

Thanks for the reply. Since i m still using hbm files i was able to fix it using below code.

BRs,


#5

Please add the source code as text, not as an image. Thanks.