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

Friday, October 7, 2011

A Quick Reference to the Spring 3 DI Bean Configuration

Spring 3 DI (dependency injection) functionality which placed in "Spring Core" is very important for managing objects (or "beans") in enterprise applications.

Here is a quick reference for bean configuration of Spring DI:
Spring Bean Configurations 

You can get a bean in application by

ApplicationContext context = new ClassPathApplicationContext(new String[] {“application-context.xml”})
ViewManager viewManager = (ViewManager)context.getBean(“viewManager”);

ViewManager viewManager;