3

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.

John Fitzpatrick
  • 4,207
  • 7
  • 48
  • 71

2 Answers2

1

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

hibernate.connection.driver_class=com.mysql.jdbc.Driver
hsqldb.write_delay_millis=0
shutdown=true
hibernate.connection.pool_size=2
hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect

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();
                configuration
                        .setProperty("hibernate.connection.username", Globals.DB_USER_NAME.trim())
                        .setProperty("hibernate.connection.password", Globals.DB_PASSWORD.trim());
                configuration.configure();
                        sessionFactory = configuration
                                .buildSessionFactory(new ServiceRegistryBuilder()
                                .applySettings(configuration.getProperties()) 
                                .buildServiceRegistry());
                } catch (Exception e) {
                log.fatal("Unable to create SessionFactory for Hibernate");
                log.fatal(e.getMessage());
                log.fatal(e);
                e.printStackTrace();
                }
        }

        if (sessionFactory == null) {
            log.fatal("Hibernate not configured.");
            System.exit(0);
        }

The XML file has

<?xml version='1.0' encoding='UTF-8'?>
 <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- other mappings -->
        <mapping resource="com/mcruiseon/server/hibernate/UserDetails.hbm.xml" />
    </session-factory>
</hibernate-configuration>

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"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated 9 Jun, 2010 11:14:41 PM by Hibernate Tools 3.3.0.GA -->
<hibernate-mapping>
    <class name="com.mcruiseon.common.concrete.UserDetailsConcrete"
        table="userDetails">
        <id name="identityHash" type="java.lang.String">
            <column name="identityHash" />
            <generator class="assigned" />
        </id>
        <property name="fullName" type="java.lang.String">
            <column name="fullName" />
        </property>
        <!-- other property -->
    </class>
</hibernate-mapping>

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.

Community
  • 1
  • 1
Siddharth
  • 9,349
  • 16
  • 86
  • 148
1

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

happybuddha
  • 1,271
  • 2
  • 20
  • 40