29
Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution. Hibernate 3.1 Siddhanta Kumar Pattnaik

Hibernate 3.1.pptx

Embed Size (px)

Citation preview

PowerPoint Presentation

Hibernate 3.1Siddhanta Kumar PattnaikCopyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.1AgendaJDBC OverviewFeatures of Hibernate Core Objects Of HibernateHibernate Associations MappingHibernate Object stateHibernate Connection ManagementHibernate Transaction ManagementHibernate Cache implementationCaching Concurrency StrategyHibernate Query LanguageHibernate Interceptors and Events

2Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.2JDBC OverviewConnection con=null;try{//load the driverClass.forName(OracleDriver);//create the Connectioncon=DriverManger.getConnection(url,un,pw);con.setAutoCommit(false);//create the statementStatement st=con.createStatement();st.executeQuery(select * from );con.commit;}catch{con.rollback();}finally{con.close();}

3Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.Features of Hibernate Hibernate is responsible for establish the connection, creating statement ,preparing sql statement required and cleaning the resource.Hibernate supports two types of connection 1)DriverManager 2)Data Source connection .Hibernate support two types of transaction 1)JDBC 2)JTA Transaction Hibernate support various types of mappings .Hibernate support powerful Object Oriented Query Language .

4Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.Features of Hibernate Hibernate supports various Caching Technique . Default support for batch updates .Hibernate can run in CME and non CME Env .Hibernate supports various types of primary key generation algorithm .Hibernate support Lazy loading .

5Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution. Core objects of hibernateConfiguration(org.hibernate.cfg.Configuration) SessionFactory (org.hibernate.SessionFactory)Immutable objectThread safe Single instance per database Session (org.hibernate.Session)Single unit of work Not thread safe Transaction (org.hibernate.Transaction)

6Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.Object StateTransientObject instantiated with new operator and is not associated with a hibernate session.PersistentHas a representation in the database. Can be a object just has been saved or loaded from db.DetachedAn object that has been persistent and a valid reference exists for it outside the scope of its session.7Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.Hibernate Association MappingsHibernate support following Association Mapping .OneToOneOneToManyManyToMany

8Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.One To One Mapping9EMP_IDEMP_NAMESALARY1Sateesh1000002Harish150000SAL_ACC_IDEMP_ID1122EmployeeSalary_AccountCopyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.One To Many Mapping10EMP_IDEMP_NAMESALARY1Sateesh1000002Hrish150000AIDSTREETEMP_ID113th Phase1214th cross132nd cross1EmployeeAddressCopyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.Many To Many Mapping11EMP_IDEMP_NAMESALARY1Sateesh1000002Harish150000AIDSTREET113th Phase214th cross32nd crossEmployeeAddressEMP_ADD_EMP_IDEMP_ADD_AID11121323EMP_ADDCopyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.Hibernate Connection ManagementHibernate support following Connection Provider .DriverManagerConnectionProviderC3P0 Connection ProviderDataSource Connection Provider Custom Connection Provider

12Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.Hibernate Connection ManagementDriverManager Coonection ProviderBydefault Hibernate use this connection provider and this is not good for production .C3P0 Connection ProviderIt is a third party connection pooling algorithm .To use this we need to add specific property in hibernate configuration file .< property name=connection.provider_class>.. C3P0ConnectionProvider 10201

13Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.Hibernate Connection ManagementDataSource Coonection ProviderTo use this connection provider our hibernate application must run inside some application server . Data source connection will automatically participate in JTA transaction .we need to add some specific property for this in hibernate cfg file .Custom Connection ProviderCustomConnectionProvider

14Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.Hibernate Transaction ManagementTransaction is the process of performing multiple database operation as a single unit with all nothing criteria .Transaction must support ACID property .A-AtomicityC-ConsistencyI-IsolationD-Durability

15Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.15Hibernate Transaction ManagementHibernate support two types of transaction 1)JDBC 2)JTA TransactionJDBCTransactionBy default Hibernate use JDBC Transaction internally .To use JDBC transaction connection must be DriverManager or C3P0 .JTA Transaction To use JTA transaction we must use Data source connection .

MYSQLDataSourceorg.hibernate.transaction.JTATransactionFactory>

16Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.Hibernate Transaction Management.. JBossTransactionManagerLookup.class Jta/UserTransaction jta

17Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.Hibernate Cache ImplementationCache is a presentation of database near to application .when we implement caching it will increase the performance .we have three types of cache scope .Transactional Scope cache

18Application DBTX1TX2CacheCacheCopyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.Hibernate Cache ImplementationProcess Scope cache19Application DBTX1TX2CacheCopyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.Hibernate Cache ImplementationClustered Scope cache

20Load Balancing ServerApplication TX1TX2cacheDBApplication TX1TX1cacheJBOSS Instance 1JBOSS Instance 2JVM 1JVM 2Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.Hibernate Cache ArchitectureFirst Level CacheSession Cache is the first level Cache .Second Level cache or Query Cache Query cache can be process scope or cluster scope .To enable query cache we need to add some property .true..EhCacheProvider

21Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.Cache Provider Supported by Hibernate22Cache Provider classTypeCluster SafeQuery CacheENVHashtableorg.hibernate.cache.HashtableCacheProviderMemoryNoYesCME and nonCMEEHCacheorg.hibernate.cache.EhCacheProviderMemory,DiskNoYesCME and nonCMEOSCacheorg.hibernate.cache.OSCacheProviderMemory,Disk

NoyesCME and nonCME

SwarmCache org.hibernate.cache.SwarmCacheProviderclusteredYesNoCME and nonCMETreeCacheorg.hibernate.cache.TreeCacheProviderclusteredYesyesCME onlyCopyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.Cache Concurrency strategiesA concurrency strategy is a mediator which responsible for storing items of data in the cache and retrieving them from the cache.Read-only: A concurrency strategy suitable for data which never changes.Nonstrict-read-write: This strategy makes no guarantee of consistency between the cache and the database. Read-write: The READ-WRITE strategy works well for data that changes and must be committed.Transactional: The TRANSACTIONAL strategy is intended for use in an environment utilizing the Java Transaction API (JTA) to manage transactions across a number of XA resources. This strategy guarantees that a cache remains in sync with other resources, such as databases and queues .23Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.Cache Concurrency Strategy Support24Cacheread-onlynonstrict-read-writeread-writetransactionalHashtableYesYesYesEHCacheYesYesYesOSCacheYesYesYesSwarmCacheYesYesTreecacheYesYesCopyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.Hibernate Query LanguageHibernate support various types of Object Oriented Query Language .HQL(Hibernate Query Language)QBC(Query By Criteria)Native Query25Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.Hibernate Interceptors and EventsInterceptorsInterceptors provides a call back method from session to application ,allowling the application to intercept and manupulate the properties of persistence object before its saved, updated or loaded .Two kinds of interceptors ->SessionScope- Applied to pertucular session Session ses=sf.openSession(new Myinterceptors());->SessionFactory-Applied to all session created from session factory .newConfiguration().setInterceptor(new Myinterceptors());26Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.Hibernate Interceptors and EventsEventsIf we have to react to particular events in your persistence layer, you can also use the Hibernate3 event architecture. The event system can be used in addition, or as a replacement, for interceptors. All the methods of the Session interface correlate to an event. > LoadEvent, > FlushEvent> etc.27Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.Hibernate Interceptors and EventsEvent Listener Implements the appropriate interface for the event it wants to process and/or extend one of the convenience base classes

public class MyLoadListener implements LoadEventListener {// this is the single method defined by the LoadEventListener interfacepublic void onLoad(LoadEvent event, LoadEventListener.LoadType loadType) throws HibernateException {if ( !MySecurity.isAuthorized( event.getEntityClassName(), event.getEntityId() ) ) {throw MySecurityException("Unauthorized access");}}}28Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.Question?Copyright 2012 Manhattan Associates, Incorporated. Strictly Confidential. Not for Distribution.29