Hibernate     Introduction and Overview          >By Manisha                       May 2012
Agenda    At the end of this course, you should be able to:     Describe the basics of Hibernate     Use Hibernate confi...
Hibernate    ●        ORM technology     Consistent database communication and Increased maintainability: In      Hiberna...
What is ORM?      Object Relational Mapping (ORM) frameworks are those which       encapsulate SQL communication with rel...
Difference in conventional and ORM based implementation     Conventional Implementation          Service                  ...
High level Hibernate    Architecture                                        Application                                   ...
Basic content of Hibernate application    In any standard hibernate application will have below data     Pojo class of ta...
How to use Hibernate for data communication?    Follow these steps to use Hibernate for data communication:     Prepare d...
Hibernate Example: Prepare pojo object(Class to table mapping)    package com.ibm.tf.hibernate;    public class Employee i...
Write mapping information(Table mapping to conf. Xml file) <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hi...
Write Hibernate configuration information Hibernate.cfg.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-con...
Write Hibernate configuration informationThe following are illustrative (not exhaustive) configuration parameters sorted b...
Create Hibernate runtime     Before creating hibernate runtime, lets understand It      Hibernate Session, Transaction, a...
Create hibernate runtime     public class HibernateUtil {     private static final SessionFactory sessionFactory;     stat...
Use Hibernate Runtime     public class EemployeetManager {     private void createAndStoreEmployee(String firstNm, String ...
Example artifacts in Hibernate Architecture       EmployeeManager.java     Application   HibernateUtil.java               ...
Demo !!!17   Hibernate Basic              May 2012
Questions and Answers     Choose the correct option:     ORM framework is the one which:     a. Abstracts communication wi...
Questions and Answers     Choose the correct option:     We can configure JNDI name for session factory to which       Hib...
Questions and Answers     Which of the following is incorrect about <property> element?     a. Maps to attributes of the e...
Questions and Answers     Choose the correct option:     Hibernate dialects are used in configuration to:     a. Connect t...
22   Hibernate Basic   May 2012
Upcoming SlideShare
Loading in …5
×

Hibernate 18052012

851 views

Published on

basic hibernate

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
851
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Hibernate 18052012

  1. 1. Hibernate Introduction and Overview >By Manisha May 2012
  2. 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. 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. 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. 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. 6. High level Hibernate Architecture Application Persistent Objects Hibernate Configuration Mapping Database6 Hibernate Basic May 2012
  7. 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. 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. 9. Hibernate Example: Prepare pojo object(Class to table mapping) package com.ibm.tf.hibernate; 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. 10. Write mapping information(Table mapping to conf. Xml file) <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping> <!– Employee class mapping to EMPLOYEE table--> <class name=" com.ibm.tf.hibernate.Employee“ 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. 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" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <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="hbm2ddl.auto">update</property> <mapping resource="com/hibernate/test/Employee.hbm.xml"/> </session-factory> </hibernate-configuration>11 Hibernate Basic May 2012
  12. 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. 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. 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. 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.save(theEmployee); 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. 16. Example artifacts in Hibernate Architecture EmployeeManager.java Application HibernateUtil.java Persistent Objects Employee.java Configuration Hibernate Mapping Hibernate.cfg.xml Employee.hbm.xm l Database jdbc:hsql:file:testdb16 Hibernate Basic May 2012
  17. 17. Demo !!!17 Hibernate Basic May 2012
  18. 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. 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. 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. 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. 22 Hibernate Basic May 2012

×