Software Development

How to use @ManyToMany in JPA

Background

[wp_ad_camp_5]

This article demonstrates how to use @ManyToMany in JPA using Hibernate JPA.

Software Environment

  • Windows 7 Professional SP1
  • Eclipse – Kepler Release
  • Java 1.7 (1.7.0_67 – Windows x86)
  • MySQL 5.6.16 – Community Server (GPL)
  • MySQL Java Connector 5.1.34
  • Hibernate 4.3.7 (Entity Manager)

Create a Maven project

To create a Maven project, please follow the steps on How to create Maven project in Eclipse article. For information on how to use Hibernate JPA, please read How to use Hibernate JPA in a simple Application.

Create a MySQL Database

Run the following DDL. The database schema for this article is named “many2manyjpa”.

Maven pom.xml dependencies

[wp_ad_camp_4]

Add the following dependencies in your pom.xml

The persistence.xml

Sample JPA Application

We have three (3) classes for this application – Customer, Product, and JoinTableSampleDaoImpl (main class). Customer and Product are @Entity classes. JoinTableSampleDaoImpl demonstrates how to saving of data using the @Entity classes.

Customer class

Product class

 The main class

Download the Project

[wp_ad_camp_3]

https://www.dropbox.com/s/olhms9ah3cesafe/TurretaJoinTableSample.zip?dl=0

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like