Introduction to Hibernate
Agenda <ul><li>Introduction to Hibernate </li></ul><ul><li>Hibernate Architecture </li></ul><ul><li>Persistence Lifecycle ...
Plain JDBC <ul><li>Simple example to insert a row into database table, using JDBC </li></ul><ul><li>public void insertRow(...
Hibernate <ul><li>Persistence for JavaBean/POJO </li></ul><ul><li>Support for fine-grained, richly typed object models </l...
Hibernate Configuration <ul><li>The hibernate.properties file </li></ul><ul><ul><li>Used for hibernate configuration </li>...
Hibernate Configuration... Contd <ul><li>The hibernate.cfg.xml </li></ul><ul><ul><li>Alternative approach of configuration...
Hibernate Configuration... Contd <ul><li>Non managed environment </li></ul><ul><li><hibernate-configuration> </li></ul><ul...
Hibernate Configuration... Contd <ul><li>Managed environment (App Server) </li></ul><ul><li><hibernate-configuration> </li...
Hibernate Mapping <ul><li>The hibernate-mapping xml file </li></ul><ul><li><hibernate-mapping> </li></ul><ul><li><class na...
Simple standalone hibernate appliation <ul><li>Requirements </li></ul><ul><ul><li>Hibernate libraries </li></ul></ul><ul><...
Sample POJO <ul><li>public class Emp implements SplEntity { </li></ul><ul><li>private Integer empId; </li></ul><ul><li>pri...
Sample Main class <ul><li>Public class Main { </li></ul><ul><li>public static void main(String[] args) { </li></ul><ul><li...
Hibernate Architecture
High Level View
Understanding the Architecture
Hibernate core interfaces <ul><li>Session </li></ul><ul><li>SessionFactory </li></ul><ul><li>Configuration </li></ul><ul><...
Session Interface
SessionFactory Interface
Configuration Interface
Transaction Interface
Query and Criteria Interfaces
Configuring logging in Hibernate
Basic O/R Mapping
Hibernate-Mapping
Class Element
ID element
Composite ID Element
Built in Types
Mapping Collections
Persistent Collections
Emp model table relationship
Mapping Set
Lazy Initialization
Component Mapping
Dependent Object
Sample table definition
Employee-Address data model
Example of component mapping
Address class
Mapping Component
Mapping Associations
Associations
Many to one association <ul><li><class name=&quot;com.entity.Emp&quot; table=&quot;EMP&quot;> </li></ul><ul><li>... </li><...
Parent child relationship <ul><li><class name=&quot;com.entity.Dept&quot; table=&quot;DEPT&quot;> </li></ul><ul><li>... </...
One to one relationship <ul><li>Employee-Person Object Dig </li></ul>
One to one association <ul><li><class name=&quot;com.entity.Emp&quot; table=&quot;EMP&quot;> </li></ul><ul><li>... </li></...
Many to many relationship
Link table structure <ul><li>EMP_TASKS table </li></ul>
Many to many association
Manipulating Persistence Data
Persistence Lifecycle
Transient Objects
Persistent Objects
Detached Objects
Persistence Manager
Making an object persistent
Retrieving persistent object
Updating persistent object
Committing a database transaction
Transaction and Concurrency
Understanding database transactions
Hibernate Transaction API
JDBC Transactions
2 Phase Transactions
Hibernate Query Language HQL
Introduction to HQL
Query Interface
Binding Parameters
An example of simple Query
HQL supports: <ul><li>WHERE clause </li></ul><ul><li>ORDER BY clause </li></ul><ul><li>GROUP BY clause </li></ul><ul><li>A...
Reporting Queries
Criteria Queries
Simple Criteria example
Other query types supported <ul><li>Query By Example </li></ul><ul><li>Native SLQ query </li></ul>
Cache
Mass Update/Deletes
Hibernate Cache Architecture
Hibernate First Level Cache
Hibernate Second Level Cache
Caching Strategies
Enabling Caching
Any Questions ?
Thanks !!!
Upcoming SlideShare
Loading in...5
×

Intro To Hibernate

4,633

Published on

Introduction to Hibernate 3

Published in: Technology, Education
1 Comment
2 Likes
Statistics
Notes
No Downloads
Views
Total Views
4,633
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
599
Comments
1
Likes
2
Embeds 0
No embeds

No notes for slide

Intro To Hibernate

  1. 1. Introduction to Hibernate
  2. 2. Agenda <ul><li>Introduction to Hibernate </li></ul><ul><li>Hibernate Architecture </li></ul><ul><li>Persistence Lifecycle </li></ul><ul><li>Object Identity </li></ul><ul><li>Mapping tables to beans </li></ul><ul><li>Mapping properties to columns </li></ul><ul><li>Relationships </li></ul><ul><li>Setting up a one to many relationships </li></ul><ul><li>Setting up a many to many relationships </li></ul><ul><li>Hibernate Transaction API </li></ul><ul><li>Basic Queries </li></ul><ul><li>Working with Queries </li></ul><ul><li>Named Queries </li></ul><ul><li>working with the query API </li></ul><ul><li>Working with binding parameters </li></ul>
  3. 3. Plain JDBC <ul><li>Simple example to insert a row into database table, using JDBC </li></ul><ul><li>public void insertRow(Employee emp) { </li></ul><ul><li>String insertSQL = “INSERT INTO emp values(….); </li></ul><ul><li>Connection conn = getConnectionFromPool(); </li></ul><ul><li>Statement stmt = conn.createStatement(insertSQL); </li></ul><ul><li>stmt.execute(); </li></ul><ul><li>} </li></ul>
  4. 4. Hibernate <ul><li>Persistence for JavaBean/POJO </li></ul><ul><li>Support for fine-grained, richly typed object models </li></ul><ul><li>Powerful queries </li></ul><ul><li>Support for detached persistence objects </li></ul><ul><li>Mapping meta data in XML file </li></ul>
  5. 5. Hibernate Configuration <ul><li>The hibernate.properties file </li></ul><ul><ul><li>Used for hibernate configuration </li></ul></ul><ul><ul><li>Contains </li></ul></ul><ul><ul><ul><li>Database configuration </li></ul></ul></ul><ul><ul><ul><li>Datasource configuration </li></ul></ul></ul><ul><ul><ul><li>Transaction configuration </li></ul></ul></ul><ul><ul><ul><li>Caching configuration </li></ul></ul></ul><ul><ul><ul><li>Connection pool configuration </li></ul></ul></ul><ul><ul><ul><li>Other settings </li></ul></ul></ul>
  6. 6. Hibernate Configuration... Contd <ul><li>The hibernate.cfg.xml </li></ul><ul><ul><li>Alternative approach of configuration </li></ul></ul><ul><ul><li>Can be used as replacement of hibernate.properties </li></ul></ul><ul><ul><li>Picked up from classpath </li></ul></ul><ul><ul><li>Has got precedence on hibernate.properties file </li></ul></ul>
  7. 7. Hibernate Configuration... Contd <ul><li>Non managed environment </li></ul><ul><li><hibernate-configuration> </li></ul><ul><li><session-factory> </li></ul><ul><li><property name=&quot;hibernate.connection.driver_class&quot;> COM.ibm.db2.jdbc.app.DB2Driver </li></ul><ul><li></property> </li></ul><ul><li><property name=&quot;hibernate.connection.url&quot;>jdbc:db2:SAMPLE</property> </li></ul><ul><li><property name=&quot;hibernate.connection.username&quot;>db2admin</property> </li></ul><ul><li><property name=&quot;hibernate.connection.password&quot;>db2admin</property> </li></ul><ul><li><property name=&quot;hibernate.connection.pool_size&quot;>10</property> </li></ul><ul><li><property name=&quot;show_sql&quot;>true</property> </li></ul><ul><li><property name=&quot;dialect&quot;>net.sf.hibernate.dialect.DB2Dialect</property> </li></ul><ul><li><!-- Mapping files --> </li></ul><ul><li><mapping resource=&quot;test_emp.hbm.xml&quot;/> </li></ul><ul><li></session-factory> </li></ul><ul><li></hibernate-configuration> </li></ul>
  8. 8. Hibernate Configuration... Contd <ul><li>Managed environment (App Server) </li></ul><ul><li><hibernate-configuration> </li></ul><ul><li><session-factory> </li></ul><ul><li><property name=&quot;hibernate.connection.datasource&quot;> </li></ul><ul><li>java:comp/env/jdbc/my_ds1 </li></ul><ul><li></property> </li></ul><ul><li><property name=&quot;hibernate.transaction.factory_class&quot;> </li></ul><ul><li>org.hibernate.transaction.CMTTransactionFactory </li></ul><ul><li></property> </li></ul><ul><li><property name=&quot;hibernate.transaction.manager_lookup_class&quot;> </li></ul><ul><li>org.hibernate.transaction.WebSphereExtendedJTATransactionLookup </li></ul><ul><li></property> </li></ul><ul><li> <property name=&quot;show_sql&quot;>true</property> </li></ul><ul><li> <property name=&quot;dialect&quot;>org.hibernate.dialect.DB2Dialect</property> </li></ul><ul><li><mapping resource=&quot;emp.hbm.xml&quot;/> </li></ul><ul><li><mapping resource=&quot;dept.hbm.xml&quot;/> </li></ul><ul><li></session-factory> </li></ul><ul><li></hibernate-configuration> </li></ul>
  9. 9. Hibernate Mapping <ul><li>The hibernate-mapping xml file </li></ul><ul><li><hibernate-mapping> </li></ul><ul><li><class name=&quot;com.entity.Emp&quot; table=&quot;EMP&quot;> </li></ul><ul><li><id name=&quot;empId&quot; column=&quot;EMP_ID&quot; > </li></ul><ul><li><generator class=&quot;native&quot;></generator> </li></ul><ul><li></id> </li></ul><ul><li><property name=&quot;empName&quot; column=&quot;EMP_NAME&quot;></property> </li></ul><ul><li><property name=&quot;city&quot; column=&quot;CITY&quot;></property> </li></ul><ul><li><property name=&quot;deptId&quot; column=&quot;DEPT_ID&quot;></property> </li></ul><ul><li><property name=&quot;joinDate&quot; column=&quot;JOIN_DATE&quot;></property> </li></ul><ul><li><property name=“gender&quot; column=“GENDER&quot;></property> </li></ul><ul><li></class> </li></ul><ul><li></hibernate-mapping> </li></ul>
  10. 10. Simple standalone hibernate appliation <ul><li>Requirements </li></ul><ul><ul><li>Hibernate libraries </li></ul></ul><ul><ul><li>Hibernate configuration file </li></ul></ul><ul><ul><li>A POJO class </li></ul></ul><ul><ul><li>A hibernate-mapping file </li></ul></ul><ul><ul><li>A Main class </li></ul></ul>
  11. 11. Sample POJO <ul><li>public class Emp implements SplEntity { </li></ul><ul><li>private Integer empId; </li></ul><ul><li>private String empName; </li></ul><ul><li>private String city; </li></ul><ul><li>private int deptId; </li></ul><ul><li>private Date joinDate; </li></ul><ul><li>private char gender; </li></ul><ul><li>public String getCity() { </li></ul><ul><li>return city; </li></ul><ul><li>} </li></ul><ul><li>public void setCity(String city) { </li></ul><ul><li>this.city = city; </li></ul><ul><li>} </li></ul><ul><li>public int getDeptId() { </li></ul><ul><li>return deptId; </li></ul><ul><li>} </li></ul><ul><li>public void setDeptId(int deptId) { </li></ul><ul><li>this.deptId = deptId; </li></ul><ul><li>} </li></ul><ul><li>public Integer getEmpId() { </li></ul><ul><li>return empId; </li></ul><ul><li>} </li></ul><ul><li>public void setEmpId(Integer empId) { </li></ul><ul><li>this.empId = empId; </li></ul><ul><li>} </li></ul><ul><li>public String getEmpName() { </li></ul><ul><li>return empName; </li></ul><ul><li>} </li></ul><ul><li>public void setEmpName(String empName) { </li></ul><ul><li>this.empName = empName; </li></ul><ul><li>} </li></ul><ul><li>public Date getJoinDate() { </li></ul><ul><li>return joinDate; </li></ul><ul><li>} </li></ul><ul><li>public void setJoinDate(Date joinDate) { </li></ul><ul><li>this.joinDate = joinDate; </li></ul><ul><li>} </li></ul><ul><li>public char getGender() { </li></ul><ul><li>return gender; </li></ul><ul><li>} </li></ul><ul><li>public void setGender(char gender) { </li></ul><ul><li>this.gender = gender; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  12. 12. Sample Main class <ul><li>Public class Main { </li></ul><ul><li>public static void main(String[] args) { </li></ul><ul><li> try { </li></ul><ul><li> SessionFactory factory = null; </li></ul><ul><li> factory = new Configuration().configure().buildSessionFactory(); </li></ul><ul><li>Session session = factory.openSession(); </li></ul><ul><li>Transaction tx = session.beginTransaction(); </li></ul><ul><ul><ul><ul><li>Emp e1 = new Emp(); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>e1.setEmpName(“Rajesh B&quot;); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>e1.setCity(“Pune&quot;); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>e1.setDeptId(3); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>e1.setJoinDate(new Date(&quot;20-Jul-1995&quot;)); </li></ul></ul></ul></ul><ul><li>session.save(e1); </li></ul><ul><li>tx.commit(); </li></ul><ul><li> } catch (Exception e) { </li></ul><ul><li>e.printStackTrace(); </li></ul><ul><li> } </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  13. 13. Hibernate Architecture
  14. 14. High Level View
  15. 15. Understanding the Architecture
  16. 16. Hibernate core interfaces <ul><li>Session </li></ul><ul><li>SessionFactory </li></ul><ul><li>Configuration </li></ul><ul><li>Transaction </li></ul><ul><li>Query </li></ul><ul><li>Criteria </li></ul><ul><li>Types </li></ul>
  17. 17. Session Interface
  18. 18. SessionFactory Interface
  19. 19. Configuration Interface
  20. 20. Transaction Interface
  21. 21. Query and Criteria Interfaces
  22. 22. Configuring logging in Hibernate
  23. 23. Basic O/R Mapping
  24. 24. Hibernate-Mapping
  25. 25. Class Element
  26. 26. ID element
  27. 27. Composite ID Element
  28. 28. Built in Types
  29. 29. Mapping Collections
  30. 30. Persistent Collections
  31. 31. Emp model table relationship
  32. 32. Mapping Set
  33. 33. Lazy Initialization
  34. 34. Component Mapping
  35. 35. Dependent Object
  36. 36. Sample table definition
  37. 37. Employee-Address data model
  38. 38. Example of component mapping
  39. 39. Address class
  40. 40. Mapping Component
  41. 41. Mapping Associations
  42. 42. Associations
  43. 43. Many to one association <ul><li><class name=&quot;com.entity.Emp&quot; table=&quot;EMP&quot;> </li></ul><ul><li>... </li></ul><ul><li>... </li></ul><ul><li><many-to-one name=&quot;dept&quot; </li></ul><ul><li> column=&quot;DEPT_ID&quot; </li></ul><ul><li> class=&quot;Department&quot; </li></ul><ul><li> not-null=&quot;true&quot; /> </li></ul><ul><li></class> </li></ul>
  44. 44. Parent child relationship <ul><li><class name=&quot;com.entity.Dept&quot; table=&quot;DEPT&quot;> </li></ul><ul><li>... </li></ul><ul><li>... </li></ul><ul><li><set name=&quot;employees&quot; </li></ul><ul><li> inverse=&quot;true&quot; </li></ul><ul><li> cascade=&quot;all-delete-orphan&quot;> </li></ul><ul><li> </li></ul><ul><li> <key column=&quot;DEPT_ID&quot; /> </li></ul><ul><li> <one-to-many class=&quot;Emp&quot; /> </li></ul><ul><li></set> </li></ul><ul><li></class> </li></ul>
  45. 45. One to one relationship <ul><li>Employee-Person Object Dig </li></ul>
  46. 46. One to one association <ul><li><class name=&quot;com.entity.Emp&quot; table=&quot;EMP&quot;> </li></ul><ul><li>... </li></ul><ul><li>... </li></ul><ul><li><one-to-one name=&quot;person&quot; class=&quot;Person&quot; /> </li></ul><ul><li></class> </li></ul>
  47. 47. Many to many relationship
  48. 48. Link table structure <ul><li>EMP_TASKS table </li></ul>
  49. 49. Many to many association
  50. 50. Manipulating Persistence Data
  51. 51. Persistence Lifecycle
  52. 52. Transient Objects
  53. 53. Persistent Objects
  54. 54. Detached Objects
  55. 55. Persistence Manager
  56. 56. Making an object persistent
  57. 57. Retrieving persistent object
  58. 58. Updating persistent object
  59. 59. Committing a database transaction
  60. 60. Transaction and Concurrency
  61. 61. Understanding database transactions
  62. 62. Hibernate Transaction API
  63. 63. JDBC Transactions
  64. 64. 2 Phase Transactions
  65. 65. Hibernate Query Language HQL
  66. 66. Introduction to HQL
  67. 67. Query Interface
  68. 68. Binding Parameters
  69. 69. An example of simple Query
  70. 70. HQL supports: <ul><li>WHERE clause </li></ul><ul><li>ORDER BY clause </li></ul><ul><li>GROUP BY clause </li></ul><ul><li>All types of joins (inner, left outer, right outer, outer) </li></ul><ul><li>Subquery </li></ul><ul><li>etc </li></ul>
  71. 71. Reporting Queries
  72. 72. Criteria Queries
  73. 73. Simple Criteria example
  74. 74. Other query types supported <ul><li>Query By Example </li></ul><ul><li>Native SLQ query </li></ul>
  75. 75. Cache
  76. 76. Mass Update/Deletes
  77. 77. Hibernate Cache Architecture
  78. 78. Hibernate First Level Cache
  79. 79. Hibernate Second Level Cache
  80. 80. Caching Strategies
  81. 81. Enabling Caching
  82. 82. Any Questions ?
  83. 83. Thanks !!!
  1. Gostou de algum slide específico?

    Recortar slides é uma maneira fácil de colecionar informações para acessar mais tarde.

×