Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@ManyToAny annotation in hibernate

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?

like image 647
Biman Tripathy Avatar asked Dec 26 '12 16:12

Biman Tripathy


1 Answers

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.

like image 158
JB Nizet Avatar answered Sep 25 '22 09:09

JB Nizet