Introduction To Hibernate

Post on 22-Jan-2015

735 views 1 download

description

Introduction to Hibernate

Transcript of Introduction To Hibernate

11 March 2009 www.ashishkulkarni.com 1

Introduction to Hibernate

Ashish Kulkarni

www.ashishkulkarni.com 211 March 2009

How this presentation is organised

� A brief history of Relational Databases� The Object Oriented Paradigm� The Problem� Solutions� Demo - The Hibernate Way� Beyond Hibernate� Questions

� Welcome anytime as long as you can accept the following answers:

� I don’t know but I will find out

� Can we park that question and take it up after this presentation?

www.ashishkulkarni.com 311 March 2009

What this Presentation is about

� There is a lot of documentation, books and examples on Hibernate.

� This Presentation doesn’t try to duplicate these. Instead, it tries to show what Hibernate is capable of with the help of real-life examples.

www.ashishkulkarni.com 411 March 2009

A brief history of Relational Databases

� Relational Databases have been around for a long time

� Out of all other database models such as Hierarchical, Network, Object, etc., Relational Databases have come to rule the Database world

� They have come to support a vast array of object types and languages and have gained vast amount of industry support in terms of the sheer number of databases as well as tools

www.ashishkulkarni.com 511 March 2009

The Object Oriented Paradigm

� Java is an Object Oriented language (duh).

� It revels in Object Orientation. POJOs or Plain Old Java Objects form a clean way of storing and passing information within an application.

www.ashishkulkarni.com 611 March 2009

The Problem

� While Java supports a vast array of databases using JDBC, performing any database actions for storing, updating and retrieving data using JDBC can be very cumbersome

� While this can be interesting for extremely small projects, it can put severe constraints on a large scale project and make any change very costly

www.ashishkulkarni.com 711 March 2009

The Problem (continued)

� While a lot of things such as managing database connections can be automated by means of libraries, we still need to write a considerable amount of code for storing and retrieving data from individual tables, setting field level parameters at every stage

� In addition, we need to create and manage transactions

� Transferring Java data types back and forward to Database data types is another issue

www.ashishkulkarni.com 811 March 2009

The Problem (continued)

� Managing relations can be even more tricky with plain JDBC. Just imagine writing code to persist a Parent Java object that Contains an ArrayList or HashSet of Child Java objects within a single transaction. And then, to retrieve it all back into a Java Object Graph.

www.ashishkulkarni.com 911 March 2009

Solutions and Issues

� Some of the early solutions were:�CMP EJBs

� The issue with CMP EJBs was latency due to Lazy Loading, among others

�Custom/Homegrown ORM Frameworks� Maintenance of such homegrown frameworks can

be expensive

www.ashishkulkarni.com 1011 March 2009

Demo – The Hibernate Way

� This demo will show CRUD (Create, Retrieve, Update and Delete) operations using Hibernate in the following scenarios:�Single table with assigned ID

�Parent-Child relationship�Single table with auto-generated ID�Many-Many relationship

www.ashishkulkarni.com 1111 March 2009

What Hibernate Tools can do

� Hibernate Tools can generate Java Classes based on database structure. These Java Classes are referred to as Entities.

� Hibernate Tools can either generate Annotations within the Entities or a separate hbm.xml files for each Entity.

www.ashishkulkarni.com 1211 March 2009

What Hibernate can do

� Hibernate Allows a Java Developer to work with Pure POJOs within the application without having to worry about persistence or database dialect.

� Hibernate can manage the complete lifecycle of an object/entity.

www.ashishkulkarni.com 1311 March 2009

Hibernate Lifecycle

www.ashishkulkarni.com 1411 March 2009

Beyond Hibernate

� With EJB 3 and JPA, Sun has incorporated much of Hibernate into the standard. Hibernate and TopLink are the 2 available Persistence Providers for JPA, Hibernate being the strong favourite.

� Hibernate itself is still expanding. Hibernate Search brings full-text indexes and Google-like search capabilities to any traditional database application.

www.ashishkulkarni.com 1511 March 2009

Questions

www.ashishkulkarni.com 1611 March 2009

Where to go from here

� Me – ashish@ebizss.com� My website – www.ashishkulkarni.com� www.hibernate.org� Books:

� Manning – Java Persistence with Hibernate – Bauer and King

www.ashishkulkarni.com 1711 March 2009

A bit about me

� I am a Java Contractor currently contracting with JP Morgan in Glasgow. Apart from contracting, I am actively working with other developers at my company to develop bespoke software for privateclients.

� I have been using JBoss Seam, Spring and Hibernate driven JPA for a couple of years now and am absolutely loving it.

� Having worked on FoxPro, Oracle, Visual C++ and SAP in the past,I have 15 years of development experience.

� I have been using Java since 2000 which to this day remains my favourite computer language.

� I have worked in a variety of industries such as Retail Banking,Investment Banking, Financial Institutions, Pharmaceuticals, Manufacturing, Automobiles and Telecommunications.