{"id":27,"date":"2011-03-28T22:49:41","date_gmt":"2011-03-29T05:49:41","guid":{"rendered":"http:\/\/brie.com\/brian\/blog\/?p=27"},"modified":"2011-03-28T23:29:39","modified_gmt":"2011-03-29T06:29:39","slug":"hibernate-tutorial-refined","status":"publish","type":"post","link":"https:\/\/brie.com\/brian\/blog\/?p=27","title":{"rendered":"Hibernate Tutorial refined"},"content":{"rendered":"<p>This tutorial refines what is presented in the Tutorial section that is included with the Hibernate documentation for 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 release<\/a>. I had a few problems while working through the tutorial contained in the documentation, thus this is my attempt to simplify things so that others can also understand how Hibernate works. The code listings contain commands that you enter at the command line. The &#8216;$&#8217; character indicates that this is the prompt. Don&#8217;t type it. The code is also available as a <a href=\"http:\/\/www.brie.com\/brian\/hibernate\/start\/myfirst-hibernate.tgz\">full download<\/a> if you don&#8217;t feel like entering it in increments.<\/p>\n<p>First, we need to do a simple configuration setup to get Maven working. I assume that you have <a href=\"http:\/\/maven.apache.org\">Maven<\/a> installed. If you don&#8217;t grab a copy and install it. Copy the <em>settings.xml<\/em> (bellow) file into your Maven directory. You need to configure Maven so that it can see the JBoss repository for this tutorial to work. The file that needs modification is located in the <em>$HOME\/.m2\/settings.xml <\/em>. Download the copy I have and place it there. If you are already a Maven user and have custom settings, make the modifications I included in this <em>settings.xml <\/em>file.<\/p>\n<p><code>$ wget http:\/\/www.brie.com\/brian\/hibernate\/start\/settings.xml<br \/>\n$ cp settings.xml $HOME\/.m2<br \/>\n<\/code><\/p>\n<p>Make a directory for your first Hibernate Project<\/p>\n<p><code>$ mkdir myfirst-hibernate<br \/>\n<\/code><\/p>\n<p>This is the way you enter code. In this directory, you are going to place a <em>pom.xml<\/em>, which you can download from my site.\u00a0 The <em>pom.xml <\/em>is the Maven file that tells what files are in the project, the structure, etc.<\/p>\n<p><code>$ cd myfirst-hibernate<br \/>\n$ wget http:\/\/www.brie.com\/brian\/hibernate\/start\/pom.xml<\/code><\/p>\n<p>Now, start the database server using the following command (all on one line):<\/p>\n<p><code>$ mvn exec:java -Dexec.mainClass=\"org.hsqldb.Server\" -Dexec.args=\"-database.0 file:target\/data\/tutorial\"<\/code><br \/>\nStart the DB Manager in a separate window from the same directory. Enter the following (all on one line):<br \/>\n<code>$ mvn exec:java -Dexec.mainClass=\"org.hsqldb.util.DatabaseManagerSwing\" -Dexec.args=\"-url jdbc:hsqldb:hsql:\/\/localhost\/ -driver org.hsqldb.jdbcDriver\"<\/code><\/p>\n<p>Now you have a pom.xml file that details the dependencies for your project, a running database server (HSQLDB) and the database manager. Other than that, you have no code. We will fix that. Open yet a third terminal window. Your current working directory should be that of the pom.xml. Do an <em>ls <\/em>command just to be sure. Now create the following directories. If you are not used to using Maven, the source files go into<em> src\/main\/java <\/em>and the accompanying <em>.hbm.xml <\/em>files go into a resource directory. <em><br \/>\n<\/em><\/p>\n<p><code>$ mkdir -p src\/main\/java\/org\/hibernate\/tutorial\/domain<br \/>\n$ mkdir -p src\/main\/java\/org\/hibernate\/tutorial\/util<br \/>\n$ mkdir -p src\/main\/resources\/org\/hibernate\/tutorial\/domain<br \/>\n<\/code><\/p>\n<p>We will now create our first Java class: <em>Event.java. <\/em>To simplify this blog, just download the class file and either copy it to the directory or type it in. I assume that you are in the directory that has the pom.xml file.<\/p>\n<p><code>$ wget http:\/\/www.brie.com\/brian\/hibernate\/start\/Event.java<br \/>\n$ mv Event.java src\/main\/java\/org\/hibernate\/tutorial\/domain<\/code><\/p>\n<p>Now, we will add a corresponding mapping file from Hibernate that will map an Event object to the database. The mapping file maps each attribute to a column, yet when it comes to creating an identifier for the object, we let Hibernate handle it. The mapping file goes into a parallel directory to the that of the class.<\/p>\n<p><code><br \/>\n$ wget http:\/\/www.brie.com\/brian\/hibernate\/start\/Event.hbm.xml<br \/>\n$ mv Event.hbm.xml src\/main\/resources\/org\/hibernate\/tutorial\/domain<\/code><\/p>\n<p>The project now has a class (Event.java), and a mapping file, but now it misses the file that tells hibernate what database to connect to and what type of database it is. It is the main configuration file, <em>hibernate.cfg.xml <\/em>. It also tells Hibernate to create the database schema automatically, something good for a tutorial, but not quite as wise for production. As before, download it and move it to the proper place.<\/p>\n<p><code>$ wget http:\/\/www.brie.com\/brian\/hibernate\/start\/hibernate.cfg.xml<br \/>\n$ mv hibernate.cfg.xml src\/main\/resources<\/code><\/p>\n<p>Compile your code using Maven. It should compile without errors, assuming the repository, etc has not changed since this was written. You should execute this command at the same location as the <em>pom.xml<\/em> file is located.<\/p>\n<p><code><br \/>\n$ mvn compile<\/code><\/p>\n<p>If all is well, you now have a way to connect. The original tutorial off which this is based uses a utility class. I believe a different approach now works, but we will follow what works. Download the utility class and move it to the proper area. It uses a singleton pattern.<\/p>\n<p><code><br \/>\n$ wget http:\/\/www.brie.com\/brian\/hibernate\/start\/HibernateUtil.java<br \/>\n$ mv HibernateUtil.java src\/main\/java\/org\/hibernate\/tutorial\/util<br \/>\n<\/code><\/p>\n<p>Now is the time to do something with this domain that we have created. We will write a small program that creates an Event object. The event will get saved to the database automatically. First, get the code, for the EventManager, compile, and then run it. Refresh the dbmanager tool so that you can see the tables and query for your newly created Event.<\/p>\n<p><code><br \/>\n$ wget http:\/\/www.brie.com\/brian\/hibernate\/start\/EventManager1.java<br \/>\n$ mv EventManager1.java src\/main\/java\/org\/hibernate\/tutorial\/EventManager.java<br \/>\n$ mvn compile<br \/>\n$ mvn exec:java -Dexec.mainClass=\"org.hibernate.tutorial.EventManager\" -Dexec.args=\"store\"<br \/>\n$ mvn exec:java -Dexec.mainClass=\"org.hibernate.tutorial.EventManager\" -Dexec.args=\"list\"<br \/>\n<\/code><\/p>\n<p>If all worked well, check the graphical DataBase manager and query for the new Event. Enter the text, and press execute. You may have to press the <em>refresh <\/em>button to see the newly created table in the HSQLDB database.<\/p>\n<p><code><br \/>\nselect * from events<\/code><\/p>\n<p>This concludes this brief introduction to using Hibernate. I will add the second class in another blog where I will add the Person class presented in the original tutorial and the web application, for now. You can grab the full code from <a href=\"http:\/\/www.brie.com\/brian\/hibernate\/start\/myfirst-hibernate.tgz\">here<\/a>. Just remember to also add the <em>settings.xml<\/em> to your Maven configuration.<\/p>\n<p>-brian<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial refines what is presented in the Tutorial section that is included with the Hibernate documentation for the 3.6.2-Final release. I had a few problems while working through the tutorial contained in the documentation, thus this is my attempt &hellip; <a href=\"https:\/\/brie.com\/brian\/blog\/?p=27\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-27","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\/27","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=27"}],"version-history":[{"count":5,"href":"https:\/\/brie.com\/brian\/blog\/index.php?rest_route=\/wp\/v2\/posts\/27\/revisions"}],"predecessor-version":[{"id":30,"href":"https:\/\/brie.com\/brian\/blog\/index.php?rest_route=\/wp\/v2\/posts\/27\/revisions\/30"}],"wp:attachment":[{"href":"https:\/\/brie.com\/brian\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=27"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/brie.com\/brian\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=27"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/brie.com\/brian\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=27"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}