Pages

Monday, October 10, 2011

Hibernate 3 Installation & Configuration





Short Description:
Hibernate is an ORM framework for Java applications, which maps application objects into relational database tables. It is important because:
  • Developers don't write SQL expressions into code. This saves time and increases maintainability.
  • It is database independent. DBMS can be changed without changing the code. Only configuration XML is modified for this.
  • It supports transaction methods and auto-creation of tables, constraints, relations (1-N,1-1, N-N, etc.).
Installation:
Requires two steps:

  1. Copying required JARs into /lib directory:
  2. Creating and configuring persistence.xml configuration file.
Required JARs are: 
Hibernate installation JARs
Hibernate requires only hibernate3.jar. But hibernate-jpa jar is also used generally for additional JPA properties. A database connector jar is also reqiured for connection handling. mysql-connector jar is used here for this reason. Other jars are required because of dependencies. These jars can be found on most Java developer download site with exact version numberings.

persistence.xml file is also required to map application into database. Database configuration is done in this file. It is put into /src/main/config/META-INF folder as default. Content should be as below:


Hibernate persistence.xml configuration

  • persistence-unit name: Defines the configuration id. More than one persistence unit can be defined at once.
  • hibernate.connection.driver_class & hibernate.dialect: Defines the database type. MySQL is used in this example.
  • hibernate.connection.username & hibernate.connection.password: Defines database connection parameters.
  • hibernate.connection.url: Defines database server name, port and schema. "test" is used here for schema name.
These two steps are required and enough for starting Hibernate usage. Hibernate entity definition and API usage will be explained in upcoming posts.

3 comments:

  1. Who still puts their external dependency jars in a WEB-INF/lib folder?
    Have a look at Maven or Gradle! :)

    ReplyDelete
  2. Maven is a very useful dependency management tool of course. But this installation is very basic, fast and especially for beginners. Those tools can not be used in this case.

    ReplyDelete