Hibernate with MySQL – a beginners guide


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!


Leave a comment

Your email address will not be published. Required fields are marked *

13 thoughts on “Hibernate with MySQL – a beginners guide

  • Ian P. Christian's personal blog

    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

  • anonymous coward

    You have an error in your ant script around line 35. You need to change the following from

    classname=”events.EventManager”

    to

    classname=”uk.co.pookey.hibernate.HibernateUtil”

    Thanks anyway, like what you have done so far 😉

    Cheers

  • Ams

    Hello,

    Can anyone let me know what should be the commands to compile and run this to get the above mentioned output?

    am using
    $>ant compile
    thats giving BUILD SUCCESSFUL but what to get rest of the output?

    Please help. Thanks!

  • John Uy

    Thanks for this easy-to-follow (considering the alternatives) tutorial. Had a few problems with my java install but nothing a troubleshoot didn’t fix.

    Thanks

  • jonas

    Buildfile: build.xml

    clean:
    [delete] Deleting directory C:\a_projects\samples\java\samples\joe\hibernate\bin
    [mkdir] Created dir: C:\a_projects\samples\java\samples\joe\hibernate\bin

    copy-resources:
    [copy] Copying 2 files to C:\a_projects\samples\java\samples\joe\hibernate\bin

    compile:
    [javac] Compiling 3 source files to C:\a_projects\samples\java\samples\joe\hibernate\bin

    run:
    [java] Exception in thread “main” java.lang.StackOverflowError
    [java] at java.lang.String.intern(Native Method)
    [java] at java.lang.Class.searchMethods(Class.java:2618)
    [java] at java.lang.Class.getMethod0(Class.java:2642)
    [java] at java.lang.Class.getMethod(Class.java:1579)
    [java] at org.apache.commons.logging.LogFactory.getContextClassLoader(LogFactory.java:438)
    [java] at org.apache.commons.logging.LogFactory$1.run(LogFactory.java:222)
    [java] at java.security.AccessController.doPrivileged(Native Method)
    [java] at org.apache.commons.logging.LogFactory.getFactory(LogFactory.java:218)

    … close to 1,000 lines of this exact same message …

    [java] at org.slf4j.impl.JCLLoggerFactory.getLogger(JCLLoggerFactory.java:69)
    [java] at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:161)
    [java] at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:155)
    [java] Java Result: 1

  • nevyn

    There seems to be an error…. at this point when you state:

    Before going any furtherer, lets make sure what we have so far works.
    # cat src/uk/co/pookey/hibernate/HibernateUtil.java

    There has been no instruction to put anything into the src directory. So where does the file HibernateUtil.java come from? and if you do locate it, should you copy just that file or the whole directory? What steps have you missed out? Also, you haven’t made it clear in the MySQL connector stage where you’re copying the file to (i.e. use absolute paths for clarity or state where you should be before running the command)

  • Seamoh

    Please notice anonymous coward comment:

    You have an error in your ant script around line 35. You need to change the following from

    classname=”events.EventManager”

    to

    classname=”uk.co.pookey.hibernate.HibernateUtil
    Thank you