Hibernate Many to Many Example using XML
We can map many to many relation either using list, set, bag, map, etc. Here, we are going to use list for many-to-many mapping. In such case, three tables will be created.
Example of Many to Many Mapping
In this example, we will generate a many to many relation between questions and answers using list.
1) Create the Persistent class
There are two persistent classes Question.java and Answer.java. Question class contains Answer class reference and vice versa.
2) Create the Mapping file for the persistent class
Here, we have created the question.hbm.xml and answer.hbm.xml file for defining the list.
3) Create the configuration file
This file contains information about the database and mapping file.
4) Create the class to store the data