Your SlideShare is downloading. ×
0
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Intro To Hibernate
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Intro To Hibernate

4,559

Published on

Introduction to Hibernate 3

Introduction to Hibernate 3

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

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Introduction to Hibernate
  • 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. 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. 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. 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. 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. Hibernate Configuration... Contd <ul><li>Non managed environment </li></ul><ul><li>&lt;hibernate-configuration&gt; </li></ul><ul><li>&lt;session-factory&gt; </li></ul><ul><li>&lt;property name=&amp;quot;hibernate.connection.driver_class&amp;quot;&gt; COM.ibm.db2.jdbc.app.DB2Driver </li></ul><ul><li>&lt;/property&gt; </li></ul><ul><li>&lt;property name=&amp;quot;hibernate.connection.url&amp;quot;&gt;jdbc:db2:SAMPLE&lt;/property&gt; </li></ul><ul><li>&lt;property name=&amp;quot;hibernate.connection.username&amp;quot;&gt;db2admin&lt;/property&gt; </li></ul><ul><li>&lt;property name=&amp;quot;hibernate.connection.password&amp;quot;&gt;db2admin&lt;/property&gt; </li></ul><ul><li>&lt;property name=&amp;quot;hibernate.connection.pool_size&amp;quot;&gt;10&lt;/property&gt; </li></ul><ul><li>&lt;property name=&amp;quot;show_sql&amp;quot;&gt;true&lt;/property&gt; </li></ul><ul><li>&lt;property name=&amp;quot;dialect&amp;quot;&gt;net.sf.hibernate.dialect.DB2Dialect&lt;/property&gt; </li></ul><ul><li>&lt;!-- Mapping files --&gt; </li></ul><ul><li>&lt;mapping resource=&amp;quot;test_emp.hbm.xml&amp;quot;/&gt; </li></ul><ul><li>&lt;/session-factory&gt; </li></ul><ul><li>&lt;/hibernate-configuration&gt; </li></ul>
  • 8. Hibernate Configuration... Contd <ul><li>Managed environment (App Server) </li></ul><ul><li>&lt;hibernate-configuration&gt; </li></ul><ul><li>&lt;session-factory&gt; </li></ul><ul><li>&lt;property name=&amp;quot;hibernate.connection.datasource&amp;quot;&gt; </li></ul><ul><li>java:comp/env/jdbc/my_ds1 </li></ul><ul><li>&lt;/property&gt; </li></ul><ul><li>&lt;property name=&amp;quot;hibernate.transaction.factory_class&amp;quot;&gt; </li></ul><ul><li>org.hibernate.transaction.CMTTransactionFactory </li></ul><ul><li>&lt;/property&gt; </li></ul><ul><li>&lt;property name=&amp;quot;hibernate.transaction.manager_lookup_class&amp;quot;&gt; </li></ul><ul><li>org.hibernate.transaction.WebSphereExtendedJTATransactionLookup </li></ul><ul><li>&lt;/property&gt; </li></ul><ul><li> &lt;property name=&amp;quot;show_sql&amp;quot;&gt;true&lt;/property&gt; </li></ul><ul><li> &lt;property name=&amp;quot;dialect&amp;quot;&gt;org.hibernate.dialect.DB2Dialect&lt;/property&gt; </li></ul><ul><li>&lt;mapping resource=&amp;quot;emp.hbm.xml&amp;quot;/&gt; </li></ul><ul><li>&lt;mapping resource=&amp;quot;dept.hbm.xml&amp;quot;/&gt; </li></ul><ul><li>&lt;/session-factory&gt; </li></ul><ul><li>&lt;/hibernate-configuration&gt; </li></ul>
  • 9. Hibernate Mapping <ul><li>The hibernate-mapping xml file </li></ul><ul><li>&lt;hibernate-mapping&gt; </li></ul><ul><li>&lt;class name=&amp;quot;com.entity.Emp&amp;quot; table=&amp;quot;EMP&amp;quot;&gt; </li></ul><ul><li>&lt;id name=&amp;quot;empId&amp;quot; column=&amp;quot;EMP_ID&amp;quot; &gt; </li></ul><ul><li>&lt;generator class=&amp;quot;native&amp;quot;&gt;&lt;/generator&gt; </li></ul><ul><li>&lt;/id&gt; </li></ul><ul><li>&lt;property name=&amp;quot;empName&amp;quot; column=&amp;quot;EMP_NAME&amp;quot;&gt;&lt;/property&gt; </li></ul><ul><li>&lt;property name=&amp;quot;city&amp;quot; column=&amp;quot;CITY&amp;quot;&gt;&lt;/property&gt; </li></ul><ul><li>&lt;property name=&amp;quot;deptId&amp;quot; column=&amp;quot;DEPT_ID&amp;quot;&gt;&lt;/property&gt; </li></ul><ul><li>&lt;property name=&amp;quot;joinDate&amp;quot; column=&amp;quot;JOIN_DATE&amp;quot;&gt;&lt;/property&gt; </li></ul><ul><li>&lt;property name=“gender&amp;quot; column=“GENDER&amp;quot;&gt;&lt;/property&gt; </li></ul><ul><li>&lt;/class&gt; </li></ul><ul><li>&lt;/hibernate-mapping&gt; </li></ul>
  • 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. 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. 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&amp;quot;); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>e1.setCity(“Pune&amp;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(&amp;quot;20-Jul-1995&amp;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. Hibernate Architecture
  • 14. High Level View
  • 15. Understanding the Architecture
  • 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. Session Interface
  • 18. SessionFactory Interface
  • 19. Configuration Interface
  • 20. Transaction Interface
  • 21. Query and Criteria Interfaces
  • 22. Configuring logging in Hibernate
  • 23. Basic O/R Mapping
  • 24. Hibernate-Mapping
  • 25. Class Element
  • 26. ID element
  • 27. Composite ID Element
  • 28. Built in Types
  • 29. Mapping Collections
  • 30. Persistent Collections
  • 31. Emp model table relationship
  • 32. Mapping Set
  • 33. Lazy Initialization
  • 34. Component Mapping
  • 35. Dependent Object
  • 36. Sample table definition
  • 37. Employee-Address data model
  • 38. Example of component mapping
  • 39. Address class
  • 40. Mapping Component
  • 41. Mapping Associations
  • 42. Associations
  • 43. Many to one association <ul><li>&lt;class name=&amp;quot;com.entity.Emp&amp;quot; table=&amp;quot;EMP&amp;quot;&gt; </li></ul><ul><li>... </li></ul><ul><li>... </li></ul><ul><li>&lt;many-to-one name=&amp;quot;dept&amp;quot; </li></ul><ul><li> column=&amp;quot;DEPT_ID&amp;quot; </li></ul><ul><li> class=&amp;quot;Department&amp;quot; </li></ul><ul><li> not-null=&amp;quot;true&amp;quot; /&gt; </li></ul><ul><li>&lt;/class&gt; </li></ul>
  • 44. Parent child relationship <ul><li>&lt;class name=&amp;quot;com.entity.Dept&amp;quot; table=&amp;quot;DEPT&amp;quot;&gt; </li></ul><ul><li>... </li></ul><ul><li>... </li></ul><ul><li>&lt;set name=&amp;quot;employees&amp;quot; </li></ul><ul><li> inverse=&amp;quot;true&amp;quot; </li></ul><ul><li> cascade=&amp;quot;all-delete-orphan&amp;quot;&gt; </li></ul><ul><li> </li></ul><ul><li> &lt;key column=&amp;quot;DEPT_ID&amp;quot; /&gt; </li></ul><ul><li> &lt;one-to-many class=&amp;quot;Emp&amp;quot; /&gt; </li></ul><ul><li>&lt;/set&gt; </li></ul><ul><li>&lt;/class&gt; </li></ul>
  • 45. One to one relationship <ul><li>Employee-Person Object Dig </li></ul>
  • 46. One to one association <ul><li>&lt;class name=&amp;quot;com.entity.Emp&amp;quot; table=&amp;quot;EMP&amp;quot;&gt; </li></ul><ul><li>... </li></ul><ul><li>... </li></ul><ul><li>&lt;one-to-one name=&amp;quot;person&amp;quot; class=&amp;quot;Person&amp;quot; /&gt; </li></ul><ul><li>&lt;/class&gt; </li></ul>
  • 47. Many to many relationship
  • 48. Link table structure <ul><li>EMP_TASKS table </li></ul>
  • 49. Many to many association
  • 50. Manipulating Persistence Data
  • 51. Persistence Lifecycle
  • 52. Transient Objects
  • 53. Persistent Objects
  • 54. Detached Objects
  • 55. Persistence Manager
  • 56. Making an object persistent
  • 57. Retrieving persistent object
  • 58. Updating persistent object
  • 59. Committing a database transaction
  • 60. Transaction and Concurrency
  • 61. Understanding database transactions
  • 62. Hibernate Transaction API
  • 63. JDBC Transactions
  • 64. 2 Phase Transactions
  • 65. Hibernate Query Language HQL
  • 66. Introduction to HQL
  • 67. Query Interface
  • 68. Binding Parameters
  • 69. An example of simple Query
  • 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. Reporting Queries
  • 72. Criteria Queries
  • 73. Simple Criteria example
  • 74. Other query types supported <ul><li>Query By Example </li></ul><ul><li>Native SLQ query </li></ul>
  • 75. Cache
  • 76. Mass Update/Deletes
  • 77. Hibernate Cache Architecture
  • 78. Hibernate First Level Cache
  • 79. Hibernate Second Level Cache
  • 80. Caching Strategies
  • 81. Enabling Caching
  • 82. Any Questions ?
  • 83. Thanks !!!

×