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,001
  • 4
  • 36
  • 57
Biman Tripathy
  • 2,686
  • 3
  • 22
  • 26
  • 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
  • 657,433
  • 87
  • 1,179
  • 1,226