Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Hibernate jj


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Hibernate jj

  1. 1. JOE JACOB
  2. 2.  Hibernate is a free, open source Java package that makes it easy to work with relational databases. Hibernate makes it seem as if your database contains plain Java objects like you use every day, without having to worry about how to get them out of (or back into) mysterious database tables. Hibernate liberates you to focus on the objects and features of your application, without having to worry about how to store them or find them later.
  3. 3.  Cost effective. Learning is very easy compare to EJB. High Performance than EJB No more need for JDBC API for Result set handling. Switching to other SQL database requires few changes in Hibernate configuration file
  4. 4.  Database independent application Avoid writing queries Avoid JDBC API completely Hibernate uses connection pooling technique Automatic Key Generation Develop the Application in short Period of time
  5. 5.  DB2 MySQL PostgreSQL Oracle (any version) Microsoft SQL Server HypersonicSQL Informix Ingres Interbase Pointbase Mckoi SQL Progress FrontBase SAP DB Sybase
  6. 6.  Hibernate architecture has three main components: Connection Management Hibernate Connection management service provide efficient management of the database connections. Database connection is the most expensive part of interacting with the database as it requires a lot of resources of open and close the database connection.    Transaction management: Transaction management service provide the ability to the user to execute more than one database statements at a time.    Object relational mapping: Object relational mapping is technique of mapping the data representation from an object model to a relational data model. This part of the hibernate is used to select, insert, update and delete the records form the underlying table. When we pass an object to a method, Hibernate reads the state of the variables of that object and executes the necessary query.
  7. 7.  Hibernate is very good tool as far as object relational mapping is concern but in terms of ◦ connection management and ◦ transaction management, it is lacking in performance and capabilities. So usually hibernate is being used with other connection management and transaction management tools. For example apache DBCP is used for connection pooling with the Hibernate.
  8. 8.  Hibernate 3.0 provides three full-featured query facilities: Hibernate Query Language Hibernate Criteria Query API Hibernate Native Query
  9. 9.  The Criteria interface allows to create and execute object-oriented queries. It is powerful alternative to the HQL but has own limitations. Criteria Query is used mostly in case of multi criteria search screens, where HQL is not very effective. 
  10. 10.  Native SQL is handwritten SQL for all database operations like create, update, delete and select. Hibernate Native Query also supports stored procedures. Hibernate allows you to run Native SQL Query for all the database operations, so you can use your existing handwritten sql with Hibernate, this also helps you in migrating your SQL/JDBC based application to Hibernate.
  11. 11.  Configuring Hibernate Persistence Class Map the Object to the Database table (Using Annotation) Setting up the Database  Insert, Update and Delete records in the table (Hibernate automatically creates query to perform this operations)
  12. 12. Make SQL be object oriented •Classes and properties instead of tables and columns •Polymorphism •Associations •Much less verbose than SQLFull support for relational operations •Inner/outer/full joins, cartesian products •Projection •Aggregation (max, avg) and grouping •Ordering •Subqueries •SQL function calls
  13. 13. Simplest HQL Query: from AuctionItemi.e. get all the AuctionItems: List allAuctions = session.createQuery(“from AuctionItem”).list(); •SQL function calls
  14. 14. 1. HQL Select Query Example Retrieve a stock data where stock code is “7277″. Query query = session.createQuery("from Stock where stockCode = :code "); query.setParameter("code", "7277"); List list = query.list(); Query query = session.createQuery("from Stock where stockCode = 7277 "); List list = query.list();
  15. 15. 2. HQL Update Query Example Update a stock name to “DIALOG1″ where stock code is “7277″ Query query = session.createQuery("update Stock set stockName = :stockName" + " where stockCode = :stockCode"); query.setParameter("stockName", "DIALOG1"); query.setParameter("stockCode", "7277"); int result = query.executeUpdate(); Query query = session.createQuery("update Stock set stockName = DIALOG1" + " where stockCode = 7277"); int result = query.executeUpdate();
  16. 16. 3. HQL Delete Query Example Delete a stock where stock code is “7277″. Query query = session.createQuery("delete Stock where stockCode = :stockCode"); query.setParameter("stockCode", "7277"); int result = query.executeUpdate(); Query query = session.createQuery("delete Stock where stockCode = 7277"); int result = query.executeUpdate();
  17. 17. 4. HQL Insert Query ExampleIn HQL, only the INSERT INTO … SELECT … is supported; there is no INSERT INTO … VALUES. HQLonly support insert from another table. For exampleInsert a stock record from another backup_stock table. This can also calledbulk-insert statement. Query query = session.createQuery("insert into Stock(stock_code, stock_name)" + "select stock_code, stock_name from backup_stock"); int result = query.executeUpdate(); Note :The query.executeUpdate() will return how many number of record has been inserted, updated or deleted.
  18. 18. Like other operations, transactions are performed using the Session object.Here’s an example:Session session = sf.openSession();Transaction tx = session.beginTransaction();try { AuctionItem item = (AuctionItem) session.get(ActionItem.class, itemId); bid.setItem(item); item.getBids().add(bid); tx.commit();} catch (Exception e) { tx.rollback(); throw e;} finally { session.close();}
  19. 19. Performance of Hibernate web applications is improved using caching byoptimizing the database applications.The cache actually stores the data already loaded from the database, so that thetraffic between our application and the database will be reduced when theapplication want to access that data again. Maximum the application will workswith the data in the cache only. Whenever some another data is needed, thedatabase will be accessed. Because the time needed to access the database ismore when compared with the time needed to access the cache. So obviously theaccess time and traffic will be reduced between the application and the database.Here the cache stores only the data related to current running application. In orderto do that, the cache must be cleared time to time whenever the applications arechanging.
  20. 20. 1.Introduction. •First-level cache. •Second-level cache.2.Cache Implementations. •EHCache. •OSCache. •SwarmCache. •JBoss TreeCache.3.Caching Stringategies. •Read-only. •Read-Write. •Nonstriict read-write. •Transactional.4.Configuration.5.<cache> element.6.Caching the queries.
  21. 21. First-level cache.First-level cache always Associates with the Session object. Hibernate usesthis cache by default. Here, it processes one transaction after another one,means wont process one transaction many times. Mainly it reduces thenumber of SQL queries it needs to generate within a given transaction. That isinstead of updating after every modification done in the transaction, it updatesthe transaction only at the end of the transaction.
  22. 22. Second-level cacheSecond-level cache always associates with the Session Factory object.While running the transactions, in between it loads the objects at the SessionFactory level, so that those objects will available to the entire application,don’t bounds to single user. Since the objects are already loaded in thecache, whenever an object is returned by the query, at that time no need to gofor a database transaction. In this way the second level cache works. Here wecan use query level cache also. Later we will discuss about it.
  23. 23. Cache ImplementationsHibernate supports four open-source cache implementations namedEHCache (Easy Hibernate Cache), OSCache (Open Symphony Cache),Swarm Cache, and JBoss Tree Cache. Each cache has differentperformance, memory use, and configuration possibilities. .
  24. 24. EHCache (Easy Hibernate Cache) (org.hibernate.cache.EhCacheProvider)• It is fast. • lightweight. • Easy-to-use. • Supports read-only and read/write caching. • Supports memory-based and disk-based caching. • Does not support clustering.
  25. 25. OSCache (Open Symphony Cache) (org.hibernate.cache.OSCacheProvider)• It is a powerful .• flexible package• supports read-only and read/write caching.• Supports memory- based and disk-based caching.• Provides basic support for clustering via either JavaGroups or JMS
  26. 26. SwarmCache (org.hibernate.cache.SwarmCacheProvider)• is a cluster-based caching.• supports read-only or nonstrict read/write caching .• appropriate for applications those have more read operations than write operations.
  27. 27. Boss TreeCache (org.hibernate.cache.TreeCacheProvider)• is a powerful replicated and transactional cache.• useful when we need a true transaction-capable caching architecture .