Introduction to Hibernate. Overview



Similar documents
Hibernate Reference Documentation. Version: 3.2 cr1

Exam Name: IBM InfoSphere MDM Server v9.0

Database Programming with PL/SQL: Learning Objectives

Geodatabase Programming with SQL

Java EE 7: Back-End Server Application Development

1 File Processing Systems

Core Java+ J2EE+Struts+Hibernate+Spring

Java and RDBMS. Married with issues. Database constraints

SQL - QUICK GUIDE. Allows users to access data in relational database management systems.

Big Data Analytics in LinkedIn. Danielle Aring & William Merritt

Performance Evaluation of Java Object Relational Mapping Tools

OpenReports: Users Guide

Java SE 8 Programming

Aspects of using Hibernate with CaptainCasa Enterprise Client

IBM InfoSphere MDM Server v9.0. Version: Demo. Page <<1/11>>

The full setup includes the server itself, the server control panel, Firebird Database Server, and three sample applications with source code.

Release Bulletin EDI Products 5.2.1

What is a database? COSC 304 Introduction to Database Systems. Database Introduction. Example Problem. Databases in the Real-World

CERTIFIED MULESOFT DEVELOPER EXAM. Preparation Guide

Introduction to Triggers using SQL

Performance Tuning for the JDBC TM API

SpagoBI exo Tomcat Installation Manual

A Brief Introduction to MySQL

SQL Databases Course. by Applied Technology Research Center. This course provides training for MySQL, Oracle, SQL Server and PostgreSQL databases.

Spring Data JDBC Extensions Reference Documentation

CSE 530A Database Management Systems. Introduction. Washington University Fall 2013

Oracle Database: SQL and PL/SQL Fundamentals NEW

Getting Started with Telerik Data Access. Contents

Chapter 9 Java and SQL. Wang Yang wyang@njnet.edu.cn

Developing Physical Solutions for InfoSphere Master Data Management Server Advanced Edition v11. MDM Workbench Development Tutorial

Enterprise Application Development In Java with AJAX and ORM

SAP Business Objects Business Intelligence platform Document Version: 4.1 Support Package Data Federation Administration Tool Guide

Oracle Business Intelligence Server Administration Guide. Version December 2006

J j enterpririse. Oracle Application Express 3. Develop Native Oracle database-centric web applications quickly and easily with Oracle APEX

SQL Server for developers. murach's TRAINING & REFERENCE. Bryan Syverson. Mike Murach & Associates, Inc. Joel Murach

Oracle Database: Program with PL/SQL

Oracle Database: SQL and PL/SQL Fundamentals

Using SQL Developer. Copyright 2008, Oracle. All rights reserved.

Database 10g Edition: All possible 10g features, either bundled or available at additional cost.

Oracle Database: Program with PL/SQL

Oracle Database: Program with PL/SQL

Hive Interview Questions

Migrating Non-Oracle Databases and their Applications to Oracle Database 12c O R A C L E W H I T E P A P E R D E C E M B E R

Oracle Database: Program with PL/SQL

Oracle Database: SQL and PL/SQL Fundamentals NEW

No no-argument constructor. No default constructor found

White Paper: 1) Architecture Objectives: The primary objective of this architecture is to meet the. 2) Architecture Explanation

Introduction to Microsoft Jet SQL

Monitoring HP OO 10. Overview. Available Tools. HP OO Community Guides

database abstraction layer database abstraction layers in PHP Lukas Smith BackendMedia

Hibernate Reference Documentation

Object Relational Mapping for Database Integration

GlassFish v3. Building an ex tensible modular Java EE application server. Jerome Dochez and Ludovic Champenois Sun Microsystems, Inc.

Retrieving Data Using the SQL SELECT Statement. Copyright 2006, Oracle. All rights reserved.

Database System Architecture & System Catalog Instructor: Mourad Benchikh Text Books: Elmasri & Navathe Chap. 17 Silberschatz & Korth Chap.

Oracle Database: Program with PL/SQL

Release Notes Skelta BPM.NET 2007 (Service Pack 2)

Oracle Database 12c: Introduction to SQL Ed 1.1

This tutorial will teach you how to use Hibernate to develop your database based web applications in simple and easy steps.

ActiveVOS Server Architecture. March 2009

Using Multiple Operations. Implementing Table Operations Using Structured Query Language (SQL)

Duration Vendor Audience 5 Days Oracle Developers, Technical Consultants, Database Administrators and System Analysts

OWB Users, Enter The New ODI World

Hibernate Language Binding Guide For The Connection Cloud Using Java Persistence API (JAP)

Toad for Oracle 8.6 SQL Tuning

ODMG-api Guide. Table of contents

Administering batch environments

HOW TO DEPLOY AN EJB APLICATION IN WEBLOGIC SERVER 11GR1

Oracle USF

Oracle Database 10g: Program with PL/SQL

Customer Bank Account Management System Technical Specification Document

Oracle Database: SQL and PL/SQL Fundamentals

3.GETTING STARTED WITH ORACLE8i

Java 7 Recipes. Freddy Guime. vk» (,\['«** g!p#« Carl Dea. Josh Juneau. John O'Conner

MySQL for Beginners Ed 3

Virtual Private Database Features in Oracle 10g.

Object Oriented Databases. OOAD Fall 2012 Arjun Gopalakrishna Bhavya Udayashankar

The first time through running an Ad Hoc query or Stored Procedure, SQL Server will go through each of the following steps.

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

TIBCO BusinessEvents Business Process Orchestration Release Notes

Using Object Database db4o as Storage Provider in Voldemort

PASS4TEST 専 門 IT 認 証 試 験 問 題 集 提 供 者

CB Linked Server for Enterprise Applications

Data Integration and ETL with Oracle Warehouse Builder: Part 1

Building Views and Charts in Requests Introduction to Answers views and charts Creating and editing charts Performing common view tasks

Nicholas S. Williams. wrox. A Wiley Brand

Oracle Database: Introduction to SQL

Copyright 2013 Consona Corporation. All rights reserved

Oracle Application Server 10g Web Services Frequently Asked Questions Oct, 2006

Table of contents. Reverse-engineers a database to Grails domain classes.

Migrating a real-time transaction evaluation and scoring solution to HP NonStop platform

Oracle Database: Introduction to SQL

OBJECTS AND DATABASES. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 21

Advanced Java Client API

PDA DRIVEN WAREHOUSE INVENTORY MANAGEMENT SYSTEM Sebastian Albert Master of Science in Technology

Transcription:

Introduction to Hibernate Overview

About the Presenter Cliff Meyers cliff.meyers@gmail.com Application Developer US Department of State, Bureau of Information Resource Management ColdFusion, Java, Oracle, MS SQL

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 description type successfulbid Bid amount datetime AuctionItem has zero or more bids Auction item has zero or one successfulbid

Plain Old Java Object (POJO) Default constructor Identifier property Get/set pairs Collection property is an interface type public class AuctionItem { private Long _id; private Set _bids; private Bid _successfulbid private String _description; } public Long getid() { return _id; } private void setid(long id) { _id = id; } public String getdescription() { return _description; } public void setdescription(string desc) { _description = desc; }

XML Mapping File Readable metadata Column / table mappings Surrogate key generation strategy Collection metadata Fetching strategies <class name= AuctionItem table= AUCTION_ITEM > <id name= id column= ITEM_ID > </id> <generator class= native /> <property name= description column= DESCR /> <many-to-one name= successfulbid column= SUCCESSFUL_BID_ID /> <set name= bids </set> </class> cascade= all lazy= true > <key column= ITEM_ID /> <one-to-many class= Bid />

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();

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: Configuration cfg = new Configuration(); // do some configuration cfg.configure(); SessionFactory sf = cfg.buildsessionfactory();

Typical Usage Pattern Session s = sessionfactory.opensession(); Transaction tx = s.begintransaction(); // perform some operation tx.commit(); s.close(); Although some additional boilerplate code is required for proper error handling***

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

Example Queries Criteria API List auctionitems = session.createcriteria(auctionitem.class).setfetchmode( bids, FetchMode.EAGER).add( Expression.like( description, description) ).createcriteria( successfulbid ).list(); Equivalent HQL: from AuctionItem item.add( Expression.gt( amount, minamount) ) left join fetch item.bids where item.description like :description and item.successfulbid.amount > :minamount

Example Queries Query by example approach AuctionItem item = new AuctionItem(); item.setdescription( hib ); Bid bid = new Bid(); bid.setamount(1.0); List auctionitems = session.createcriteria(auctionitem.class).add( Example.create(item).enableLike(MatchMode.START) ).createcriteria( bids ).add( Example.create(bid) ).list(); Equivalent HQL: from AuctionItem item join item.bids bid where item.description like hib% and bid.amount > 1.0

Introduction to Hibernate More than Hibernate

Simplifying Hibernate Boilerplate public void deleteitem(long itemid) throws HibernateException { Session session = null; try { session = sessionfactory.opensession(); tx = session.begintransaction(); AuctionItem item = (AuctionItem) session.get(actionitem.class, itemid); session.delete(item); tx.commit(); catch(hibernateexception ex) { tx.rollback(); // do something useful, like log and rethrow exception } finally { session.close(); } }

Spring to the Rescue Spring offers a convenient HibernateSupportDao class that offers free convenience methods and exception handling public void deleteitem(long itemid) } throws DataAccessException { AuctionItem item = (AuctionItem) gethibernatetemplate().get(actionitem.class, itemid); session.delete(item);

Hibernate with ColdFusion Write Java classes, mapping files and Hibernate configuration Bundle into a.jar file Deploy to ColdFusion server along with Hibernate s required.jar files

Hibernate with ColdFusion Use the multiserver install for CF Create a separate instance for the hybrid application Deploy files to this location: {jrun.home}/servers/myserver/cfusion.ear/ cfusion.war/web-inf/cfusion/lib

Hibernate with ColdFusion Hibernate s required.jar files: hibernate3.jar antlr.jar asm.jar asm-attrs.jar cglib.jar dom4j.jar ehcache.jar jta.jar commons-collections.jar commons-logging.jar log4j.jar

Hibernate with ColdFusion Database connectivity: Hibernate can use JNDI datasources (similar to CF datasources) instead of connection information in hibernate.properties JNDI can be configured through the JRun administration console (JMC) Only available in CF multiserver install

Hibernate with ColdFusion Java is a strong-typed language whereas ColdFusion is weakly-typed Use of ColdFusion s javacast() function is required to distinguish between methods like getauctionitem(long itemid) and getauctionitem(string description) Quantity of casting can get tedious ColdFusion s logging infrastructure (Log4J) suppresses all of Hibernate s logging info, making it difficult to debug deployed code

Any Questions?

References Hibernate docs http://hibernate.org Hibernate in Action http://manning.com/bauer/ Spring docs http://springframework.org

Thank You!