Home >> Hibernate
Explaining a scenario on using many to many mapping between multiple
POJOs/Entities, while working with Hibernate Framework as ORM tool.
Hypothetical Scenario:
A Car dealer can have association with multiple Car manufacturing
factories and/or distributors from different sources. And each Source
can have one to more models from each segment, such as small, medium
and large Car segments.
In order to come up with final analysis models (Entity-Relationship Model),
one may have to start with thorough analysis on this problem statement.
While doing analysis, I have managed to break this problem statement
into various Entities like as follows:
|
|  |
|
Dealer
Car
Distributor
Manufacturer
Segment (like small, medium or large car segments)
Model
1. Each Distributor may have one or more Segment(s)
2. Each Segment may have one or more Model(s).
3. Each Dealer may have one or more Distributor(s).
4. Each Distributor may have one or more Manufacturer(s) to deal with.
5. Each Dealer ultimately works with one or more Car(s).
There can be many more relationships and many more Entities, but as of
now I have managed to identified these above mentioned Entity(s) and
their associations. If anyone has any more thought on this, and if
interested please share your thought on this using below comments section.
When I re-visited the above 5 described associations, I felt that
there are some disconnect between these relationships, and then I thought
to re-write these associations one more time with properly rearranging
those five lines.
1. Each Manufacturer may have one or more Distributor(s) to deal with.
1.1. Each Distributor may be working with one or more Manufacturer(s).
2. Each Distributor may have one or more Segment(s).
3. Each Segment may have one or more Model(s).
4. Each Model may have one Car type associated with it.
5. Each Dealer may have one or more Distributor(s) to work with.
5.1. Each Distributor may have one or more Dealer(s) to work with.
I think, now this is pretty connected and easy to interprete and define
Domain object and their association/aggregation/composition.
Looking at point number 1 and 1.1, Manufacturer and Distributor share
many to many type of mapping.
--------
Looking at point number 2, Distributor and Segment has one to many
mapping between these.
--------
Looking at point number 3, Segment and Model share one to many mapping
between these.
--------
Looking at point number 4, Model and Car has one to one mapping.
--------
Referring to point number 5 and 5.1, Dealer and Distributor share
many to many mapping type.
--------
Using ArgoUML (web site http://argouml.tigris.org), I have created a Class Diagram
as shown below:

In order to demonstrate mapping between domain classes and tables
from database (I have used HQLDB[http://hsqldb.sourceforge.net] for this example).
Just by refering above class diagram, I defined following POJO classes
Manufacturer.java
Distributor.java
Segment.java
Dealer.java
Model.java
Car.java
And table names as follows:
MANUFACTURER_DETAILS
DISTRIBUTOR_DETAILS
SEGMENT_DETAILS
DEALER_DETAILS
CAR_MODEL
CAR_DETAILS
Manufacture and Distributor shares many to many type of mapping,
so we have associations with multiplicity as 1..* for both these
POJOs.
Manufacturer.java
package example;
import java.util.Collection;
public class Manufacturer {
private long id;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
private Collection<Distributor> distributors;
public Collection<Distributor> getDistributors() {
return distributors;
}
public void setDistributors(Collection<Distributor> distributors) {
this.distributors = distributors;
}
}
Distributor.java
package example;
import java.util.Collection;
public class Distributor {
private long id;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
private Collection<Manufacturer> manufacturers;
private Collection<Segment> segments;
private Collection<Dealer> dealers;
public Collection<Dealer> getDealers() {
return dealers;
}
public void setDealers(Collection<Dealer> dealers) {
this.dealers = dealers;
}
public Collection<Segment> getSegments() {
return segments;
}
public void setSegments(Collection<Segment> segments) {
this.segments = segments;
}
public Collection<Manufacturer> getManufacturers() {
return manufacturers;
}
public void setManufacturers(Collection<Manufacturer> manufacturers) {
this.manufacturers = manufacturers;
}
}
many to many mapping between Manufacturer and Distributor can be
doen this way as follows:
<class name="Manufacturer" table="MANUFACTURER_DETAILS">
<id name="id" type="long"/>
<set name="distributors" table="MANUFACTURER_DISTRIBUTOR">
<key column="DISTRIBUTOR_ID"/>
<many-to-many class="Distributor" column="MANUFACTURER_ID"/>
</set>
</class>
<class name="Distributor" table="DISTRIBUTOR_DETAILS">
<id name="id" type="long"/>
<set name="manufacturers" table="MANUFACTURER_DISTRIBUTOR">
<key column="MANUFACTURER_ID"/>
<many-to-many class="Manufacturer" column="DISTRIBUTOR_ID"/>
</set>
</class>
So for this many-to-many mapping there are three tables used,
MANUFACTURER_DETAILS - Individual table for one POJO
DISTRIBUTOR_DETAILS - Individual table for another POJO
MANUFACTURER_DISTRIBUTOR - Common table for achieving many to many mapping.
Test program could be as simple as the code snippet below:
sessionFactory = new Configuration()
.configure().buildSessionFactory();
Distributor dist1 = new Distributor();
dist1.setId(1001);
Distributor dist2 = new Distributor();
dist2.setId(2001);
Collection<Distributor> distCol = new HashSet<Distributor>();
distCol.add(dist1);
distCol.add(dist2);
Manufacturer manuf = new Manufacturer();
manuf.setId(100);
manuf.setDistributors(distCol);
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
tx.begin();
session.saveOrUpdate(dist1);
session.saveOrUpdate(dist2);
session.saveOrUpdate(manuf);
session.flush();
tx.commit();
session.close();
Of course these source codes found in this Page are just made
simple enough to quickly write and execute and test this example,
and these may not be fit for any other purposes.
One has to setup own example execution environment appropriately,
including setting appropriate JAR files, including following
files.
asm.jar
cglib-2.1.3.jar
commons-collections-2.1.1.jar
commons-logging-1.0.4.jar
dom4j-1.6.1.jar
hibernate3.jar
hsqldb.jar
jta.jar
I have used Eclipse as IDE for this example in order to quickly
setup environment and classpath, and execute this example quick enough.
Your thought on improving this example is most welcome.
Please use following comment section for writing to me.
All your comments will be moderated and will be visible here on
this page, if found suitable or relevant to this topic.
If anything missed out , please let me know at
techienjoy at yahoo . com