Many to many mapping is made between two entities where one can have relation with multiple other entity instances. For example, for a subscription service SubscriptionEntity and ReaderEntity can be two type of entities. Any subscription can have multiple readers, where a reader can subscribe to multiple subscriptions. In this post, we will learn to make such mapping in database using hibernate 3.
Download source code
Sections in this post Proposed solution Writing owner entity Writing mapped entity Configuring entities in config file Testing the code
Proposed solution
To demonstrate many to many mapping, we will associate two entities i.e. ReaderEntity and SubscriptionEntity. Their database schema should look like this. Using these tables, any application can save multiple associations between readers and subscriptions.
Writing owner entity
Owner entity is the entity which is responsible make making the association and maintaining it. In our case, I am making ReaderEntity the owner entity. @JoinTable annotation has been used to make this association.
package hibernate.test.manyToMany.joinTable;
import java.io.Serializable;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
@Entity(name = "ReaderEntity")
@Table(name = "READER", uniqueConstraints = {
@UniqueConstraint(columnNames = "ID"),
@UniqueConstraint(columnNames = "EMAIL") })
public class ReaderEntity implements Serializable {
private static final long serialVersionUID = -1798070786993154676L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID", unique = true, nullable = false)
private Integer readerId;
@Column(name = "EMAIL", unique = true, nullable = false, length = 100)
private String email;
@Column(name = "FIRST_NAME", unique = false, nullable = false, length = 100)
private String firstName;
@Column(name = "LAST_NAME", unique = false, nullable = false, length = 100)
private String lastName;
@ManyToMany(cascade=CascadeType.ALL)
@JoinTable(name="READER_SUBSCRIPTIONS", joinColumns={@JoinColumn(referencedColumnName="ID")}
, inverseJoinColumns={@JoinColumn(referencedColumnName="ID")})
private Set<SubscriptionEntity> subscriptions;
public Integer getReaderId() {
return readerId;
}
public void setReaderId(Integer readerId) {
this.readerId = readerId;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Set<SubscriptionEntity> getSubscriptions() {
return subscriptions;
}
public void setSubscriptions(Set<SubscriptionEntity> subscriptions) {
this.subscriptions = subscriptions;
}
}
Writing mapped entity
Our mapped entity is SubscriptionEntity which is mapped to ReaderEntity using “mappedBy” attribute.
package hibernate.test.manyToMany.joinTable;
import java.io.Serializable;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
@Entity(name = "SubscriptionEntity")
@Table(name = "SUBSCRIPTION", uniqueConstraints = {
@UniqueConstraint(columnNames = "ID")})
public class SubscriptionEntity implements Serializable
{
private static final long serialVersionUID = -6790693372846798580L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID", unique = true, nullable = false)
private Integer subscriptionId;
@Column(name = "SUBS_NAME", unique = true, nullable = false, length = 100)
private String subscriptionName;
@ManyToMany(mappedBy="subscriptions")
private Set<ReaderEntity> readers;
public Integer getSubscriptionId() {
return subscriptionId;
}
public void setSubscriptionId(Integer subscriptionId) {
this.subscriptionId = subscriptionId;
}
public String getSubscriptionName() {
return subscriptionName;
}
public void setSubscriptionName(String subscriptionName) {
this.subscriptionName = subscriptionName;
}
public Set<ReaderEntity> getReaders() {
return readers;
}
public void setReaders(Set<ReaderEntity> readers) {
this.readers = readers;
}
}
Configuring entities in config file
We have available both entities to runtime, we have to add them in hibernate.cfg.xml file.
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatetest</property>
<property name="hibernate.connection.password">XXXXXX</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="hibernate.test.manyToMany.joinTable.ReaderEntity"/>
<mapping class="hibernate.test.manyToMany.joinTable.SubscriptionEntity"/>
</session-factory>
</hibernate-configuration>
Testing the code
Now, its time to test the code. I have written following code to test above entities.
package hibernate.test.manyToMany;
import hibernate.test.HibernateUtil;
import hibernate.test.manyToMany.joinTable.*;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
public class TestJoinTable
{
public static void main(String[] args)
{
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
//Add subscription
SubscriptionEntity subOne = new SubscriptionEntity();
subOne.setSubscriptionName("Entertainment");
SubscriptionEntity subTwo = new SubscriptionEntity();
subTwo.setSubscriptionName("Horror");
Set<SubscriptionEntity> subs = new HashSet<SubscriptionEntity>();
subs.add(subOne);
subs.add(subTwo);
//Add readers
ReaderEntity readerOne = new ReaderEntity();
readerOne.setEmail("demo-user1@mail.com");
readerOne.setFirstName("demo");
readerOne.setLastName("user");
ReaderEntity readerTwo = new ReaderEntity();
readerTwo.setEmail("demo-user2@mail.com");
readerTwo.setFirstName("demo");
readerTwo.setLastName("user");
Set<ReaderEntity> readers = new HashSet<ReaderEntity>();
readers.add(readerOne);
readers.add(readerTwo);
readerOne.setSubscriptions(subs);
readerTwo.setSubscriptions(subs);
session.save(readerOne);
session.save(readerTwo);
session.getTransaction().commit();
HibernateUtil.shutdown();
}
}
Output:
Hibernate: insert into READER (EMAIL, FIRST_NAME, LAST_NAME) values (?, ?, ?)
Hibernate: insert into SUBSCRIPTION (SUBS_NAME) values (?)
Hibernate: insert into SUBSCRIPTION (SUBS_NAME) values (?)
Hibernate: insert into READER (EMAIL, FIRST_NAME, LAST_NAME) values (?, ?, ?)
Hibernate: insert into READER_SUBSCRIPTIONS (readers_ID, subscriptions_ID) values (?, ?)
Hibernate: insert into READER_SUBSCRIPTIONS (readers_ID, subscriptions_ID) values (?, ?)
Hibernate: insert into READER_SUBSCRIPTIONS (readers_ID, subscriptions_ID) values (?, ?)
Hibernate: insert into READER_SUBSCRIPTIONS (readers_ID, subscriptions_ID) values (?, ?)
Download source code
Happy Learning !!







Discussion
Trackbacks/Pingbacks
[...] Many to many mapping is made between two entities where one can have relation with multiple other entity instances. For example, … [...]
[...] Many to many mapping is made between two entities where one can have relation with multiple other entity instances. For example, for a subscription service SubscriptionEntity and ReaderEntity can b… [...]