IntroductiontoHibernate V1
IntroductiontoHibernate V1
Overview
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
Why Use Hibernate?
• 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
Introduction to Hiberate
The Basics
Simple Object Model
• AuctionItem • Bid
– description – amount
– type
– datetime
– successfulBid
pairs }
public void
setDescription(String desc) {
• Collection _description = desc;
}
property is an } …
interface
XML Mapping File
• Readable metadata <class name=“AuctionItem”
table=“AUCTION_ITEM”>
• Column / table <id name=“id”
column=“ITEM_ID”>
mappings <generator
class=“native”/>
• Surrogate key </id>
generation strategy <property name=“description”
column=“DESCR”/>
• Collection metadata <many-to-one name=“successfulBid”
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();
tx.commit();
session.close();
Introduction to Hibernate
The Details
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
Configuration
hibernate.properties
hibernate.dialect =
org.hibernate.dialect.SQLServerDialect
hibernate.connection.driver_class = net.sf.jtds.Driver
hibernate.connection.url =
jdbc:sqlserver://localhost/db:1433
hibernate.connection.username = myuser
hibernate.connection.password =
mypass
• Also configurable via using XML
• Several ways to add mapping files to
configuration, including XML or API-based
SessionFactory
• Once the Configuration is prepared,
obtaining the SessionFactory is
easy: