Javatpoint Logo
Javatpoint Logo

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.

Question.java

Answer.java

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.

question.hbm.xml

answer.hbm.xml

3) Create the configuration file

This file contains information about the database and mapping file.

hibernate.cfg.xml

4) Create the class to store the data

StoreData.java

Output

Hibernate Many to Many Example 1 Hibernate Many to Many Example 2

Download





Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


B.Tech / MCA