- Hibernate is an ORM tool that uses POJO objects and allows working with objects instead of directly with the database.
- It uses a SessionFactory to provide Sessions which represent a single unit of work and handles dirty checking. Mapping is defined in XML files.
- Hibernate supports complex queries using the Criteria interface to create restrictions and conditions equivalent to SQL queries.
9. 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()
10. 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();
11. 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();
12. table per class hierarchy table per subclass table per concrete class
14. Each class has its own table with the primary key related to the main class. Four tables are involved.
15. Each table defines columns for all properties of the class, including inherited properties. Three tables are involved for the subclasses
16. 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.
17. 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 )
18. 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 )
19. 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 )
20. 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