Hibernate: Many To Many Example using Annotation

In this tutorial, you will learn how to work with Many to Many table relationship in Hibernate using Annotation.

Tools & technologies used in this article:

  1. Java 1.6
  2. Maven 3.2.
  3. Hibernate 4.x
  4. Oracle 11.2.0
  5. Eclipse

Many-to-Many relationship between TABLE A and TABLE B is as follows.

  • Each row in a TABLE A is related to 0,1 or many rows in TABLE B and Vice versa.
  • It requires TABLE C called mapping table to map Many-to-Many relation between TABLE A and TABLE B.
CUSTOMERS (TABLE-A)
CUSTOMER_ID(PK)
CUSTOMER_NAME
PRODUCTS (TABLE-B)
PRODUCT_ID
PRODUCT_NAME
CUSTOMER_PRODUCT (TABLE-C OR Mapping table)
CUSTOMER_ID
PRODUCT_ID

1)  POM.xml

2) Project structure

Hibernate-ManytoManyAnnotation

 

 

 

 

 

 

 

 

 

 

3) Hibernate Entity classes

 

4) Hibernate Configuration File

5) Run App.java

6) Output in Eclipse and Oracle

Eclipse

Oracle

 Download Source Code

Posted in hibernate and tagged , , .

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">