Hibernate JPA Many To Many Relation Mapping Example

Hibernate JPA Many To Many Relation Mapping Example explains about how to implement a Many to Many relationship by using Hibernate JPA

Java Persistence API (JPA) is a standard interface which wraps different ORM tools such as  Hibernate, EclipseLink, OpenJPA etc. ie; you can able to change Hibernate implementation to EclipseLink implementation without changing the code base.

many-to-many entity relationship, each record of an entity have multiple records in other associated entity and vice versa

On this standalone Many To Many Mapping Example, we are using Java Persistence (JPA) With MySQL Database. If you want to configure Hibernate JPA With other Databases, you can check below table

Database Driver class Jar name Examples
MySQL com.mysql.jdbc.Driver mysql-connector-java-5.1.25-bin.jar (exact name depends on version)
EclipseLink JPA With MySQL
Hibernate JPA With MySQL
HSQLDB org.hsqldb.jdbcDriver hsqldb.jar
Hibernate JPA With HSQL
EclipseLink JPA With HSQL
Sybase com.sybase.jdbc3.jdbc.SybDriver jconnect.jar
Hibernate JPA With Sybase
EclipseLink JPA With Sybase
Apache Derby org.apache.derby.jdbc.EmbeddedDriver derby.jar
Hibernate JPA With Derby
EclipseLink JPA With Derby
IBM DB2 com.ibm.db2.jcc.DB2Driver db2jcc4.jar
Hibernate JPA With DB2
EclipseLink JPA With DB2
PostgreSQL org.postgresql.Driver postgresql-8.4-701.jdbc4.jar (exact name depends on PostgreSQL version)
Hibernate JPA With PostgreSQL
EclipseLink JPA With PostgreSQL
SQL Server (Microsoft driver) com.microsoft.sqlserver.jdbc.SQLServerDriver sqljdbc4.jar
Hibernate JPA With SQL Server
EclipseLink JPA With SQL Server
Informix com.informix.jdbc.IfxDriver ifxjdbc.jar
Hibernate JPA With Informix
EclipseLink JPA With Informix

You can also check following related Hibernate JPA Tutorial Series

1)    Hibernate JPA One To One Relation Mapping Example
2)    Hibernate JPA One To Many Relation Mapping Example
3)    Hibernate JPA Many To One Relation Mapping Example
4)    Hibernate JPA Many To Many Relation Mapping Example

Let us see how to implement JPA Many-to-Many relationship using Hibernate

Required Libraries

You need to download

  1. JDK 7
  2. Eclipse 3.7
  3. Hibernate 4.3.1
  4. Mysql 5.1.30

Following jar must be in classpath

  1. antlr-2.7.7.jar
  2. dom4j-1.6.1.jar
  3. hibernate-commons-annotations-4.0.4.Final.jar
  4. hibernate-core-4.3.1.Final.jar
  5. hibernate-entitymanager-4.3.1.Final.jar
  6. hibernate-jpa-2.1-api-1.0.0.Final.jar
  7. jandex-1.1.0.Final.jar
  8. dom4j-1.6.1.jar
  9. jboss-logging-3.1.3.GA.jar
  10. jboss-transaction-api_1.2_spec-1.0.0.Final.jar
  11. mysql-connector-java-5.1.28-bin.jar

By using hibernate.hbm2ddl.auto property, hibernate automatically create below table structure, so you only need to have the following property in persistence.xml

<property name="hibernate.hbm2ddl.auto" value="create-drop" />

Table structure

create table `employee` (
	`EMPLOYEEID` double ,
	`EMPLOYEENAME` varchar (765)

create table `employee_event` (
	`EMPLOYEEID` double ,
	`EVENTID` double 

create table `event` (
	`EVENTID` double ,
	`EVENTNAME` varchar (765)

1 2 3 Next

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