hibernate

19
Hibernate Hibernate An object-relational An object-relational mapping solution for Java mapping solution for Java

Upload: ajay-kemparaj

Post on 19-May-2015

8.474 views

Category:

Technology


0 download

DESCRIPTION

Brief Introduction about hibernate

TRANSCRIPT

Page 1: Hibernate

HibernateHibernate

An object-relational mapping An object-relational mapping solution for Javasolution for Java

Page 2: Hibernate

Buzz WordsBuzz Words

Persistence Persistence

O/R Mapping O/R Mapping ((Object-Relational mappingObject-Relational mapping ) )

Page 3: Hibernate

What Is Persistence ?What Is Persistence ?The storage of an object on a disk or other The storage of an object on a disk or other permanent storage device or data that exists permanent storage device or data that exists from session to sessionfrom session to session as opposed to transient dataas opposed to transient data

Persistent data typically implies that it is Persistent data typically implies that it is durabledurable (i.e. will survive a crash or shutdown of the (i.e. will survive a crash or shutdown of the process) usually with some guarantee of process) usually with some guarantee of integrityintegrityPersistence generally implies use of a databasePersistence generally implies use of a database One could use the file system (with suitably careful One could use the file system (with suitably careful

procedures)procedures)

Page 4: Hibernate

Persistence ClassPersistence Class

Default Default constructorconstructor

Get/set pairsGet/set pairs

Collection Collection property is an property is an interface typeinterface type

Identifier Identifier propertyproperty

public class AuctionItem {public class AuctionItem {private Long _id;private Long _id;private Set _bids;private Set _bids;private Bid _successfulBidprivate Bid _successfulBidprivate String _description;private String _description;

public Long getId() {public Long getId() {return _id;return _id;

}}private void setId(Long id) {private void setId(Long id) {

_id = id;_id = id;}}public String getDescription() {public String getDescription() {

return _description;return _description;}}public void setDescription(String desc) {public void setDescription(String desc) {

_description=desc;_description=desc;}}……

}}

Page 5: Hibernate

O/R Mapping O/R Mapping

Object-Relational mappingObject-Relational mapping (aka (aka O/RMO/RM, , ORMORM, , and and O/R mappingO/R mapping), is a ), is a programmingprogramming technique technique for converting data between incompatible type for converting data between incompatible type systems in databases and systems in databases and Object-orientedObject-oriented programming languages. In effect, this creates a programming languages. In effect, this creates a "virtual "virtual object databaseobject database" which can be used from " which can be used from within the programming language. There are within the programming language. There are both free and commercial packages available both free and commercial packages available that perform object-relational mapping, although that perform object-relational mapping, although some programmers opt to create their own ORM some programmers opt to create their own ORM tools.tools.

Page 6: Hibernate

O/R Mapping O/R Mapping

Most business database applications use Most business database applications use relational databasesrelational databasesNeed to map the objects in the application to Need to map the objects in the application to tables in the databasetables in the databaseSometimes be a simple matter of mapping Sometimes be a simple matter of mapping individual classes to separate database tablesindividual classes to separate database tablesHowever, if the class structure is more complex, However, if the class structure is more complex, then the mapping must be carefully considered then the mapping must be carefully considered to allow data to be represented and accessed as to allow data to be represented and accessed as efficiently as possibleefficiently as possible

Page 7: Hibernate

Object Relational MappingObject Relational Mapping

Page 8: Hibernate

O/R Mapping O/R Mapping

..JavaObjectint id;String name;String getName()int getId()void setName(String)void setId(int)

SQL Table

id [number] primary key,name [varchar(50)]

Magic Happens Here(O/R Mapper – i.e. Hibernate)

Page 9: Hibernate

Who, What, WhenWho, What, When

Officially maintained by Jboss, Inc, started in Officially maintained by Jboss, Inc, started in December, 2001.December, 2001.

Hibernate is an object-relational mapping (ORM) Hibernate is an object-relational mapping (ORM) solution for the Java language. It is free, open solution for the Java language. It is free, open source software that is distributed under the source software that is distributed under the GNU Lesser General Public License. It provides GNU Lesser General Public License. It provides an easy to use framework for mapping an an easy to use framework for mapping an object-oriented domain model to a traditional object-oriented domain model to a traditional relational database.relational database.

Page 10: Hibernate

History is Awesome!History is Awesome!

Hibernate was developed by a team of Java software developers Hibernate was developed by a team of Java software developers around the world led by Gavin King.around the world led by Gavin King.

JBoss Group (now part of Red Hat) later hired the lead Hibernate JBoss Group (now part of Red Hat) later hired the lead Hibernate developers and worked with them in supporting Hibernate. This developers and worked with them in supporting Hibernate. This move brought Hibernate development under Java EE move brought Hibernate development under Java EE Specifications.Specifications.

The current version of Hibernate is Version 3.1, which enriched the The current version of Hibernate is Version 3.1, which enriched the platform with new features like a new Interceptor/Callback platform with new features like a new Interceptor/Callback architecture, user defined filters, and JDK 5.0 Annotations (Java's architecture, user defined filters, and JDK 5.0 Annotations (Java's metadata feature). Hibernate 3 is also very close to the EJB 3.0 metadata feature). Hibernate 3 is also very close to the EJB 3.0 specification (although it was finished before the EJB 3.0 specification (although it was finished before the EJB 3.0 specification was released by the Java Community Process) and specification was released by the Java Community Process) and serves as the backbone for the EJB 3.0 implementation of JBoss.serves as the backbone for the EJB 3.0 implementation of JBoss.

Page 11: Hibernate

ArchitectureArchitecture

• Hibernate basically sits between the DB and your code

• Can map persistent objects to tables

Page 12: Hibernate

Why Hibernate ?Why Hibernate ?

The purpose of Hibernate is to relieve the The purpose of Hibernate is to relieve the developer from a significant amount of common developer from a significant amount of common data persistence-related programming tasks. data persistence-related programming tasks. Hibernate adapts to the developer's Hibernate adapts to the developer's development process, whether starting from development process, whether starting from scratch or from a legacy database. scratch or from a legacy database. Minimize database access with smart fetching Minimize database access with smart fetching strategiesstrategiesOpportunities for aggressive cachingOpportunities for aggressive cachingStructuralStructural mapping more robust when mapping more robust when object/data model changesobject/data model changes

Page 13: Hibernate

Why Hibernate ?Why Hibernate ?

• Minimizes CodeMinimizes Code

• Does not require a containerDoes not require a container

• Model is not tied to persistence Model is not tied to persistence implementationimplementation

Page 14: Hibernate

Features Features

Hibernate not only takes care of the mapping Hibernate not only takes care of the mapping from Java classes to database tables (and from from Java classes to database tables (and from Java data types to SQL data types), but also Java data types to SQL data types), but also provides data query and retrieval facilities and provides data query and retrieval facilities and can significantly reduce development time can significantly reduce development time otherwise spent with manual data handling in otherwise spent with manual data handling in SQL and JDBC. Hibernate generates the SQL SQL and JDBC. Hibernate generates the SQL calls and relieves the developer from manual calls and relieves the developer from manual result set handling and object conversion, result set handling and object conversion, keeping the application portable to all SQL keeping the application portable to all SQL databases.databases.

Page 15: Hibernate

Features Features HibernateDaoSupport – superclass, easy HibernateTemplate HibernateDaoSupport – superclass, easy HibernateTemplate

accessaccess Database independence - sits between the database and your Database independence - sits between the database and your

java code, easy database switch without changing any codejava code, easy database switch without changing any code Object / Relational Mapping (ORM) - Allows a developer to treat Object / Relational Mapping (ORM) - Allows a developer to treat

a database like a collection of Java objectsa database like a collection of Java objects Object oriented query language (HQL) - *Portable* query Object oriented query language (HQL) - *Portable* query

language, supports polymorphic queries etc.language, supports polymorphic queries etc. You can also still issue native SQL, and also queries by You can also still issue native SQL, and also queries by

“Criteria” (specified using “parse tree” of Java objects)“Criteria” (specified using “parse tree” of Java objects) Hibernate Mapping - Uses HBM XML files to map value objects Hibernate Mapping - Uses HBM XML files to map value objects

(POJOs) to database tables (POJOs) to database tables Transparent persistence - Allows easy saves/delete/retrieve for Transparent persistence - Allows easy saves/delete/retrieve for

simple value objectssimple value objects Very high performance “in general” due to intelligent (2-level) Very high performance “in general” due to intelligent (2-level)

caching, although in a few cases hand-written SQL might beat itcaching, although in a few cases hand-written SQL might beat it

Page 16: Hibernate

Hibernate's GoalHibernate's Goal

• Remove 95% of common data persistence Remove 95% of common data persistence problemsproblems

Page 17: Hibernate

Advantages Advantages

Support for Query Language

Database Dependent Code

Maintenance Cost

Optimize Performance

Automatic Versioning and Time Stamping

Open-Source, Zero-Cost Product License

Scalable

Page 18: Hibernate

DisadvantagesDisadvantages

Steep Learning CurveSteep Learning CurveUse of Hibernate is an overhead for the applications which are :

• simple and use one database that never change

• need to put data to database tables, no further SQL queries

• there are no objects which are mapped to two different tables Hibernate increases extra layers and complexity. So for these types of applications JDBC is the best choice.

Page 19: Hibernate

DisadvantagesDisadvantages

Support for Hibernate on Internet is not sufficient.

Hibernate does not allow some type of queries which are supported by JDBC. For example It does not allow to insert multiple objects (persistent data) to same table using single query. Developer has to write separate query to insert each object.