{"id":42,"date":"2011-04-11T23:56:58","date_gmt":"2011-04-12T06:56:58","guid":{"rendered":"http:\/\/brie.com\/brian\/blog\/?p=42"},"modified":"2011-05-26T09:47:08","modified_gmt":"2011-05-26T16:47:08","slug":"making-a-servlet-with-hibernate","status":"publish","type":"post","link":"https:\/\/brie.com\/brian\/blog\/?p=42","title":{"rendered":"Making a Servlet with Hibernate"},"content":{"rendered":"<p>These are the details I followed for making a servlet with Hibernate. This will take off from the last post with <a title=\"Adding many-to-many in Hibernate\" href=\"http:\/\/brie.com\/brian\/blog\/?p=35\">Many to Many<\/a>. It is a rehash from the original Hibernate documentation.\u00a0 Make sure that you have .m2\/settings.xml so that it has the jboss repository. See my <a title=\"Hibernate Tutorial refined\" href=\"http:\/\/brie.com\/brian\/blog\/?p=27\">Hibernate Tutorial Refined<\/a> for details. For this tutorial. You will also need <a href=\"http:\/\/tomcat.apache.org\/\">Tomcat<\/a>. Download it. Unzip it somewhere else on your system.<\/p>\n<p>Start this tutorial by downloading the base code and unzip it. The &#8216;$&#8217; indicates the command prompt. Don&#8217;t type it. I assume you are using GNU\/Linux for this tutorial. It will work on Windows too with minor modification.<\/p>\n<p><code><br \/>\n<\/code><code>$ wget http:\/\/brie.com\/brian\/hibernate\/servlet\/hib-serve.zip<br \/>\n$ unzip hib-serve.zip<br \/>\n$ cd hib-serve<\/code><\/p>\n<p>Make sure that you are in the directory that contains the <em>pom.xml<\/em> file. Start the HSQLDB database as a server (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><\/p>\n<p>Open a separate command prompt window and start the Database Manager (all on one line). Make sure that you are in the directory with the <em>pom.xml <\/em>file.<\/p>\n<p><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>Make the directory for the Servlet configuration. Download the configuration file and move it to the correct place. Basically, this configures the url that maps to the servlet class.<\/p>\n<p><code>$ mkdir -p .\/src\/main\/webapp\/WEB-INF<br \/>\n$ wget http:\/\/brie.com\/brian\/hibernate\/servlet\/web.xml<br \/>\n$ mv web.xml .\/src\/main\/webapp\/WEB-INF<\/code><\/p>\n<p>The following is the code for the Servlet. Download it, copy it to the correct place.<\/p>\n<p><code>$ wget http:\/\/brie.com\/brian\/hibernate\/servle\/EventManagerServlet.java<br \/>\n$ mv EventManagerServlet.java src\/main\/java\/org\/hibernat\/tutorial\/web\/<\/code><\/p>\n<p>The EventManager had all private methods. Replace it with one that has public methods that can be used by the Servlet.<\/p>\n<p><code>$ wget http:\/\/brie.com\/brian\/hibernate\/servlet\/EventManager.java<br \/>\n$ mv EventManager.java src\/main\/java\/org\/hibernate\/tutorial\/<\/code><\/p>\n<p>Compile the project using the following Maven command:<\/p>\n<p><code>$ mvn compile<\/code><\/p>\n<p>This example uses the hbm2ddl.auto property, so it will automatically create the schema when the program is run. Now add a person to the event using the Event manager from the command line with the following command (all on one line):<\/p>\n<p><code>$ mvn exec:java -Dexec.mainClass=\"org.hibernate.tutorial.EventManager\" -Dexec.args=\"addpersontoevent\"<\/code><\/p>\n<p>Check the database manager. You should see an event. Now, create a war file using the following command. It will create a hib-create.war file in the target subdirectory.<\/p>\n<p><code>$ mvn package<\/code><\/p>\n<p>Now copy the hib-create.war to your Tomcat webapps directory. Replace <em>&lt;Tomcat-home&gt; <\/em>with where you unziped Tomcat.<\/p>\n<p><code>$ cd target<br \/>\n$ cp hib-create.war &lt;Tomcat-home&gt;\/webapps<br \/>\n<\/code><\/p>\n<p>Start Tomcat.<br \/>\n<code>$ cd<br \/>\n$ bin\/startup.sh<\/code><\/p>\n<p>Tomcat should start. Go to your browser, and you should be able to access the servlet and enter an event.<\/p>\n<p><em>http:\/\/localhost:8080\/hib-serve\/eventmanager<\/em><\/p>\n<p>Add an event and you are good to go. This concludes the simple creation of a servlet with Hibernate and Maven.<em><br \/>\n<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>These are the details I followed for making a servlet with Hibernate. This will take off from the last post with Many to Many. It is a rehash from the original Hibernate documentation.\u00a0 Make sure that you have .m2\/settings.xml so &hellip; <a href=\"https:\/\/brie.com\/brian\/blog\/?p=42\">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-42","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\/42","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=42"}],"version-history":[{"count":5,"href":"https:\/\/brie.com\/brian\/blog\/index.php?rest_route=\/wp\/v2\/posts\/42\/revisions"}],"predecessor-version":[{"id":53,"href":"https:\/\/brie.com\/brian\/blog\/index.php?rest_route=\/wp\/v2\/posts\/42\/revisions\/53"}],"wp:attachment":[{"href":"https:\/\/brie.com\/brian\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=42"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/brie.com\/brian\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=42"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/brie.com\/brian\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=42"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}