{"id":35,"date":"2011-03-31T00:44:25","date_gmt":"2011-03-31T07:44:25","guid":{"rendered":"http:\/\/brie.com\/brian\/blog\/?p=35"},"modified":"2019-05-30T09:27:41","modified_gmt":"2019-05-30T16:27:41","slug":"adding-many-to-many-in-hibernate","status":"publish","type":"post","link":"https:\/\/brie.com\/brian\/blog\/?p=35","title":{"rendered":"Adding many-to-many in Hibernate"},"content":{"rendered":"<p>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 <a title=\"Hibernate Tutorial refined\" href=\"http:\/\/brie.com\/brian\/blog\/?p=27\">simple mapped class<\/a> describe in my <a title=\"Hibernate Tutorial refined\" href=\"http:\/\/brie.com\/brian\/blog\/?p=27\">Hibernate getting started<\/a> post. Once again, this post is built off the documentation included in the <a href=\"http:\/\/sourceforge.net\/projects\/hibernate\/files\/hibernate3\/3.6.2.Final\/hibernate-distribution-3.6.2.Final-dist.tar.gz\">3.6.2-Final<\/a> 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 <a title=\"Hibernate Tutorial refined\" href=\"http:\/\/brie.com\/brian\/blog\/?p=27\">Start post<\/a> for instructions for Maven. Then, we will follow the steps to add many-to-many relationship. The following are the commands to enter. Don&#8217;t type the &#8216;$&#8217; character.<\/p>\n<p><code><br \/>\n$ wget <a href=\"http:\/\/brie.com\/brian\/hibernate\/step02\/myfirst-hibernate.tgz\">http:\/\/brie.com\/brian\/hibernate\/step02\/myfirst-hibernate.tgz<\/a><br \/>\n$ tar zxf myfirst-hibernate.tgz<br \/>\n$ cd myfirst-hibernate<\/code><\/p>\n<p>Now, open two separate windows with the same current working directory and start the database and the <a href=\"http:\/\/www.hsqldb.org\">HSQL<\/a> 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:<\/p>\n<p><code><br \/>\n$ mvn exec:java -Dexec.mainClass=\"org.hsqldb.Server\" -Dexec.args=\"-database.0 file:target\/data\/tutorial\"<\/code><\/p>\n<p><code><br \/>\n$ mvn exec:java -Dexec.mainClass=\"org.hsqldb.util.DatabaseManagerSwing\" -Dexec.args=\"-url jdbc:hsqldb:hsql:\/\/localhost\/ -driver org.hsqldb.jdbcDriver\"<\/code><\/p>\n<p>Add the Person class<\/p>\n<p><code><br \/>\n$ wget <a href=\"http:\/\/brie.com\/brian\/hibernate\/step02\/Person.java\">http:\/\/brie.com\/brian\/hibernate\/step02\/Person.java<\/a><br \/>\n$ mv Person.java src\/main\/java\/org\/hibernate\/tutorial\/domain<\/code><\/p>\n<p>Now that you have added the Person class, add the mapping for Hibernate.<\/p>\n<p><code><br \/>\n$ wget <a href=\"http:\/\/brie.com\/brian\/hibernate\/step02\/Person.hbm.xml\">http:\/\/brie.com\/brian\/hibernate\/step02\/Person.hbm.xml<\/a><br \/>\n$ mv Person.hbm.xml src\/main\/resources\/org\/hibernate\/tutorial\/domain\/<\/code><\/p>\n<p>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.<\/p>\n<p><code><br \/>\n&lt;set name=\"events\" table=\"PERSON_EVENT\"&gt;<br \/>\n&lt;key column=\"PERSON_ID\"\/&gt;<br \/>\n&lt;many-to-many column=\"EVENT_ID\"\/&gt;<br \/>\n&lt;\/set&gt;<\/code><\/p>\n<p>You will also notice that each Person has a set of email addresses. This is mapped with the following configuration on the hbm file.<\/p>\n<p><code><br \/>\n&lt;set name=\"emailAddresses\" table=\"PERSON_EMAIL_ADDR\"&gt;<br \/>\n&lt;key column=\"PERSON_ID\"\/&gt;<br \/>\n&lt;element type=\"string\" column=\"EMAIL_ADDR\"\/&gt;<\/code><\/p>\n<p>Now, fetch the configuration for hibernate.<\/p>\n<p><code><br \/>\n$ wget <a href=\"http:\/\/brie.com\/brian\/hibernate\/step02\/hibernate.cfg.xml\">http:\/\/brie.com\/brian\/hibernate\/step02\/hibernate.cfg.xml<\/a><br \/>\n$ mv hibernate.cfg.xml src\/main\/resources<\/code><\/p>\n<p>If you examine the <em>hibernate.cfg.xml <\/em>file, you will notice that it has the following line that tells Hibernate about the Person class.<\/p>\n<p><code>&lt;mapping resource=\"org\/hibernate\/tutorial\/domain\/Person.hbm.xml\"\/&gt;<\/code><\/p>\n<p>Now modify the EventManager.java class, so that we can create a person and an event, and associate them with each other<\/p>\n<p><code><br \/>\n$ wget <a href=\"http:\/\/brie.com\/brian\/hibernate\/step02\/EventManager.java\">http:\/\/brie.com\/brian\/hibernate\/step02\/EventManager.java<\/a><br \/>\n$ mv EventManager.java src\/main\/java\/org\/hibernate\/tutorial<\/code><\/p>\n<p>If you examine the code for EventManager, you will notice that it now will check for the argument named &#8220;addpersontoevent&#8221;. Compile the code using the following command. If all goes well, you should see &#8220;success&#8221;.<\/p>\n<p><code>$ mvn compile<\/code><\/p>\n<p>Run the newly compiled program using the following Maven command (all on one line). It will run the Main class with the &#8220;addpersontoevent&#8221; argument.<\/p>\n<p><code><br \/>\n$ mvn exec:java -Dexec.mainClass=\"org.hibernate.tutorial.EventManager\" -Dexec.args=\"addpersontoevent\"<\/code><\/p>\n<p>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.<\/p>\n<p><code><br \/>\nselect * from events<br \/>\nselect * from person<br \/>\nselect * from person_event<br \/>\nselect * from person_email_addr<\/code><\/p>\n<p>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.<br \/>\n<code><br \/>\n&lt;property name=\"hbm2ddl.auto\"&gt;update&lt;\/property&gt;<br \/>\n<\/code><\/p>\n<p>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.<\/p>\n<p><a href=\"http:\/\/brie.com\/brian\/hibernate\/step02\/step02-hibernate.tgz\">http:\/\/brie.com\/brian\/hibernate\/step02\/step02-hibernate.tgz<\/a><\/p>\n<p>brian<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/brie.com\/brian\/blog\/?p=35\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-35","post","type-post","status-publish","format-standard","hentry","category-hibernate"],"_links":{"self":[{"href":"https:\/\/brie.com\/brian\/blog\/index.php?rest_route=\/wp\/v2\/posts\/35","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/brie.com\/brian\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/brie.com\/brian\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/brie.com\/brian\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/brie.com\/brian\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=35"}],"version-history":[{"count":4,"href":"https:\/\/brie.com\/brian\/blog\/index.php?rest_route=\/wp\/v2\/posts\/35\/revisions"}],"predecessor-version":[{"id":183,"href":"https:\/\/brie.com\/brian\/blog\/index.php?rest_route=\/wp\/v2\/posts\/35\/revisions\/183"}],"wp:attachment":[{"href":"https:\/\/brie.com\/brian\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=35"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/brie.com\/brian\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=35"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/brie.com\/brian\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=35"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}