2. 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.
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.
4.
5.
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
7.
8. 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.
12. Hibernate Architecture
• Basic interfaces used while working with Hibernate are:
- SessionFactory
- Session
- Transaction
13. 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
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 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.
18. 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
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
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.