This article is an introduction from getting hibernate working from scratch. The only dependency is that you have a text editor, a working Java install, and ant. In this part of my guide, we will simply setup our environment, download some needed jar files and ensure that we can build an run a simple Java application. In the follow up part to this article, I will cover connecting to a database and doing a few interesting things with Hibernate.
Lets start by setting up our directory structure. I like to include required jar files in 'lib'. This means that all the dependencies that my code needs are included within the same folder. This is my preference, if you wish to do it another way, you'll have to modify my code below.
# the top level folder for our project
$ mkdir hibernate
$ cd hibernate
$ mkdir lib
$ mkdir src
Obtain and extract Hibernate
Now, lets download hibernate from http://www.hibernate.org/6.html , at time of writing the 3.2.6.ga release was current, so that's what I downloaded. Extract the downloaded file into your lib folder.
$ cd lib
$ tar -xvzf hibernate-3.2.6.ga.tar.gz
There are a lot of lib files distributed with hibernate, you do not need them all. To see more information about what each one does, have a look at the file hibernate-3.2/lib/_README.txt
Obtain and extract MySQL Connector
To connect to MySQL with hibernate, you will need the MySQL connector. Download mysql connector from http://dev.mysql.com/downloads/connector/j/5.1.html. Extract this file into your lib folder, and copy over the required .jar file.
$ tar -xvzf mysql-connector-java-5.1.6.tar.gz
$ cp mysql-connector-java-5.1.6/mysql-connector-java-5.1.6-bin.jar .
Configuring ant
Here is the contents of the build.xml file:
<project name="hibernate-tutorial" default="compile">
<property name="sourcedir" value="${basedir}/src"/>
<property name="targetdir" value="${basedir}/bin"/>
<property name="librarydir" value="${basedir}/lib"/>
<path id="libraries">
<fileset dir="${librarydir}">
<include name="*.jar"/>
</fileset>
</path>
<target name="clean">
<delete dir="${targetdir}"/>
<mkdir dir="${targetdir}"/>
</target>
<target name="compile" depends="clean, copy-resources">
<javac srcdir="${sourcedir}"
destdir="${targetdir}"
classpathref="libraries">
<compilerarg value="-Xlint"/>
</javac>
</target>
<target name="copy-resources">
<copy todir="${targetdir}">
<fileset dir="${sourcedir}">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target name="run" depends="compile">
<java fork="true" classname="events.EventManager" classpathref="libraries">
<classpath path="${targetdir}"/>
<arg value="${action}"/>
</java>
</target>
</project>
Before going any furtherer, lets make sure what we have so far works.
# cat src/uk/co/pookey/hibernate/HibernateUtil.java
package uk.
co.
pookey.
hibernate;
public class HibernateUtil
{ public static void main
(String[] args
) { System.
out.
println("ok!");
}}
If everything went to plan, you should see the following output:
# ant run
Buildfile: build.xml
clean:
[delete] Deleting directory /home/pookey/hibernate/bin
[mkdir] Created dir: /home/pookey/hibernate/bin
copy-resources:
[copy] Copying 1 file to /home/pookey/hibernate/bin
[copy] Copied 4 empty directories to 2 empty directories under /home/pookey/hibernate/bin
compile:
[javac] Compiling 1 source file to /home/pookey/hibernate/bin
[javac] warning: [path] bad path element "/usr/share/ant/lib/xml-apis.jar": no such file or directory
[javac] warning: [path] bad path element "/usr/share/ant/lib/xercesImpl.jar": no such file or directory
[javac] warning: [path] bad path element "/usr/share/ant/lib/xalan.jar": no such file or directory
[javac] 3 warnings
run:
[java] ok!
BUILD SUCCESSFUL
Total time: 0 seconds
Assuming this all works, now it's time to move on to part 2!
This post is a follow on from my previous post Hibernate with MySQL - a beginners guide - part 1. Having followed part 1, you should have a simple java application that prints 'ok', an ant build script, and downloaded hibernate and the MySQL connecto
Tracked: Aug 01, 18:17