Monday, May 20, 2013

Hibernate - First Application

Download Hibernate 4 from hibernate4. Unzip to local file system, for example, D:\hibernate-release-4.1.12.Final.

Create a Java Project in Eclipse, add User library Hibernate with jars under D:\hibernate-release-4.1.12.Final\lib\required.

Since Hibernate is built on top of JDBC, we still need related JDBC driver. So add the jar to the project as well.

Take a look at the project structure.



















To use Hibernate, at least we need -
  • hibernate.cfg.xml
  • Object class with Annotation
  • Session, main runtime interface between Java and Hibernate

Snip of hibernate.cfg.xml


 

  
  com.mysql.jdbc.Driver
  jdbc:mysql://localhost:3306/hibernatedb
  root
  root

  
  1

  
  org.hibernate.dialect.MySQLDialect

  
  org.hibernate.cache.internal.NoCacheProvider

  
  true

  
  create

  
  

 



We need to set JDBC connection parameters, and select one proper SQL dialect. I am using MySQLDialect, and you can find all dialects at hibernate-core/org.hibernate.dialect.

Mapping object class
package com.mqin.hibernate.demo;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Student {

    @Id
    private int ID;
    private String firstName;
    private String lastName;

    public int getID() {
        return ID;
    }

    public void setID(int iD) {
        ID = iD;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

}

Test Code
package com.mqin.hibernate.demo;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateTest {

    public static void main(String[] args) {
        Student student = new Student();
        student.setID(1);
        student.setFirstName("James");
        student.setLastName("Bond");

        Configuration configuration;
        ServiceRegistry serviceRegistry;
        SessionFactory sessionFactory;
        Session session;

        configuration = new Configuration();
        configuration.configure();

        serviceRegistry = new ServiceRegistryBuilder()
                              .applySettings(configuration.getProperties())
                              .buildServiceRegistry();
        
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        // sessionFactory = configuration.buildSessionFactory();
        // deprecated in Hibernate 4
        
        session = sessionFactory.openSession();
        session.beginTransaction();
        session.save(student);
        session.getTransaction().commit();

    }

}


Console ouput
Hibernate: drop table if exists Student
Hibernate: create table Student (ID integer not null, firstName varchar(255), lastName varchar(255), primary key (ID))
Hibernate: insert into Student (firstName, lastName, ID) values (?, ?, ?)

@Entity table created in database and @Id is primary key.






No comments:

Post a Comment