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. Make sure that you have .m2/settings.xml so that it has the jboss repository. See my Hibernate Tutorial Refined for details. For this tutorial. You will also need Tomcat. Download it. Unzip it somewhere else on your system.
Start this tutorial by downloading the base code and unzip it. The ‘$’ indicates the command prompt. Don’t type it. I assume you are using GNU/Linux for this tutorial. It will work on Windows too with minor modification.
$ wget http://brie.com/brian/hibernate/servlet/hib-serve.zip
$ unzip hib-serve.zip
$ cd hib-serve
Make sure that you are in the directory that contains the pom.xml file. Start the HSQLDB database as a server (all on one line):
$ mvn exec:java -Dexec.mainClass="org.hsqldb.Server" -Dexec.args="-database.0 file:target/data/tutorial"
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 pom.xml file.
$ mvn exec:java -Dexec.mainClass="org.hsqldb.util.DatabaseManagerSwing" -Dexec.args="-url jdbc:hsqldb:hsql://localhost/ -driver org.hsqldb.jdbcDriver"
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.
$ mkdir -p ./src/main/webapp/WEB-INF
$ wget http://brie.com/brian/hibernate/servlet/web.xml
$ mv web.xml ./src/main/webapp/WEB-INF
The following is the code for the Servlet. Download it, copy it to the correct place.
$ wget http://brie.com/brian/hibernate/servle/EventManagerServlet.java
$ mv EventManagerServlet.java src/main/java/org/hibernat/tutorial/web/
The EventManager had all private methods. Replace it with one that has public methods that can be used by the Servlet.
$ wget http://brie.com/brian/hibernate/servlet/EventManager.java
$ mv EventManager.java src/main/java/org/hibernate/tutorial/
Compile the project using the following Maven command:
$ mvn compile
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):
$ mvn exec:java -Dexec.mainClass="org.hibernate.tutorial.EventManager" -Dexec.args="addpersontoevent"
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.
$ mvn package
Now copy the hib-create.war to your Tomcat webapps directory. Replace <Tomcat-home> with where you unziped Tomcat.
$ cd target
$ cp hib-create.war <Tomcat-home>/webapps
Start Tomcat.
$ cd
$ bin/startup.sh
Tomcat should start. Go to your browser, and you should be able to access the servlet and enter an event.
http://localhost:8080/hib-serve/eventmanager
Add an event and you are good to go. This concludes the simple creation of a servlet with Hibernate and Maven.