Vous êtes sur la page 1sur 20

Introduction To Hibernate

Introduction To Hibernate
Why was Hibernate created?
Hibernate's Main Feature
Hibernate's Architecture
Object Relation Mapping in Hibernate
Code Implementation
Why was Hibernate created?
Combining object orientation of data and relational model of storing data.
Solves object-relational impedance mismatch problems
Database portability

Hibernate was started in 2001 by Gavin King


Early in 2003, Hibernate2 releases which offered many significant improvements.
JBoss, Inc. (now part of Red Hat) later hired the lead Hibernate developers
The current version of Hibernate is 3.6.4 (May 5, 2011)
Hibernate's Main Feature
•Mapping from Java classes to database tables
•Mapping from Java data types to SQL data types,
•Provides data query ( and retrieval facilities )
•Database portability
•Helps create high-performance database applications with Java much faster and
easier.
•You can use Hibernate as in standalone Java applications or as in Java EE
applications using servlets or EJB session beans.
Getting Started
Hibernate's Architecture
Basic classes:
- SessionFactory,
- Session,
An object of this class represents a unit of work with a database
Session is opened by SessionFactory.newSession()
Closed by session.close()
- Transaction
equals one database transaction.
Transaction tx = session.beginTransaction( );
tx.commit(); tx.rollback();
Object Relation Mapping in
Hibernate
Configuring Hibernate

Hibernate uses the hibernate.cfg.xml to create the


connection pool and setup required environment.
hibernate.properties
Persistence Class

Hibernate uses the Plain Old Java Objects


(POJOs) classes to map to the database table. We
can configure the variables to map to the database
column.
package dev;
//  Java Class to map to the datbase Contact Table
public class Contact {
  private String firstName;
  private String lastName;
  private String email;
  private long id;
 public String getEmail() {
    return email;  } 
public String getFirstName() {
 return firstName; }
public String getLastName() {
    return lastName; }
}
public void setEmail(String string) {email = string;}
public void setFirstName(String string) {
  firstName = string;  }
  public void setLastName(String string) {
    lastName = string; }
  public long getId() {
    return id;  }
  public void setId(long l) {
   id = l;  }
}
Mapping Files

The file *.hbm.xml is used to map Contact


Object to the Contact table in the database.
<?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=“dev.Contact" table="CONTACT">
   <id name="id" type="long" column="ID" >
   <generator class="assigned"/> </id>
<property name="firstName">
   <column name="FIRSTNAME" />
  </property>
  <property name="lastName">
    <column name="LASTNAME"/>
  </property>
  <property name="email">
    <column name="EMAIL"/>
  </property>
 </class></hibernate-mapping>

Vous aimerez peut-être aussi