Hibernate

What is Hibernate?

• Hibernate is an object-relational mapping tool (ORM) that allows for persisting Java objects in a relational database
• Driven by XML configuration files to configure data connectivity and map classes to database tables
• Not a Java/SQL code generation tool
• Developer writes code to call API
• API executes necessary SQL at runtime

Why Use Hibernate?

• Eliminate need for repetitive SQL
• Work with classes and objects instead of queries and result sets
• More OO, less procedural
• Mapping approach can resist changes in object/data model more easily
• Strong support for caching
• Handles all create-read-update-delete (CRUD) operations using simple API; no SQL
• Generates DDL scripts to create DB schema (tables, constraints, sequences)
• Flexibility to hand-tune SQL and call stored procedures to optimize performance
• Supports over 20 RDBMS; change the database by tweaking configuration files

Plain Old Java Object (POJO)

Default constructor
• Identifier property
• Get/set pairs
• Collection

property is an interface type

Creating Objects

Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
AuctionItem item = new AuctionItem();
item.setDescription(”Batman Begins”);
item.setType(”DVD”);
session.save(item);
tx.commit();
session.close();

Updating Objects

Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
AuctionItem item =(AuctionItem) session.get(ActionItem.class, itemId);
item.setDescription(newDescription);
tx.commit();
session.close();

Deleting Objects

Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
AuctionItem item =(AuctionItem) session.get(ActionItem.class, itemId);
session.delete(item);
tx.commit();
session.close();

Selecting Objects

• Hibernate Query Language (HQL), similar to SQL

Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

List allAuctions = session.createQuery(”select item from AuctionItem item
join item.bids bid where item.description like ‘Batman%’
and bid.amount < 15 “).list();

tx.commit();
session.close();

Key Hibernate Classes

Configuration – uses mapping and database connection metadata to create SessionFactory

SessionFactory –thread-safe cache of compiled mappings for database; created

once at application startup (expensive)

•– Session – represents a “conversation” between application and database; holds 1st

level cache of objects

• Transaction – an atomic unit of work SessionFactory

• Once the Configuration is prepared, obtaining the SessionFactory is

easy:

Configuration cfg = new Configuration();

// … do some configuration …

cfg.configure();

SessionFactory sf = cfg.buildSessionFactory();
Hibernate Querying Options


• HQL

– Syntax similar to SQL
– Unlike SQL, HQL is still database-agnostic

• Criteria

– Java-based API for building queries
– Good for queries that are built up using lots of conditional logic; avoids messy string manipulation

• SQL / PLSQL

– Often needed to optimize for performance or
leverage vendor-specific features