1 of 14

Topic : Hibernate

By : Noman Islam

2 of 14

Outline

  • Background
  • Introduction to Hibernate
  • Sample Application
  • Conclusion
  • Question & Answers

3 of 14

What is a framework?

  • A set of classes that are closely related in terms of function and data, and which form an independent and reusable product [google.com]
  • A cohesive set of classes that collaborate to provide services for the core unvarying part of a logical subsystem [1]

4 of 14

Persistence Framework

  • That provides services for persistent objects
  • Provides these functions:
    • Store and retrieve objects
    • Commit and rollback transactions
    • Extendable to support any kind of storage mechanisms
    • Easy to use
    • Transparent

5 of 14

What is hibernate ?

  • a powerful, open source, ultra-high performance, scalable, object/relational persistence and query service (HQL) for Java
  • lets you develop persistent classes following common Java idiom - including association, inheritance, polymorphism, composition and the Java collections framework

6 of 14

Four Steps

  • Defining the Persistence class
  • XML mapping document
  • Hibernate Configuration file
  • The actual code to persist the objects

7 of 14

Defining the Persistent class (User.java)

public class User {

private Long id;

private String userName;

private String userPassword;

private String userEmail;

public User() { }

public Long getId() { return this.id; }

public void setId(Long id) { this.id = id; }

public String getUserName() { return this.userName; }

public void setUserName(String userName) { this.userName = userName; }

public String getUserPassword() { return this.userPassword; }

public void setUserPassword(String userPassword) { this.userPassword = userPassword; }

public String getUserEmail() { return this.userEmail; }

public void setUserEmail(String userEmail) { this.userEmail = userEmail; }

}

8 of 14

XML mapping document (User.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="User" table="user">

<id column="USER_ID" name="id" type="java.lang.Long">

<generator class="increment"/>

</id>

<property column="USER_NAME"

length="20"

name="userName" not-null="true"

type="java.lang.String"/>

<property column="USER_PASSWORD"

length="10"

name="userPassword" not-null="true"

type="java.lang.String"/>

<property column="USER_EMAIL"

length="20"

name="userEmail" type="java.lang.String"/>

</class>

</hibernate-mapping>

9 of 14

Configuration file (hibernate.cfg.xml)

<?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>

<!-- Database connection settings -->

<property name="hibernate.dialect">

org.hibernate.dialect.MySQLDialect </property>

<property name="hibernate.connection.driver_class">

com.mysql.jdbc.Driver </property>

<property name="hibernate.connection.url">

jdbc:mysql://localhost:3306/quickstart

</property>

<property name="hibernate.connection.username">

root

</property>

<property name="hibernate.connection.password">

</property>

<!-- Mapping files -->

<mapping resource="User.hbm.xml"/>

</session-factory>

</hibernate-configuration>

10 of 14

Inserting new records (UserInsertHibernate.java)

import org.hibernate.*;

import org.hibernate.cfg.*;

public class UserInsertHibernate {

public static void main(String[] args)

throws Exception {

// Fire up Hibernate

SessionFactory sessionFactory = new Configuration()

.configure().buildSessionFactory();

// Open Session

Session session = sessionFactory.openSession();

// Build a User

User user = new User();

Transaction tx = session.beginTransaction();

user.setUserEmail("user@yahoo.com");

user.setUserName("userName-1");

user.setUserPassword("Password-1");

// Save User and close Session

session.saveOrUpdate(user);

session.close();

}

}

11 of 14

Hibernate Query Language (HQL)

  • makes SQL be object oriented
  • it uses classes and properties instead of tables and columns
  • supports polymorphism, associations
  • Example
    • String query =

"select product from product "

+ "in class test.hibernate.Product "

+ "where product.name=:name";

// search for what?

String name = "noman";

// search and return

List list = session.find(query, name,

Hibernate.STRING);

12 of 14

Conclusion

  • Open source
  • Simpler
  • Efficient
  • JBoss EJB 3.0 is built on top of the Hibernate ORM solution [JBoss EJB 3.0 Reference Documentation]

13 of 14

References

[1] Applying UML and Pattern by Craig Larmen

14 of 14

Questions (if any)