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 | { 7 Comments }

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

 

What you are thinkig....

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.

If you want a pic to show with your comment, go get a gravatar !
Please post your questions on Java4s Answers forum

Name*
Ask a Question ?
or
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Tutorials
Hibernate Recent Posts
Spring Recent Posts
Struts Recent Posts
Recomandded Links Current & UpComing Tutorials Java4s.com
Tutorials Online :
spring Hibernate struts Json Ajax Log4j Log4j
coreJava Servlets


UpComing :
Jsps,Web Services
Java4s is optimized for learning java technologies, all the examples in this site are constantly reviewed to avoid errors. While using this site you agree to have read and accepted our terms of use and privacy policy
Especially i have prepared this blog by keeping fresher's in mind, however it will be very useful for real time developers too.


© 2014 Java4s All rights reserved. | strPro4Tut v(2.0) Theme designed by Team Java4s.