Rule Definition
One-to-many associations are easily the most important kind of association. In fact, its usage is discouraged when a simple bidirectional many-to-one/one-to-many will do the job. In particular, a many-to-many association may always be represented as two many-to-one associations to an intervening class. This model is usually more easily extensible. In a real system, you may not have a many-to-many association. Usually there is almost always other information that must be attached to each link between associated instances (such as the date and time when an item was added to a category) and that the best way to represent this information is via an intermediate association class.
Moreover, changing the definition of a primary key and all foreign keys that refer to it is a frustrating task.
Remediation
In Hibernate, you can map the association class as an entity and map two one-to-many associations for either side.
Violation Code Sample
<class name="A" table="A">
...
<set name="B" table="B">
<key column="A_ID"/>
<many-to-many column="B_ID" class="B"/> // VIOLATION
</set>
</class>
Fixed Code Sample
Annotation based configuration:
@Entity
public class A {
@Id
Long id;
@OneToMany // Possible fix
Set<B> fieldA;
}
XML based configuration:
<class name="A" table="A">
...
<set name="B" table="B">
<key column="A_ID"/>
<one-to-many column="B_ID" class="B"/> // Possible fix
</set>
</class>
Reference
Hibernate in Action (ISBN 1932394-15-X) p 220
Related Technologies
JEE
Technical Criterion
Architecture - Multi-Layers and Data Access
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.