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”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | CREATE TABLE IF NOT EXISTS customer ( id int(11) NOT NULL AUTO_INCREMENT, cust_name varchar(30) NOT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ; CREATE TABLE IF NOT EXISTS cust_prod ( id int(11) NOT NULL AUTO_INCREMENT, cust_id int(11) NOT NULL, prod_id int(11) NOT NULL, PRIMARY KEY (id), KEY cust_id (cust_id), KEY prod_id (prod_id) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ; CREATE TABLE IF NOT EXISTS product ( id int(11) NOT NULL AUTO_INCREMENT, product_name varchar(30) NOT NULL, price decimal(10,0) DEFAULT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ; ALTER TABLE cust_prod ADD CONSTRAINT cust_prod_ibfk_1 FOREIGN KEY (cust_id) REFERENCES customer (id), ADD CONSTRAINT cust_prod_ibfk_2 FOREIGN KEY (prod_id) REFERENCES product (id); |
Maven pom.xml dependencies
[wp_ad_camp_4]
Add the following dependencies in your pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.turreta.jpa.jointable</groupId> <artifactId>TurretaJoinTableSample</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>4.3.8.Final</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.34</version> </dependency> </dependencies> </project> |
The persistence.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0"> <persistence-unit name="jpahibernate" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> <class>com.turreta.jpa.hibernate.entities.Student</class> <properties> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/many2manyjpa" /> <property name="javax.persistence.jdbc.user" value="root" /> <property name="javax.persistence.jdbc.password" value="password" /> <property name="hibernate.show_sql" value="true" /> </properties> </persistence-unit> </persistence> |
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
1 2 3 4 5 6 7 | ... @ManyToMany @JoinTable(name="cust_prod", joinColumns={@JoinColumn(name="cust_id")}, inverseJoinColumns={@JoinColumn(name="prod_id")}) private List<Product> products = new ArrayList<Product>(); ... |
Product class
1 2 3 4 | ... @ManyToMany(mappedBy="products") private List<Customer> customers = new ArrayList<Customer>(); ... |
The main class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | package com.turreta.jpa.jointable.dao; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import com.turreta.jpa.jointable.persistence.Customer; import com.turreta.jpa.jointable.persistence.Product; public class JoinTableSampleDaoImpl { public static void main(String[] args) { EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpahibernate"); EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); Customer c = new Customer(); Product p1 = new Product(); p1.setName("Product 1"); c.setCustomerName("Karl"); c.getProducts().add(p1); em.persist(c); em.persist(p1); em.getTransaction().commit(); em.close(); emf.close(); } } |
Download the Project
[wp_ad_camp_3]
https://www.dropbox.com/s/olhms9ah3cesafe/TurretaJoinTableSample.zip?dl=0