CONTENTS
1. Overview
2. Advantages over JDBC
3. Hibernate Architecture
4. Downloading and Installing Hibernate
Overview
• A popular open source Object Relational Mapping(ORM) tool which
provides transparent persistence for POJOS.
• ORM is used to map objects to relational databases(Oracle,Mysql).
• Allows to map java objects to relational databases and also provides data
query
• Generates the SQL calls and makes application portable to all SQL
databases, with database portability
• Allows to develop persistent classes with all java terms – like association,
inheritance, polymorphism etc
• Can be used as in standalone Java applications or as in Java EE
applications using servlets or EJB session beans.
Advantages of Hibernate
• Retains natural object model (transparent)
• Minimizes Code
• Does not require a container
• Model is not tied to persistence implementation.
• Metadata controlled persistence
• Transparent - working with the model, not the data access technology
• Pooling, Caching, Transactions can be handled outside of our code
• Object/relational mappings are defined in an XML document. The
mapping document is designed to be readable and hand-editable.
• Mapping language is Java-centric, meaning that mappings are
constructed around persistent class declarations, not table declarations.
• Choose to define XML mappings by hand, or by using tools like XDoclet,
Middlegen and AndroMDA.
• The “Lite" architecture has the application provide its own
JDBC connections and manage its own transactions
• Has
– SessionFactory
– Session
– Persistent Objects
– Transient Objects
• The "full cream" architecture abstracts the application away
from the underlying JDBC and Java Transaction API (JTA) and
lets Hibernate take care of the details
Configuration
Configuration class's operation has two key components:
• The database connection
– handled through one or more configuration files supported by
Hibernate
– hibernate.properties and hibernate.cfg.xml
• The classmapping setup
– makes the connection between the Java classes and database tables.
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-
2.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver">
com.mysql.jdbc.Driver </property>
<property name="connection.url“>
jdbc:mysql://localhost/products </property>
<property name="dialect">
net.sf.hibernate.dialect.MySQLDialect </property>
</session-factory>
</hibernate-configuration>
hibernate.properties
hibernate.dialect net.sf.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class com.mysql.jdbc.Driver
hibernate.connection.url jdbc:mysql://localhost/products
hibernate.connection.username root
hibernate.connection.password root
hibernate.show_sql true
hibernate.cglib.use_reflection_optimizer false
Hibernate mapping file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="Notes“ table="notes">
<id name="id" unsaved-value="0">
<generator class="native"/></id>
<property name="info" type="string"/>
<property name="count" type="integer" not-null="true"/>
<property name="zipcode" type="string"/>
<property name="fullname" type="string"/>
</class></hibernate-mapping>
Hibernate Architecture
• Basic interfaces used while working with Hibernate are:
- SessionFactory
- Session
- Transaction
SessionFactory
• A singleton class called SessionFactory is instantiated.
• A threadsafe, immutable cache of compiled mappings for a single
database.
• The SessionFactory object is heavyweight; better to create only one
SessionFactory per application.
• A factory for creating session objects
• Can hold an optional(second-level) cache of data that is reusable between
transactions at a process
Session
• A single-threaded, short-lived object representing a conversation between
the application and the persistent store.
• is lightweight and instantiated each time an interaction is needed with
the database.
• As the Session object is created, the objects are persisted or loaded from
the database
• Then the session object is flushed and closed.
• It wraps a JDBC connection and is a factory for Transaction.
• Opened by SessionFactory.openSession()
• Closed by session.close()
ThreadLocal Session Pattern
• Use ThreadLocal variable to maintain session data
ThreadLocal
• Specialized Java class which is associated to a user thread of execution and
serves as a data container throughout the lifespan of the thread (or child
threads)
• Global variable which is local for every thread
• To create a thread
ThreadLocal sess = new ThreadLocal();
Session session = sess.get(); // to get the current session
Transaction
• A single-threaded, short-lived object used by the application
to specify atomic units of work.
• It abstracts the application from the underlying JDBC, JTA or
CORBA transaction.
• A Session might span several Transactions in some cases. is
connected with a concrete session
Transaction tx = session.beginTransaction( );
Persistent objects and collections(Entities)
• Short-lived, single threaded objects containing persistent
state and business function.
• Can be ordinary JavaBeans/POJOs.
• associated with exactly one Session.
Transient and detached objects and collections
• Instances of persistent classes that are not currently
associated with a Session.
• Might have been instantiated by the application and not yet
persisted
• Might have been instantiated by a closed Session.
Hibernate Callbacks
• public Boolean onSave(Session s); - called before the object is saved to the
• database
• public Boolean onUpdate(Session s); - called before the object is updated
• public boolean onDelete(Session s); - called before the object is deleted
• public Boolean onLoad(Session s); - called after the object is loaded from
• the database
Downloading and Installing
Downloading
• Download Hibernate from the site (ie) from JBoss tools as Zip
• Extract the files to your preferred location
• Has 2 folders – plugins and features
Adding to Eclipse
• Add the files within hibernate /features folder to
eclipse/features folder
• Add the files within hibernate /plugins folder to
eclipse/plugins folder
Alternate Way
• Open eclipse Help  Update site add hibernate from site

Hibernate

  • 1.
    CONTENTS 1. Overview 2. Advantagesover JDBC 3. Hibernate Architecture 4. Downloading and Installing Hibernate
  • 2.
    Overview • A popularopen source Object Relational Mapping(ORM) tool which provides transparent persistence for POJOS. • ORM is used to map objects to relational databases(Oracle,Mysql). • Allows to map java objects to relational databases and also provides data query • Generates the SQL calls and makes application portable to all SQL databases, with database portability • Allows to develop persistent classes with all java terms – like association, inheritance, polymorphism etc • Can be used as in standalone Java applications or as in Java EE applications using servlets or EJB session beans.
  • 3.
    Advantages of Hibernate •Retains natural object model (transparent) • Minimizes Code • Does not require a container • Model is not tied to persistence implementation. • Metadata controlled persistence • Transparent - working with the model, not the data access technology • Pooling, Caching, Transactions can be handled outside of our code • Object/relational mappings are defined in an XML document. The mapping document is designed to be readable and hand-editable. • Mapping language is Java-centric, meaning that mappings are constructed around persistent class declarations, not table declarations. • Choose to define XML mappings by hand, or by using tools like XDoclet, Middlegen and AndroMDA.
  • 6.
    • The “Lite"architecture has the application provide its own JDBC connections and manage its own transactions • Has – SessionFactory – Session – Persistent Objects – Transient Objects • The "full cream" architecture abstracts the application away from the underlying JDBC and Java Transaction API (JTA) and lets Hibernate take care of the details
  • 8.
    Configuration Configuration class's operationhas two key components: • The database connection – handled through one or more configuration files supported by Hibernate – hibernate.properties and hibernate.cfg.xml • The classmapping setup – makes the connection between the Java classes and database tables.
  • 9.
    hibernate.cfg.xml <?xml version="1.0" encoding="utf-8"?> <!DOCTYPEhibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration- 2.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.driver"> com.mysql.jdbc.Driver </property> <property name="connection.url“> jdbc:mysql://localhost/products </property> <property name="dialect"> net.sf.hibernate.dialect.MySQLDialect </property> </session-factory> </hibernate-configuration>
  • 10.
    hibernate.properties hibernate.dialect net.sf.hibernate.dialect.MySQLDialect hibernate.connection.driver_class com.mysql.jdbc.Driver hibernate.connection.urljdbc:mysql://localhost/products hibernate.connection.username root hibernate.connection.password root hibernate.show_sql true hibernate.cglib.use_reflection_optimizer false
  • 11.
    Hibernate mapping file <?xmlversion="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping> <class name="Notes“ table="notes"> <id name="id" unsaved-value="0"> <generator class="native"/></id> <property name="info" type="string"/> <property name="count" type="integer" not-null="true"/> <property name="zipcode" type="string"/> <property name="fullname" type="string"/> </class></hibernate-mapping>
  • 12.
    Hibernate Architecture • Basicinterfaces used while working with Hibernate are: - SessionFactory - Session - Transaction
  • 13.
    SessionFactory • A singletonclass called SessionFactory is instantiated. • A threadsafe, immutable cache of compiled mappings for a single database. • The SessionFactory object is heavyweight; better to create only one SessionFactory per application. • A factory for creating session objects • Can hold an optional(second-level) cache of data that is reusable between transactions at a process
  • 14.
    Session • A single-threaded,short-lived object representing a conversation between the application and the persistent store. • is lightweight and instantiated each time an interaction is needed with the database. • As the Session object is created, the objects are persisted or loaded from the database • Then the session object is flushed and closed. • It wraps a JDBC connection and is a factory for Transaction. • Opened by SessionFactory.openSession() • Closed by session.close()
  • 15.
    ThreadLocal Session Pattern •Use ThreadLocal variable to maintain session data ThreadLocal • Specialized Java class which is associated to a user thread of execution and serves as a data container throughout the lifespan of the thread (or child threads) • Global variable which is local for every thread • To create a thread ThreadLocal sess = new ThreadLocal(); Session session = sess.get(); // to get the current session
  • 16.
    Transaction • A single-threaded,short-lived object used by the application to specify atomic units of work. • It abstracts the application from the underlying JDBC, JTA or CORBA transaction. • A Session might span several Transactions in some cases. is connected with a concrete session Transaction tx = session.beginTransaction( );
  • 17.
    Persistent objects andcollections(Entities) • Short-lived, single threaded objects containing persistent state and business function. • Can be ordinary JavaBeans/POJOs. • associated with exactly one Session. Transient and detached objects and collections • Instances of persistent classes that are not currently associated with a Session. • Might have been instantiated by the application and not yet persisted • Might have been instantiated by a closed Session.
  • 18.
    Hibernate Callbacks • publicBoolean onSave(Session s); - called before the object is saved to the • database • public Boolean onUpdate(Session s); - called before the object is updated • public boolean onDelete(Session s); - called before the object is deleted • public Boolean onLoad(Session s); - called after the object is loaded from • the database
  • 19.
    Downloading and Installing Downloading •Download Hibernate from the site (ie) from JBoss tools as Zip • Extract the files to your preferred location • Has 2 folders – plugins and features Adding to Eclipse • Add the files within hibernate /features folder to eclipse/features folder • Add the files within hibernate /plugins folder to eclipse/plugins folder Alternate Way • Open eclipse Help  Update site add hibernate from site

Editor's Notes

  • #4 Dirty checking Hibernate allows dirty checking feature.All persistent objects are monitored by hibernate.it detects which objects have been modified and then calls update statements on all updated objects.the process of updating the changed object is called automatic dirty checking. Dirty checking allows the user or developer to avoid the time consuming databases write actions. This feature makes necessary updations and changes to the fields which require a change, remaining fields are left unchanged or untouched.