Hibernate Many to Many Mapping Example

Let us see an example on this many to many relationship in hibernate.  Actually here there is no question of unidirectional, only Bi-Directional.

Applying many to many relationship between two pojo class objects is nothing but applying one to many relationship on both sides, which tends to Bi-Directional i mean many to many.


Let us see this, if we apply many to many association between two pojo class objects student and course, provided the relationship is one student may joined in multiple courses and one course contains lot of students (joined by multiple students)

Remember, when ever we are applying many to many relationship between two pojo class objects, on both sides  we need a collection property [As we are applying one to many from both the sides]

Note Points:

  • While applying many to many relationship between pojo classes,  a mediator table is mandatory in the database, to store primary key as foreign key both sides, we call this table as Join table
  • In many to many relationship join table contain foreign keys only

Many To Many Relationship Example

files required…..

  • Course.hbm.xml
  • Student.hbm.xml
  • hibernate.cfg.xml





Just see the mapping files by seeing this output file….

You Might Also Like

  ::. About the Author .::

- Full Stack Java/J2EE & UI Web Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.


12 Responses to “Hibernate Many to Many Mapping Example”
  1. hi jaca4s team ,
    i got this many-to-many concept clearly . thanks a lot .

  2. krish says:

    Super …..Excellent

  3. mahesh says:

    Hi Sivateja can u provide db tables syntax?

  4. sree says:

    Its too good to learn easilyy….

  5. can u provide oracle data base syntax along with the hibernate examples because we got some problems while creating the tables.

  6. Sasi says:

    Hi SivaTeja/Java4s Team,
    In Course.hbm.xml,pls explain why we used inverse=”false”.Anyway,it is a bidirectional relation,then why did we specify>Any specific purpose?

  7. Sir I’ve a doubt. Why are you using collection on both sides, Even it is not required to use collection in ‘’ because we are not using ‘setStudents()’ method. The same example is also explained here –
    the example given in this link implement many to many relationship without using the collection in ‘’.
    Please sir help me out, I need more explanation.

  8. shrikanth reddy says:

    Superb i liked it…….

  9. praveen says:

    in course.hbm.xml you are applyied Inverse=false, what is that mean?

  10. arun says:

    How many tables we need to create while we using many to many?

  11. arun says:

    we have N no'of vendors and N no'of customers are there that time we need only two tables in this scenario can we use many to many relationship.


By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Hibernate Spring Struts
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2018 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.