Take the 2-minute tour ×

Short version: How do I incorporate Hibernate into an application without using an IDE or build tool (without Eclipse or Maven for example)?

Long version:

Let's imagine for a moment I live in a country where Eclipse and Maven (and their ilk) are illegal. The government has decreed we all write our code with vi and compile it with javac. How can we incorporate Hibernate 4.2 into our applications? Which jar files need to be in our classpath? What configuration files must be created? (Names of config files would suffice, I don't need a description of the contents.)

I was a little surprised to find that such a set of instructions didn't exist already. Everything is written for Eclipse and/or Maven users.

Oh, we have been given a copy of hibernate-release-4.2.2.Final.gz.

share|improve this question
Please try out and share feedback. –  Siddharth May 23 '13 at 18:53
@Siddharth that's my plan. I just need a day or two to try it out. Lots going on at work today... –  John Fitzpatrick May 24 '13 at 10:08
add comment

2 Answers

up vote 1 down vote accepted

Not as crazy as you think. Yes, its difficult to get a answer from SO since all hibernate folks here use spring or maven or some very fancy tool to ease hibernate configuration.

Here is what I did.

Copied all library's to classpath. Created a hibernate.properties and hibernate.xml file in my src folder.

The properties file has


In your java main you can programmatically specify the mysql server, username and password (mind you took me 2 days to get this damn thing working, with little help from SO).

synchronized (this) {
        if (sessionFactory == null) {
            try {
                String connection = "jdbc:mysql://"
                        + Globals.DBSERVER.trim()
                        + "/mCruiseOnServerDB?autoReconnect=true&failOverReadOnly=false&maxReconnects=10";
                log.debug("Connection URL "+connection) ;
                Configuration configuration = new Configuration();
                        .setProperty("hibernate.connection.username", Globals.DB_USER_NAME.trim())
                        .setProperty("hibernate.connection.password", Globals.DB_PASSWORD.trim());
                        sessionFactory = configuration
                                .buildSessionFactory(new ServiceRegistryBuilder()
                } catch (Exception e) {
                log.fatal("Unable to create SessionFactory for Hibernate");

        if (sessionFactory == null) {
            log.fatal("Hibernate not configured.");

The XML file has

 <!DOCTYPE hibernate-configuration PUBLIC
        <!-- other mappings -->
        <mapping resource="com/mcruiseon/server/hibernate/UserDetails.hbm.xml" />

Make sure you have those hbm.xml file in a folder (inside of src) com.mcruiseon.server.hibernate (and /carpool in some cases).

The same folder should also have POJO's corresponding to the hbm file. I suggest that you keep your db column names EXACTLY same as your variable names makes life very simple (Contrary to what some silly people may advice). Dont use names like t_age instead use age (no acronyms).

Example of hbm file

<?xml version="1.0"?>
<!-- Generated 9 Jun, 2010 11:14:41 PM by Hibernate Tools 3.3.0.GA -->
    <class name="com.mcruiseon.common.concrete.UserDetailsConcrete"
        <id name="identityHash" type="java.lang.String">
            <column name="identityHash" />
            <generator class="assigned" />
        <property name="fullName" type="java.lang.String">
            <column name="fullName" />
        <!-- other property -->

Create a UserDetailsConcrete in com/mcruiseon/common/concrete folder

Ensure that you have all variables private (identityHash, fullName... etc). Ensure that you have getters and setters all public. Infact auto generate it (if you have eclipse, sorry). DONT have spelling mistakes and capitalization mistakes. Copy paste to make sure.

You should have it working.

share|improve this answer
One of the best written answers I've ever received. –  John Fitzpatrick May 24 '13 at 16:54
add comment

Short Answer : If it is a simple java application, if you are using version 3.3.2.GA all you would need is hibernate-core to be on the classpath. Using java -cp with appropriate arguments before you execute your main method will get you up and and running.
All other configuration files are boilerplate tutorials from hibernate.
Also, maven and ant are no magic bullets. They wont magically know what files are required. All of the details are required to be wired into the pom or the build file respectively.

share|improve this answer
add comment

Your Answer


