Your SlideShare is downloading. ×
Hibernate 18052012
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.


Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Hibernate 18052012


Published on

basic hibernate

basic hibernate

  • Be the first to comment

  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 1. Hibernate Introduction and Overview >By Manisha May 2012
  • 2. Agenda At the end of this course, you should be able to:  Describe the basics of Hibernate  Use Hibernate configuration  Explain basic O/R Mapping  Write simple application in Hibernate ● Q&A2 Hibernate Basic May 2012
  • 3. Hibernate ● ORM technology  Consistent database communication and Increased maintainability: In Hibernate database communication policies are applied as configuration, in contrast as to code them in SQL based communication.  Developers with limited SQL knowledge can participate in development  Reduced testing time  Performance Optimization: Fine tuning database communication, adopting caching strategies can be done with configuration.  Portable code across multiple databases: Hibernate API and HQL can facilitate database neutral programming.3 Hibernate Basic May 2012
  • 4. What is ORM?  Object Relational Mapping (ORM) frameworks are those which encapsulate SQL communication with relational databases, with object- oriented communication.  ORM is a programming way for converting data between incompatible type systems in relational databases and object-oriented programming languages.  Currently, several ORM frameworks such as Entity beans in Enterprise JavaBeans (EJB), TopLink from Oracle, iBatis, Hibernate, and so on are available.4 Hibernate Basic May 2012
  • 5. Difference in conventional and ORM based implementation Conventional Implementation Service Data Access Database Object Object Implementation with ORM Data Service Hibernate Access Object Database Object5 Hibernate Basic May 2012
  • 6. High level Hibernate Architecture Application Persistent Objects Hibernate Configuration Mapping Database6 Hibernate Basic May 2012
  • 7. Basic content of Hibernate application In any standard hibernate application will have below data  Pojo class of table  Configuration files(hibernate.cfg.xml)  Mapping file(xxx.hbm.xml)  Application code to create Hibernate Runtime and execute7 Hibernate Basic May 2012
  • 8. How to use Hibernate for data communication? Follow these steps to use Hibernate for data communication:  Prepare domain objects (POJO) for table.  Write mapping information.  Write Hibernate configuration.  Create Hibernate Runtime.  Use Hibernate Runtime to communicate with database.8 Hibernate Basic May 2012
  • 9. Hibernate Example: Prepare pojo object(Class to table mapping) package; public class Employee implements Serializable{ // attributes - relate to business entities private Integer id; private firstName; private lastName; // accessor and mutator methods public Integer getId() { //implementation code } …. public void printEmployeeName() { // some business method } }9 Hibernate Basic May 2012
  • 10. Write mapping information(Table mapping to conf. Xml file) <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" ""> <hibernate-mapping> <!– Employee class mapping to EMPLOYEE table--> <class name="“ table=“EMPLOYEE"> <!– identifier column mapping--> <id name="id“ column=“ID“ type="long"> <generator class="native"/> </id> <!– property mapping---> <property name=“firstName“ column=“FIRST_NAME“ type="string"/> <property name=“lastName“ column=“LAST_NAME“ type="string"/> </class> </hibernate-mapping>10 Hibernate Basic May 2012
  • 11. Write Hibernate configuration information Hibernate.cfg.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" ""> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property> <property name="hibernate.connection.username">sa</property> <property name="hibernate.connection.password"></property> <property name="hibernate.connection.url">jdbc:hsqldb:file:testdb</property> <property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property> <property name="current_session_context_class">thread</property> <property name="show_sql">true</property> <property name="">update</property> <mapping resource="com/hibernate/test/Employee.hbm.xml"/> </session-factory> </hibernate-configuration>11 Hibernate Basic May 2012
  • 12. Write Hibernate configuration informationThe following are illustrative (not exhaustive) configuration parameters sorted by their categories. With category description we can guess on what kind of values could be assigned to individual parameters. Hibernate JDBC Properties (useful for standalone) – hibernate.connection.driver_class – hibernate.connection.url – hibernate.connection.username – hibernate.connection.password Hibernate Datasource Properties (useful inside container) – hibernate.connection.datasource – hibernate.jndi.url List of managed entities Hibernate Configuration Properties: – hibernate.dialect n hibernate.order_updates12 Hibernate Basic May 2012
  • 13. Create Hibernate runtime Before creating hibernate runtime, lets understand It  Hibernate Session, Transaction, and all Objects working behind them are collectively called as Hibernate Runtime.  Information like how to connect to database, how to map entity data to table row, how to handle transactions and many more are provided to Hibernate Runtime by means of configuration.  As Hibernate is designed to operate in many different environments. Hence, there are: – Many configurable parameters of various category. – Many ways to configure Hibernate Runtime.13 Hibernate Basic May 2012
  • 14. Create hibernate runtime public class HibernateUtil { private static final SessionFactory sessionFactory; static { try {// Create the SessionFactory from hibernate.cfg.xml sessionFactory = new Configuration().configure() .buildSessionFactory(); } catch (Throwable ex) { throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } }14 Hibernate Basic May 2012
  • 15. Use Hibernate Runtime public class EemployeetManager { private void createAndStoreEmployee(String firstNm, String lastNm) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); Employee theEmp = new Employee(); theEmp.setFirstName(firstNm); theEmp.setLastName(lastNm);; session.getTransaction().commit(); } @SuppressWarnings("unchecked") private List<Employee> listEmployee() { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); List<Employee> result = session.createQuery("from Eemployee").list(); session.getTransaction().commit(); return result; } }15 Hibernate Basic May 2012
  • 16. Example artifacts in Hibernate Architecture Application Persistent Objects Configuration Hibernate Mapping Hibernate.cfg.xml Employee.hbm.xm l Database jdbc:hsql:file:testdb16 Hibernate Basic May 2012
  • 17. Demo !!!17 Hibernate Basic May 2012
  • 18. Questions and Answers Choose the correct option: ORM framework is the one which: a. Abstracts communication with object-oriented database. b. Abstracts communication with Relational database. c. Acts as database driver. d. Is a replacement of Java EE Application Container.18 Hibernate Basic May 2012
  • 19. Questions and Answers Choose the correct option: We can configure JNDI name for session factory to which Hibernate can automatically bind its _. a. SessionFactory b. JMX deployment c. hibernate.dialect19 Hibernate Basic May 2012
  • 20. Questions and Answers Which of the following is incorrect about <property> element? a. Maps to attributes of the entity class b. Can be computed dynamically c. Column attribute is optional d. Name attribute can only start with a capital letter20 Hibernate Basic May 2012
  • 21. Questions and Answers Choose the correct option: Hibernate dialects are used in configuration to: a. Connect to database instance. b. Replace database driver c. Make Hibernate generate database specific queries.21 Hibernate Basic May 2012
  • 22. 22 Hibernate Basic May 2012