patni hibernate 100113062516 phpapp02

39
8/9/2019 Patni Hibernate 100113062516 Phpapp02 http://slidepdf.com/reader/full/patni-hibernate-100113062516-phpapp02 1/39

Upload: neerajvit1073

Post on 30-May-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Patni Hibernate 100113062516 Phpapp02

8/9/2019 Patni Hibernate 100113062516 Phpapp02

http://slidepdf.com/reader/full/patni-hibernate-100113062516-phpapp02 1/39

Page 2: Patni Hibernate 100113062516 Phpapp02

8/9/2019 Patni Hibernate 100113062516 Phpapp02

http://slidepdf.com/reader/full/patni-hibernate-100113062516-phpapp02 2/39

Patni Internal

Object Relational Mapping (ORM)

The term object/relational mapping (ORM) refers to thetechnique of mapping a data representation from an objectmodel to a relational data model.

Various ORM products include :- Apache DB Project  NET Data Objects SimpleORM Sybase, Inc. Hibernate

Obtain the complete list of products from :-http://www.service-architecture.com/products/object-relational_mapping.html 

Page 3: Patni Hibernate 100113062516 Phpapp02

8/9/2019 Patni Hibernate 100113062516 Phpapp02

http://slidepdf.com/reader/full/patni-hibernate-100113062516-phpapp02 3/39

Patni Internal

Paradigm Mismatch

Problem of Identity

Problem of Sub Types

Problems relating to associations

Problem of object graph navigation

Result :-

Significant waste of time and effortSolution:-

Usage of ORM tool

Page 4: Patni Hibernate 100113062516 Phpapp02

8/9/2019 Patni Hibernate 100113062516 Phpapp02

http://slidepdf.com/reader/full/patni-hibernate-100113062516-phpapp02 4/39

Patni Internal

Hibernate Features

Hibernate is an object/relational mapping tool for Javaenvironments.

Eliminates manual data handling involved in SQL andJDBC.

Direct mapping of an object oriented solution

Provides Transparent Persistence

Data query and retrieval facilities with provisions to executenative SQL(s)

Page 5: Patni Hibernate 100113062516 Phpapp02

8/9/2019 Patni Hibernate 100113062516 Phpapp02

http://slidepdf.com/reader/full/patni-hibernate-100113062516-phpapp02 5/39

Patni Internal

Benefits of using Hibernate

Reduction of development time and effort otherwise spentwith manual data handling in SQL and JDBC.

Relieve the developer from 95 percent of common data persistence related programming tasks.

Object oriented solutions can be directly mapped to realworld problems.No longer the mapping between the Objectorient data model and relational data model is required.

ORM mapping facilitated using an XML

Higher Performance depending on usage

Open Source Project

Page 6: Patni Hibernate 100113062516 Phpapp02

8/9/2019 Patni Hibernate 100113062516 Phpapp02

http://slidepdf.com/reader/full/patni-hibernate-100113062516-phpapp02 6/39

Patni Internal

Hibernate Usage

Hibernate works well in a managed environment with allmajor J2EE application servers, or even in standalone Javaapplications .

It is most useful with object-oriented Domain Model and business logic in the Java-based middle-tier.

Hibernate may not be the best solution for data-centricapplications that only use stored-procedures to implementthe business logic in the database.

Hibernate is most suitable for applications based on a richDomain Model involving complex interaction betweenentities

Page 7: Patni Hibernate 100113062516 Phpapp02

8/9/2019 Patni Hibernate 100113062516 Phpapp02

http://slidepdf.com/reader/full/patni-hibernate-100113062516-phpapp02 7/39

Hibernate Setup

Page 8: Patni Hibernate 100113062516 Phpapp02

8/9/2019 Patni Hibernate 100113062516 Phpapp02

http://slidepdf.com/reader/full/patni-hibernate-100113062516-phpapp02 8/39

Patni Internal

Installation

Download the latest version of Hibernate(e.g. hibernate-3.0.5.zip) from the site http://www.hibernate.org

The zip file will contain the Hibernate jar (along withother third party jar files for various features like

logging,connection pooling), template of configurationfiles and source code of example applicationsimplemented using Hibernate

Set the class path to include the necessary jar files and

the configuration files

Page 9: Patni Hibernate 100113062516 Phpapp02

8/9/2019 Patni Hibernate 100113062516 Phpapp02

http://slidepdf.com/reader/full/patni-hibernate-100113062516-phpapp02 9/39

Patni Internal

Configuration Files

Hibernate uses 3 configuration files during runtime, thesefiles need to be present in its Class path :-

hibernate.cfg.xml : Configuration of properties (bothrelated and not related to Hibernate).Contains the ORM

Mapping file names

hibernate.properties : Configuration of properties (bothrelated and not related to Hibernate).

Application.

hbm.xml :This is the ORM mapping filecorresponding to your application.The name of this file is

defined by the developer (mapping is done in thehibernate.cfg.xml ), e.g. Event.hbm.xml.There can be morethan one file like this for the same application

Page 10: Patni Hibernate 100113062516 Phpapp02

8/9/2019 Patni Hibernate 100113062516 Phpapp02

http://slidepdf.com/reader/full/patni-hibernate-100113062516-phpapp02 10/39

Patni Internal

hibernate.cfg.xml

Example<hibernate-configuration><session-factory>

<property name=

connection.datasource">java:comp/env/jdbc/quickstart</property><property name="show_sql">false</property><property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>

<!-- Mapping files --><mapping resource="Cat.hbm.xml"/>

</session-factory></hibernate-configuration>

Page 11: Patni Hibernate 100113062516 Phpapp02

8/9/2019 Patni Hibernate 100113062516 Phpapp02

http://slidepdf.com/reader/full/patni-hibernate-100113062516-phpapp02 11/39

Patni Internal

hibernate.cfg.xml

Hibernate XML-based configuration.to know the the JDBCconnection parameter(the other approach being the properties file)

A SessionFactory is Hibernate's concept of a singledatastore, multiple databases can be used by creatingmultiple XML configuration files and creating multipleConfiguration and SessionFactory objects inyour application.

The element (resource mapping ) provides the name of theORM mapping file(s) for the persistent classes of theapplication

Page 12: Patni Hibernate 100113062516 Phpapp02

8/9/2019 Patni Hibernate 100113062516 Phpapp02

http://slidepdf.com/reader/full/patni-hibernate-100113062516-phpapp02 12/39

Patni Internal

hibernate.properties

Please find the attached template of the property file

hibernate.properties

Page 13: Patni Hibernate 100113062516 Phpapp02

8/9/2019 Patni Hibernate 100113062516 Phpapp02

http://slidepdf.com/reader/full/patni-hibernate-100113062516-phpapp02 13/39

Page 14: Patni Hibernate 100113062516 Phpapp02

8/9/2019 Patni Hibernate 100113062516 Phpapp02

http://slidepdf.com/reader/full/patni-hibernate-100113062516-phpapp02 14/39

Page 15: Patni Hibernate 100113062516 Phpapp02

8/9/2019 Patni Hibernate 100113062516 Phpapp02

http://slidepdf.com/reader/full/patni-hibernate-100113062516-phpapp02 15/39

Working with Hibernate

Page 16: Patni Hibernate 100113062516 Phpapp02

8/9/2019 Patni Hibernate 100113062516 Phpapp02

http://slidepdf.com/reader/full/patni-hibernate-100113062516-phpapp02 16/39

Patni Internal

Core Interfaces

Configuration Interface:The application uses the configurationinstance to specify the location of the mapping documents andHibernate-specific properties and then create the SessionFactory

SessionFactory Interface: A SessionFactory is an expensive-to-create,thread safe object intended to be shared by all applicationthreads.It is created once, usually on application startup, from aConfiguration instance.

Session Interface: A single-threaded, short-lived objectrepresenting a conversation between the application and the persistentstore.

Page 17: Patni Hibernate 100113062516 Phpapp02

8/9/2019 Patni Hibernate 100113062516 Phpapp02

http://slidepdf.com/reader/full/patni-hibernate-100113062516-phpapp02 17/39

Patni Internal

Core Interfaces

Transaction Interface: Optional interface to managetransactions.Hibernate applications may choose not to use thisinterface, instead manage transactions through their infrastructurecode.

Query Interface : This allows the developer to perform queries againstthe database and control how the query is executed

Page 18: Patni Hibernate 100113062516 Phpapp02

8/9/2019 Patni Hibernate 100113062516 Phpapp02

http://slidepdf.com/reader/full/patni-hibernate-100113062516-phpapp02 18/39

Patni Internal

States of an Instance

Instances(of Persistent Classes) may exist in one of threestates: Transient :The instance is not, and has never been associated withany persistence context. It has no persistent identity (primary keyvalue).

Persistent :The instance is currently associated with a persistencecontext. It has a persistent identity (primary key value) and, perhaps,a corresponding row in the database. For a particular persistencecontext

Detached :The instance was once associated with a persistencecontext, but that context was closed, or the instance was serialized toanother process. It has a persistent identity and, perhaps, acorresponding row in the database.For detached instances, Hibernatemakes no guarantees about the relationship between persistent

identity and Java identity.

Page 19: Patni Hibernate 100113062516 Phpapp02

8/9/2019 Patni Hibernate 100113062516 Phpapp02

http://slidepdf.com/reader/full/patni-hibernate-100113062516-phpapp02 19/39

Patni Internal

States of an Instance

Example:Session session = HibernateUtil.currentSession();Transaction tx = session.beginTransaction();Person aPerson = (Person) session.load(Person.class, personId);

Event anEvent = (Event) session.load(Event.class, eventId);tx.commit();HibernateUtil.closeSession();aPerson.getEvents().add(anEvent); // aPerson is detachedSession session2 = HibernateUtil.currentSession();Transaction tx2 = session.beginTransaction();

session2.update(aPerson); // Reattachment of aPersontx2.commit();HibernateUtil.closeSession();

Page 20: Patni Hibernate 100113062516 Phpapp02

8/9/2019 Patni Hibernate 100113062516 Phpapp02

http://slidepdf.com/reader/full/patni-hibernate-100113062516-phpapp02 20/39

Basic O/R Mapping

Page 21: Patni Hibernate 100113062516 Phpapp02

8/9/2019 Patni Hibernate 100113062516 Phpapp02

http://slidepdf.com/reader/full/patni-hibernate-100113062516-phpapp02 21/39

Patni Internal

Basic O/R Mapping

Object/relational mappings are defined in an XMLdocument.

The mapping language is Java-centric, meaning thatmappings are constructed around persistent class

declarations, not table declarations.

A number of tools exist to generate the mappingdocument, including XDoclet, Middlegen and

AndroMDA.

Page 22: Patni Hibernate 100113062516 Phpapp02

8/9/2019 Patni Hibernate 100113062516 Phpapp02

http://slidepdf.com/reader/full/patni-hibernate-100113062516-phpapp02 22/39

Patni Internal

Element <hibernate-mapping>

The root element in O/R mapping file.Contains thefollowing attributes

schema (optional): The name of a database schema.catalog (optional): The name of a database catalog.

default-cascade (optional - defaults to none): Adefault cascade style.package (optional): Specifies a package prefix to assumefor unqualified class names in the mapping document.

Get the complete list and more information on theattributes from Hibernate_reference.pdf(Basic O/R Mapping)

Page 23: Patni Hibernate 100113062516 Phpapp02

8/9/2019 Patni Hibernate 100113062516 Phpapp02

http://slidepdf.com/reader/full/patni-hibernate-100113062516-phpapp02 23/39

Patni Internal

Element <class>

You may declare a persistent class using the classelement:

name (optional): The fully qualified Java class name of the persistent class (or interface). If this attribute is missing, it is

assumed that the mapping is for a non-POJO entity. table (optional - defaults to the unqualified class name): Thename of its database table.mutable (optional, defaults to true): Specifies that instances of the class are (not) mutable.

Get the complete list and more information on theattributes from Hibernate_reference.pdf(Basic O/R Mapping)

Page 24: Patni Hibernate 100113062516 Phpapp02

8/9/2019 Patni Hibernate 100113062516 Phpapp02

http://slidepdf.com/reader/full/patni-hibernate-100113062516-phpapp02 24/39

Patni Internal

Element <id>

Mapped classes must declare the primary key column of the database tab:

name (optional): The name of the identifier property.type (optional): A name that indicates the Hibernate type.

column (optional - defaults to the property name): Thename of the primary key column.

There is an alternative <composite-id> declaration

to allow access to legacy data with composite keysGet the complete list and more information on the

attributes from Hibernate_reference.pdf(Basic O/R Mapping)

Page 25: Patni Hibernate 100113062516 Phpapp02

8/9/2019 Patni Hibernate 100113062516 Phpapp02

http://slidepdf.com/reader/full/patni-hibernate-100113062516-phpapp02 25/39

Patni Internal

Element <property>

Mapped classes must declare the primary key column of the database tab:

name: the name of the property, with an initial lowercaseletter.

column (optional - defaults to the property name): the nameof the mapped database table column. This may also be specified by nested <column> element(s). type (optional): a name that indicates the Hibernate type.

Get the complete list and more information on theattributes from Hibernate_reference.pdf(Basic O/R Mapping)

Page 26: Patni Hibernate 100113062516 Phpapp02

8/9/2019 Patni Hibernate 100113062516 Phpapp02

http://slidepdf.com/reader/full/patni-hibernate-100113062516-phpapp02 26/39

Collections & Associations

Page 27: Patni Hibernate 100113062516 Phpapp02

8/9/2019 Patni Hibernate 100113062516 Phpapp02

http://slidepdf.com/reader/full/patni-hibernate-100113062516-phpapp02 27/39

Patni Internal

Collection

Hibernate supports all kinds of collection mappings, a <set> being most common.

The other collection mapping include <list>, <map>, <bag>and <array>

Collection instances are distinguished in the database by theforeign key of the entity that owns the collection.For amany-to-many association(or n:m entity relationship), anassociation table is needed. This foreign key is referred to as

the collection key column (or columns) of the collectiontable. The collection key column is mapped by the <key>element.

Page 28: Patni Hibernate 100113062516 Phpapp02

8/9/2019 Patni Hibernate 100113062516 Phpapp02

http://slidepdf.com/reader/full/patni-hibernate-100113062516-phpapp02 28/39

Patni Internal

Collection Mapping

Example :<class name="Department" table="DEPARTMENT">

<id name="id" column="DEPCODE"><generator class="increment"/></id>

<property name="DeptName" column="DNAME"/><set name="EMPLIST">

<key column="DEPCODE"/><one-to-many column = "DEPTID" class="Employee"/>

</set>

</class>

Page 29: Patni Hibernate 100113062516 Phpapp02

8/9/2019 Patni Hibernate 100113062516 Phpapp02

http://slidepdf.com/reader/full/patni-hibernate-100113062516-phpapp02 29/39

Patni Internal

Associations

Associations refer to relationships between entities

Using Hibernate, we can represent relationships of allcardinality,i.e. one-to-one, one-to-many,many-to-one andmany-to-many

A many-to-many association will result in a link or association table

Hibernate supports polymorphic association

Page 30: Patni Hibernate 100113062516 Phpapp02

8/9/2019 Patni Hibernate 100113062516 Phpapp02

http://slidepdf.com/reader/full/patni-hibernate-100113062516-phpapp02 30/39

Exercise 3

Page 31: Patni Hibernate 100113062516 Phpapp02

8/9/2019 Patni Hibernate 100113062516 Phpapp02

http://slidepdf.com/reader/full/patni-hibernate-100113062516-phpapp02 31/39

HQL(Hibernate Query Language)

Page 32: Patni Hibernate 100113062516 Phpapp02

8/9/2019 Patni Hibernate 100113062516 Phpapp02

http://slidepdf.com/reader/full/patni-hibernate-100113062516-phpapp02 32/39

Patni Internal

HQL(Hibernate Query Language)

Hibernate is equipped with an extremely powerful querylanguage that (quite intentionally) looks very much likeSQL

HQL is fully object-oriented, understanding notions like

inheritance,polymorphism and association.

Queries are case-insensitive, except for names of Javaclasses and properties.

HQL isn¶t a data-manipulation language like SQL.It is usedonly for object retrieval and not for updating,inserting or deleting data.Object state synchronization is the work of the persistence manager.

Page 33: Patni Hibernate 100113062516 Phpapp02

8/9/2019 Patni Hibernate 100113062516 Phpapp02

http://slidepdf.com/reader/full/patni-hibernate-100113062516-phpapp02 33/39

Patni Internal

HQL example 1

E.g. Get a list of all the employees (Table :MASTER_EMPLOYEE)

SQL :-

select * from MASTER_EMPLOYEE EMP

HQL :-from MASTER_EMPLOYEE EMP

Programmatically :-Query q = session.createQuery(³fromMASTER_EMPLOYEE EMP ´) ;List result = q.list();

Page 34: Patni Hibernate 100113062516 Phpapp02

8/9/2019 Patni Hibernate 100113062516 Phpapp02

http://slidepdf.com/reader/full/patni-hibernate-100113062516-phpapp02 34/39

Patni Internal

HQL example 2

E.g. Get a list of all the employees (Table :MASTER_EMPLOYEE) where D_CODE = ¶00A¶

SQL :-select * from MASTER_EMPLOYEE EMP

where EMP.D_CODE = ¶00A¶

HQL Programmatically :-Query q = session.createQuery(³fromMASTER_EMPLOYEE EMP where EMP.D_CODE =:code ´) ;q.setString(³code´,´00A´);List result = q.list();

Page 35: Patni Hibernate 100113062516 Phpapp02

8/9/2019 Patni Hibernate 100113062516 Phpapp02

http://slidepdf.com/reader/full/patni-hibernate-100113062516-phpapp02 35/39

Patni Internal

HQL example 3

E.g. Get a count of all the employees SQL :-

select count(*) from MASTER_EMPLOYEE EMP

HQL Programmatically :-

Query q = session.createQuery(³select count(*) fromMASTER_EMPLOYEE EMP´) ;List result = q.list();

The List(result) returned in the above case will containsingle object of the Integer Class.Incase of functions likeavg() the object will be of Float Class

The supported aggregate functions are avg(...), sum(...), min(...), max(...) count(*)

count(...), count(distinct ...), count(all...)

Page 36: Patni Hibernate 100113062516 Phpapp02

8/9/2019 Patni Hibernate 100113062516 Phpapp02

http://slidepdf.com/reader/full/patni-hibernate-100113062516-phpapp02 36/39

Patni Internal

HQL

HQL provides several other features for dataretrieval which include :-

Sub queryingJoin types supported by ANSI SQL (e.g. inner join,left outer,etc)

Polymorphic queriesOrdering/gropingUsage of Expressions [e.g. emp.name in ( µRakesh', µManish',µDhiraj' )]Executing native SQL queries

Using stored procedures for querying

Refer to hibernate_reference.pdf(Chapters : HQL,CriteriaQuery,Native SQL)

Page 37: Patni Hibernate 100113062516 Phpapp02

8/9/2019 Patni Hibernate 100113062516 Phpapp02

http://slidepdf.com/reader/full/patni-hibernate-100113062516-phpapp02 37/39

Patni Internal

Appendix

ThreadLocal :

Each thread that accesses one (via its get or set method) has its own,independently initialized copy of the variable. ThreadLocal objects aretypically private static variables in classes that wish to associate statewith a thread (Transaction ID).

Domain Model:An object model of the domain problem that incorporates both

 behavior and data[BACK ]

Page 38: Patni Hibernate 100113062516 Phpapp02

8/9/2019 Patni Hibernate 100113062516 Phpapp02

http://slidepdf.com/reader/full/patni-hibernate-100113062516-phpapp02 38/39

Patni Internal

Appendix

Transparent PersistenceIn object-relational mapping products, the ability to directlymanipulate data stored in a relational database using an object programming language is called transparent persistence

[BACK ]

Persistent classesClasses in an application that implement the entities of the business problem (e.g. Customer and Order in an E-commerce application)

[BACK ]

Page 39: Patni Hibernate 100113062516 Phpapp02

8/9/2019 Patni Hibernate 100113062516 Phpapp02

http://slidepdf.com/reader/full/patni-hibernate-100113062516-phpapp02 39/39

Patni Internal

References

URL

www.hibernate.org

R eference Material

hibernate_reference.pdf(can be downloaded from the

above site)

Hibernate in Action(Christian Bauer & Gavin King)

[Incase of any doubts or queries about this presentation

mail me at [email protected]]