9

I am unable to understand the use of @ManyToAny annotation in Hibernate. In which case it should be used? I googled it, but the explanations in a couple of websites just bounced over my head...

Can anyone explain it with a simple example? How's it different from @ManyToMany and @ManyToOne?

Swapnil
  • 8,201
  • 4
  • 38
  • 57
Biman Tripathy
  • 2,766
  • 3
  • 23
  • 27
  • seems like a similar question to [this one](http://stackoverflow.com/questions/217831/how-to-use-hibernate-any-related-annotations). – Lucas Dec 26 '12 at 16:20

1 Answers1

17

The javadoc says:

Defines a ToMany association pointing to different entity types. Matching the according entity type is done through a metadata discriminator column. This kind of mapping should be only marginal.

So instead of having a ManyToMany between Student and Course, you could have a ManyToAny between Student and [Course or Teacher], and the metadata discriminator column would say, for a given row, if it points to a Course or to a Teacher.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255