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.

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.

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.

Configuring ant

Here is the contents of the build.xml file:

Before going any furtherer, lets make sure what we have so far works.

# cat src/uk/co/pookey/hibernate/HibernateUtil.java

If everything went to plan, you should see the following output:

Assuming this all works, now it’s time to move on to part 2!

13 thoughts on “Hibernate with MySQL – a beginners guide”

  1. 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

  2. In part 1 and part 2 I showed how to get up and running with hibernate. This part continues from where we left off. I have packaged up all the files form part 2, including all required jars, and the build script. The file is 4.5meg, and is down

  3. 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

  4. 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!

  5. 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

  6. Buildfile: build.xml

    clean:
    [delete] Deleting directory C:a_projectssamplesjavasamplesjoehibernatebin
    [mkdir] Created dir: C:a_projectssamplesjavasamplesjoehibernatebin

    copy-resources:
    [copy] Copying 2 files to C:a_projectssamplesjavasamplesjoehibernatebin

    compile:
    [javac] Compiling 3 source files to C:a_projectssamplesjavasamplesjoehibernatebin

    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

  7. 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)

  8. 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

Leave a Reply

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