introduction to hibernate

Post on 19-May-2015

2.612 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

By Harshit Rastogi

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

Objects) No direct interaction with the database.

SessionFactory – Is a factory which provides Session.

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

.

A typical POJO class

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>

hibenrate.hbm.xml

Singleton class to create SessionFactory object

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

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

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

table per class hierarchy table per subclass table per concrete class

Exactly one table is required

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

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

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.

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 )

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 )

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 )

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

top related