Introduction to hibernate

20
By Harshit Rastogi

Transcript of Introduction to hibernate

Page 1: Introduction to hibernate

By Harshit Rastogi

Page 2: Introduction to hibernate

It is ORM (Object –Relation Mapping) Tool It uses POJO objects (Plain Old Java

Objects) No direct interaction with the database.

Page 3: Introduction to hibernate

SessionFactory – Is a factory which provides Session.

Session – Single unit of work. Dirty checking Hibernate xml file – ‘.hbm’ It uses reflection

Page 4: Introduction to hibernate

.

A typical POJO class

Page 5: Introduction to hibernate

Event.hbm.xml

<hibernate-mapping>

<class name="events.Event" table="EVENTS"> <id name="id" column="EVENT_ID"> <generator class="native"/> </id> <property name=“name"/> <property name="address" column="ADDRESS"/> </class>

</hibernate-mapping>

Page 6: Introduction to hibernate

hibenrate.hbm.xml

Page 7: Introduction to hibernate

Singleton class to create SessionFactory object

Page 8: Introduction to hibernate
Page 9: Introduction to hibernate

Get() - Return the persistent instance of the given entity class with the given identifier, or null if there is no such persistent instance.

Load() - Return the persistent instance of the given entity class with the given identifier, assuming that the instance exists

Flush() - Force this session to flush Delete() - Remove a persistent instance from the datastore. Contains() -  Check if this instance is associated with this

Session. Persist() -Make a transient instance persistent. Evict() - Remove this instance from the session cache() beginTransaction() closeTransaction()

Page 10: Introduction to hibernate

Execute complex queries containing conditions where clauses.

Criteria Interface is the option. Some typical SQL example:

Select * from cat where name like “Fritz%” and weight between + minweight + and + maxweight

Equivalent hibernate query: List cats =

sess.createCriteria(Cat.class) .add( Restrictions.like("name", "Fritz%") ) .add( Restrictions.between("weight", minWeight, maxWeight) ) .list();

Page 11: Introduction to hibernate

List cats = sess.createCriteria(Cat.class) .add( Restrictions.like("name", "Fritz%") ) .add( Restrictions.or( Restrictions.eq( "age", new

Integer(0) ), Restrictions.isNull("age") ) ) .list();

List cats = sess.createCriteria(Cat.class) .add( Restrictions.in( "name", new String[] { "Fritz", "Izi",

"Pk" } ) ) .add( Restrictions.disjunction() .add( Restrictions.isNull("age") ) .add( Restrictions.eq("age", new Integer(0) ) )

.add( Restrictions.eq("age", new Integer(1) ) ) .add( Restrictions.eq("age", new Integer(2) ) ) ) ) .list();

Page 12: Introduction to hibernate

table per class hierarchy table per subclass table per concrete class

Page 13: Introduction to hibernate

Exactly one table is required

Page 14: Introduction to hibernate

Each class has its own table with the primary key related to the main class. Four tables are involved.

Page 15: Introduction to hibernate

Each table defines columns for all properties of the class, including inherited properties. Three tables are involved for the subclasses

Page 16: Introduction to hibernate

Four ways of achieving mapping the in RDBMS. One-to-one One-to-many Many-to-one Many-to-many

Association can be unidirectional or bi-directional.

Page 17: Introduction to hibernate

One person can have only one address Table schema

create table Person ( personId bigint not null primary key, addressId bigint not null unique )

create table Address ( addressId bigint not null primary key )

Page 18: Introduction to hibernate

Many people sharing the same address Table Schema

create table Person ( personId bigint not null primary key, addressId bigint not null )

create table Address ( addressId bigint not null primary key )

Page 19: Introduction to hibernate

One person having many places to stay. Table Schema

create table Person ( personId bigint not null primary key ) create table Address ( addressId bigint not null primary key, personId

bigint not null )

Page 20: Introduction to hibernate

Caching can be done at various level. First level caching is done by session Second level can be done using cache

frameworks Hibernate supports various implementation EHCache OSCache SwarmCache JBoss TreeCache