Adding many-to-many in Hibernate

In this post, I will demonstrate how to do a many-to-many relation in Hibernate. A Person(s) can attend many Event(s) and an Event(s) can have many Person(s). This follows the initial creation of the simple mapped class describe in my Hibernate getting started post. Once again, this post is built off the documentation included in the 3.6.2-Final release and the frustration getting the tutorial to work. First, download the working version (commands follow for GNU/Linux) of a simple mapped class, untar it, and change into the directory containing the sample code. You will need Maven installed too. If not, check my Start post for instructions for Maven. Then, we will follow the steps to add many-to-many relationship. The following are the commands to enter. Don’t type the ‘$’ character.


$ wget http://brie.com/brian/hibernate/step02/myfirst-hibernate.tgz
$ tar zxf myfirst-hibernate.tgz
$ cd myfirst-hibernate

Now, open two separate windows with the same current working directory and start the database and the HSQL Database Manager. If for some reason, you already have the database running from the first example, you can leave it running. Type the following all on one line:


$ mvn exec:java -Dexec.mainClass="org.hsqldb.Server" -Dexec.args="-database.0 file:target/data/tutorial"


$ mvn exec:java -Dexec.mainClass="org.hsqldb.util.DatabaseManagerSwing" -Dexec.args="-url jdbc:hsqldb:hsql://localhost/ -driver org.hsqldb.jdbcDriver"

Add the Person class


$ wget http://brie.com/brian/hibernate/step02/Person.java
$ mv Person.java src/main/java/org/hibernate/tutorial/domain

Now that you have added the Person class, add the mapping for Hibernate.


$ wget http://brie.com/brian/hibernate/step02/Person.hbm.xml
$ mv Person.hbm.xml src/main/resources/org/hibernate/tutorial/domain/

If you examine the contents of the .hbm.xml file, the following creates the many-to-many mapping. The table used to hold the many-to-many mapping will be named PERSON_EVENT. The id for the Person will be mapped to the PERSON_ID column, and the id of the Event will be mapped to the EVENT_ID column.


<set name="events" table="PERSON_EVENT">
<key column="PERSON_ID"/>
<many-to-many column="EVENT_ID"/>
</set>

You will also notice that each Person has a set of email addresses. This is mapped with the following configuration on the hbm file.


<set name="emailAddresses" table="PERSON_EMAIL_ADDR">
<key column="PERSON_ID"/>
<element type="string" column="EMAIL_ADDR"/>

Now, fetch the configuration for hibernate.


$ wget http://brie.com/brian/hibernate/step02/hibernate.cfg.xml
$ mv hibernate.cfg.xml src/main/resources

If you examine the hibernate.cfg.xml file, you will notice that it has the following line that tells Hibernate about the Person class.

<mapping resource="org/hibernate/tutorial/domain/Person.hbm.xml"/>

Now modify the EventManager.java class, so that we can create a person and an event, and associate them with each other


$ wget http://brie.com/brian/hibernate/step02/EventManager.java
$ mv EventManager.java src/main/java/org/hibernate/tutorial

If you examine the code for EventManager, you will notice that it now will check for the argument named “addpersontoevent”. Compile the code using the following command. If all goes well, you should see “success”.

$ mvn compile

Run the newly compiled program using the following Maven command (all on one line). It will run the Main class with the “addpersontoevent” argument.


$ mvn exec:java -Dexec.mainClass="org.hibernate.tutorial.EventManager" -Dexec.args="addpersontoevent"

If all went well, the program will have run an inserted the data into the Person table, the Event Table, and the corresponding associations into the Person_Event table to map the relationship between the Person and the Event. The email address set is mapped into the Person_Email_Addr table. Use the following queries to view the results in the database. Enter the following queries one at a time into the HSQL database manager. Before doing so, you may have to refresh the database using the Ctrl-R command.


select * from events
select * from person
select * from person_event
select * from person_email_addr

You will note that in the above example that the tables were created automatically. This is because the hibernate.cfg.xml file has been configured to automatically create the database schema using the following directive.

<property name="hbm2ddl.auto">update</property>

This concludes the demonstration for mapping a many-to-many classes for Hibernate. If something happened with your modifications, check the following download that has the completed additions.

http://brie.com/brian/hibernate/step02/step02-hibernate.tgz

brian

About Brian Lavender

I recently obtained my Masters in Computer Science. I like hacking GNU/Linux and hacking different programming languages in general.
This entry was posted in Hibernate. Bookmark the permalink.

2 Responses to Adding many-to-many in Hibernate

  1. PandaBear says:

    I can’t really google and get to your blog. Why’s that? I was just thinking that since you are writing these blogs, you may want to make it available also to those who may not access your blog through your website directly. i.e. I was hoping that I can just google something like “hibernate tutorial” and have your blogs popped-up on the search, as these blogs are easy to follow and they should be shared to the general public….I think.

    • It turns out that I had robots.txt denying all robots.txt long ago for some dumb reason. I have since taken that out, so hopefully Google, and other engines, will index it now.

Comments are closed.