Hibernate with MySQL – a beginners guide – part 3

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 downloadable here: hibernate.tar.gz. Having got the the unit testing setup and running in part 2, I’ll not cover creating more tests here. When developing your own code though, you should of course be writing tests as you go along!

In this part, I’ll show you how to load this object from the database, edit it, and save it back again. Relationships (for example, linking comments to the blog post) are quite complicated, so I’ll leave that to part 4. This part is almost entirely shown though example code – however I think the code speaks for itself.

The first thing I’ll demonstrate is deleting all records from a table. We’ll add this to the application so that everytime it is run, we start with an empty table. Before doing that, I’m going to create a new class which I’ll use to host the main() method, as it doesn’t really belong in the HibernateUtil package as it was in part 2.

First, build.xml needs modifying so that it knows what class to run when we execute ant run.

Here’s several of the features of hibernate shown off in a single class. The inline comments (forgive the lack of javadoc comments, my blog highlight engine didn’t like them!) explain what’s going on. When fetching data from Hibernate, you can use a Criteria API (as shown in getBlogsBySubject()), HQL (deleteAll()) or even by using SQL.

Below shows the output of running the application now, showing all the SQL statements.

10 thoughts on “Hibernate with MySQL – a beginners guide – part 3”

  1. Your tutorials are great and very easy!!!!

    I had problems with this last one but was able to find the problem.

    Error I got:

    [javac] Compiling 4 source files to C:hibernatebin
    [javac] C:hibernatesrcukcopookeyBlogApp.java:57: warning: [unchecked]
    unchecked conversion
    [javac] found : java.util.List
    [javac] required: java.util.List
    [javac] .list();
    [javac] ^
    [javac] 1 warning

    [java] log4j:WARN No appenders could be found for logger (org.hibernate.cfg
    [java] log4j:WARN Please initialize the log4j system properly.
    [java] Exception in thread “main” java.lang.NoClassDefFoundError: antlr/ANT
    [java] at org.hibernate.hql.ast.ASTQueryTranslatorFactory.createQueryTr

    So what I did is added antlr-2.7.6.jar to the C:hibernatelib.

    Thank you for the help!!!!!

  2. Superb,
    Brilliant tutorial for the beginners like me.I got to know the abc of hibernate.
    Great work and here is a BIG THANKS.
    keep making life arround better.keep it up.

  3. Hi all,
    while commiting session.getTransaction().commit(); is being used,which in turn gets a new transaction and commits it rather than commiting the intended transaction on which operations has been performed.

    so I think we need to collect the Transaction in a Transaction type variable and then commit the same so that this transcation will be committed to the database.

  4. Wonderfull, all your tuto are wonderfull… I do all the tuto with eclipse and it is perfect. Just 2 things:
    -update the jar list
    -change in part 1


    Thanks a lot.

  5. I have searched in a lot of web tutorials and this is the best tutorial that I have found.

    Just a little comment, I would be better if you add the sql script. in the tutorial.

    This is the MySQL Script for the people that could be need it.

    mysql > create database hibernate;
    mysql > create table blog (
    > id INT(10),
    > subject VARCHAR(100),
    > body VARCHAR(2000),
    > createAt Date);

    Thanks & Regards

Leave a Reply

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