Sunday, June 12, 2011

Hibernate and Java command-line compilation, the understanding

1. Prepare a directory(java package) that won't have a collision with other package name, ideally this is your reverse domain name. Mine is http://anicehumble.com. So the package goes to com/anicehumble directory.
$ mkdir -p source/com/anicehumble

2. Create a directory for your compiled class
$ mkdir class

3. Create a directory for libraries you are using, this is where you will put jar(java archive) files, e.g. Hibernate
$ mkdir lib



4. Then fire up your favorite text editor, mine is vi ;-)
$ vim source/com/anicehumble/TryHib.java

5. Then type this:
package com.anicehumble;

public class TryHib {
        public static void main(String[] args) {
                System.out.println("Hello World");
        }
}

6. Then compile:
$ javac -d class source/com/anicehumble/TryHib.java

The -d class is a directive for javac compiler to put your compiled class(es) to class directory, the compiled class(es) will be put on the directory based on the name you specified on the topmost line of your java code, which is package com.anicehumble;; with this, your .class file(s) will be put in class/com/anicehumble. Make it a habit to use -d yourdirectoryhere so your source code and compiled code is more organized. If you don't specify -d directive, the compiled code will also be put in the same directory as your source code.

7. To run:
java -classpath class com.anicehumble.TryHib

Note that we specify classpath, if we don't specify -classpath class, we have to do two steps:
cd class
java com.anicehumble.TryHib

Please do note that we need to use dots instead of forward-slashes(or back-slash when using Windows) when running Java programs.

8. Let's create a model

Create a directory for model
$ mkdir source/com/anicehumble/model

Create a Person.java class under your model directory
$ vi source/com/anicehumble/model/Person.java 

Type this in your text editor:

package com.anicehumble.model;

public class Person {

        private int personId;
        private String personName;
        private int age;


        public int getPersonId()  {
                return personId;
        }
        public void setPersonId(int value) {
                personId = value;
        }

        public String getPersonName() {
                return personName;
        }
        public void setPersonName(String value) {
                personName = value;
        }

        public int getAge() {
                return age;
        }
        public void setAge(int value) {
                age = value;
        }

}

Compile
$ javac -d class source/com/anicehumble/model/Person.java 

To check the compiled code
ls class/com/anicehumble/model/Person.class

9. Access the Person class from TryHib.java

package com.anicehumble;

import com.anicehumble.model.*;

public class TryHib {
        public static void main(String[] args) {
                // com.anicehumble.model.Person p = new com.anicehumble.model.Person();
                Person p = new Person();
                p.setPersonName("Michael");
                System.out.println("Hello " + p.getPersonName());
        }
}

Compile:
$ javac -d class -classpath class source/com/anicehumble/TryHib.java

Then run:
$ java -classpath class com.anicehumble.TryHib

Output:
Hello Michael

10. Map the Person class to Hibernate, download this first:

Download the following files and put them on your lib directory
commons-collections-3.1.jar
dom4j-1.6.1.jar
hibernate-jpa-2.0-api-1.0.0.Final.jar
hibernate3.jar
javassist-3.12.0.GA.jar
jta-1.1.jar
postgresql-9.0-801.jdbc4.jar
slf4j-api-1.6.1.jar
slf4j-simple-1.6.1.jar

11. Create a hibernate configuration under class/com/anicehumble

$ vi class/com/anicehumble/hibernate.cfg.xml

Type this, or better yet paste it:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.url">jdbc:postgresql://127.0.0.1/testdb</property>
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.username">postgres</property>
        <property name="hibernate.connection.password">opensesame</property>
        <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.use_outer_join">true</property>

        <mapping resource="com/anicehumble/model/person.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

12. Map the class to table, use this xml mapping:

Create person.hbm.xml mapping with your favorite editor:
$ vi class/com/anicehumble/model/person.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

        <class name="com.anicehumble.model.Person" table="person">
                <id name="PersonId" column="personid">
                <generator class="sequence">
                        <param name="sequence">person_personid_seq</param>
                </generator>
                </id>

                <property name="PersonName" column="personname"/>
                <property name="Age" column="age"/>
        </class>

</hibernate-mapping>


13. Use the Person class and map it to database:

Launch your favorite editor
$ vi source/com/anicehumble/TryHib.java 

Type this:
package com.anicehumble;

import com.anicehumble.model.*;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;



public class TryHib {
        public static void main(String[] args) {
                System.out.println("Hello");

                SessionFactory sf = new Configuration().configure("/com/anicehumble/hibernate.cfg.xml").buildSessionFactory();


                Session sess = sf.openSession();
                Person p = (Person) sess.get(Person.class, 1);

                int result = p.getAge();

                System.out.println(Integer.toString(result));

                System.out.println("Test");
        }
}

Compile(substitute colon with semicolon when you are on Windows):
$ javac -classpath class:lib/hibernate3.jar -d class source/com/anicehumble/TryHib.java 

14. Run(substitute colon with semicolon when you are on Windows):
$ java -classpath class:lib/commons-collections-3.1.jar:lib/dom4j-1.6.1.jar:lib/hibernate-jpa-2.0-api-1.0.0.Final.jar:lib/hibernate3.jar:lib/javassist-3.12.0.GA.jar:lib/jta-1.1.jar:lib/postgresql-9.0-801.jdbc4.jar:lib/slf4j-api-1.6.1.jar:lib/slf4j-simple-1.6.1.jar com.anicehumble.TryHib

Output:
Hello
35
Test

15. I forgot, before you do step 14, here's the Postgres DDL for your model/object:

CREATE TABLE person
(
  personid serial NOT NULL primary key,
  personname varchar(1000) NOT NULL unique,
  age integer NOT NULL,
);
insert into person(personname, age) values('michael', 35);

No comments:

Post a Comment