You can view the tutorials best in Google Chrome, Mozilla Firefox, Opera, higher version of Internet Explorer

Hibernate Many to Many Mapping Example

Hibernate » On Aug 1, 2011 By Sivateja

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.

Example:

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…..

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

Student.java

Course.java

Student.hbm.xml

Course.hbm.xml

OurLogic.java

hibernate.cfg.xml

Output

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








By ( Java/J2EE Developer & Web Designer )
Founder of Java4s.com. You can contact me at sivateja [at] java4s.com


Comments

7 Responses to “Hibernate Many to Many Mapping Example”
  1. Raju Kapadne says:

    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. bhaskarreddy says:

    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. Suyash Soni says:

    Sir I’ve a doubt. Why are you using collection on both sides, Even it is not required to use collection in ‘Course.java’ because we are not using ‘setStudents()’ method. The same example is also explained here – http://www.dzone.com/tutorials/java/hibernate/hibernate-example/hibernate-mapping-many-to-many-1.html
    the example given in this link implement many to many relationship without using the collection in ‘Course.java’.
    Please sir help me out, I need more explanation.

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
So, What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs

Most Recent Tutorials
Hibernate Recent Posts
Spring Recent Posts
Struts Recent Posts
Recomandded Links
Current & UpComing Tutorials
Sitemap :
spring Hibernate struts Json Ajax Log4j Log4j coreJava Servlets Web Services


Upcoming
  Web Services [ SOAP ], JSP
Facebook_Java4s  Twitter_Java4s  GooglePlus_Java4s  Youtube_Java4s 
© 2010 - 2014 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.