Intro To Hibernate
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Intro To Hibernate

on

  • 5,475 views

Introduction to Hibernate 3

Introduction to Hibernate 3

Statistics

Views

Total Views
5,475
Views on SlideShare
5,472
Embed Views
3

Actions

Likes
2
Downloads
535
Comments
1

2 Embeds 3

http://www.slideshare.net 2
http://www.health.medicbd.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • nice job ,,,,,
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Intro To Hibernate Presentation Transcript

  • 1. Introduction to Hibernate
  • 2. Agenda
    • Introduction to Hibernate
    • Hibernate Architecture
    • Persistence Lifecycle
    • Object Identity
    • Mapping tables to beans
    • Mapping properties to columns
    • Relationships
    • Setting up a one to many relationships
    • Setting up a many to many relationships
    • Hibernate Transaction API
    • Basic Queries
    • Working with Queries
    • Named Queries
    • working with the query API
    • Working with binding parameters
  • 3. Plain JDBC
    • Simple example to insert a row into database table, using JDBC
    • public void insertRow(Employee emp) {
    • String insertSQL = “INSERT INTO emp values(….);
    • Connection conn = getConnectionFromPool();
    • Statement stmt = conn.createStatement(insertSQL);
    • stmt.execute();
    • }
  • 4. Hibernate
    • Persistence for JavaBean/POJO
    • Support for fine-grained, richly typed object models
    • Powerful queries
    • Support for detached persistence objects
    • Mapping meta data in XML file
  • 5. Hibernate Configuration
    • The hibernate.properties file
      • Used for hibernate configuration
      • Contains
        • Database configuration
        • Datasource configuration
        • Transaction configuration
        • Caching configuration
        • Connection pool configuration
        • Other settings
  • 6. Hibernate Configuration... Contd
    • The hibernate.cfg.xml
      • Alternative approach of configuration
      • Can be used as replacement of hibernate.properties
      • Picked up from classpath
      • Has got precedence on hibernate.properties file
  • 7. Hibernate Configuration... Contd
    • Non managed environment
    • <hibernate-configuration>
    • <session-factory>
    • <property name=&quot;hibernate.connection.driver_class&quot;> COM.ibm.db2.jdbc.app.DB2Driver
    • </property>
    • <property name=&quot;hibernate.connection.url&quot;>jdbc:db2:SAMPLE</property>
    • <property name=&quot;hibernate.connection.username&quot;>db2admin</property>
    • <property name=&quot;hibernate.connection.password&quot;>db2admin</property>
    • <property name=&quot;hibernate.connection.pool_size&quot;>10</property>
    • <property name=&quot;show_sql&quot;>true</property>
    • <property name=&quot;dialect&quot;>net.sf.hibernate.dialect.DB2Dialect</property>
    • <!-- Mapping files -->
    • <mapping resource=&quot;test_emp.hbm.xml&quot;/>
    • </session-factory>
    • </hibernate-configuration>
  • 8. Hibernate Configuration... Contd
    • Managed environment (App Server)
    • <hibernate-configuration>
    • <session-factory>
    • <property name=&quot;hibernate.connection.datasource&quot;>
    • java:comp/env/jdbc/my_ds1
    • </property>
    • <property name=&quot;hibernate.transaction.factory_class&quot;>
    • org.hibernate.transaction.CMTTransactionFactory
    • </property>
    • <property name=&quot;hibernate.transaction.manager_lookup_class&quot;>
    • org.hibernate.transaction.WebSphereExtendedJTATransactionLookup
    • </property>
    • <property name=&quot;show_sql&quot;>true</property>
    • <property name=&quot;dialect&quot;>org.hibernate.dialect.DB2Dialect</property>
    • <mapping resource=&quot;emp.hbm.xml&quot;/>
    • <mapping resource=&quot;dept.hbm.xml&quot;/>
    • </session-factory>
    • </hibernate-configuration>
  • 9. Hibernate Mapping
    • The hibernate-mapping xml file
    • <hibernate-mapping>
    • <class name=&quot;com.entity.Emp&quot; table=&quot;EMP&quot;>
    • <id name=&quot;empId&quot; column=&quot;EMP_ID&quot; >
    • <generator class=&quot;native&quot;></generator>
    • </id>
    • <property name=&quot;empName&quot; column=&quot;EMP_NAME&quot;></property>
    • <property name=&quot;city&quot; column=&quot;CITY&quot;></property>
    • <property name=&quot;deptId&quot; column=&quot;DEPT_ID&quot;></property>
    • <property name=&quot;joinDate&quot; column=&quot;JOIN_DATE&quot;></property>
    • <property name=“gender&quot; column=“GENDER&quot;></property>
    • </class>
    • </hibernate-mapping>
  • 10. Simple standalone hibernate appliation
    • Requirements
      • Hibernate libraries
      • Hibernate configuration file
      • A POJO class
      • A hibernate-mapping file
      • A Main class
  • 11. Sample POJO
    • public class Emp implements SplEntity {
    • private Integer empId;
    • private String empName;
    • private String city;
    • private int deptId;
    • private Date joinDate;
    • private char gender;
    • public String getCity() {
    • return city;
    • }
    • public void setCity(String city) {
    • this.city = city;
    • }
    • public int getDeptId() {
    • return deptId;
    • }
    • public void setDeptId(int deptId) {
    • this.deptId = deptId;
    • }
    • public Integer getEmpId() {
    • return empId;
    • }
    • public void setEmpId(Integer empId) {
    • this.empId = empId;
    • }
    • public String getEmpName() {
    • return empName;
    • }
    • public void setEmpName(String empName) {
    • this.empName = empName;
    • }
    • public Date getJoinDate() {
    • return joinDate;
    • }
    • public void setJoinDate(Date joinDate) {
    • this.joinDate = joinDate;
    • }
    • public char getGender() {
    • return gender;
    • }
    • public void setGender(char gender) {
    • this.gender = gender;
    • }
    • }
  • 12. Sample Main class
    • Public class Main {
    • public static void main(String[] args) {
    • try {
    • SessionFactory factory = null;
    • factory = new Configuration().configure().buildSessionFactory();
    • Session session = factory.openSession();
    • Transaction tx = session.beginTransaction();
          • Emp e1 = new Emp();
          • e1.setEmpName(“Rajesh B&quot;);
          • e1.setCity(“Pune&quot;);
          • e1.setDeptId(3);
          • e1.setJoinDate(new Date(&quot;20-Jul-1995&quot;));
    • session.save(e1);
    • tx.commit();
    • } catch (Exception e) {
    • e.printStackTrace();
    • }
    • }
    • }
  • 13. Hibernate Architecture
  • 14. High Level View
  • 15. Understanding the Architecture
  • 16. Hibernate core interfaces
    • Session
    • SessionFactory
    • Configuration
    • Transaction
    • Query
    • Criteria
    • Types
  • 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
    • <class name=&quot;com.entity.Emp&quot; table=&quot;EMP&quot;>
    • ...
    • ...
    • <many-to-one name=&quot;dept&quot;
    • column=&quot;DEPT_ID&quot;
    • class=&quot;Department&quot;
    • not-null=&quot;true&quot; />
    • </class>
  • 44. Parent child relationship
    • <class name=&quot;com.entity.Dept&quot; table=&quot;DEPT&quot;>
    • ...
    • ...
    • <set name=&quot;employees&quot;
    • inverse=&quot;true&quot;
    • cascade=&quot;all-delete-orphan&quot;>
    • <key column=&quot;DEPT_ID&quot; />
    • <one-to-many class=&quot;Emp&quot; />
    • </set>
    • </class>
  • 45. One to one relationship
    • Employee-Person Object Dig
  • 46. One to one association
    • <class name=&quot;com.entity.Emp&quot; table=&quot;EMP&quot;>
    • ...
    • ...
    • <one-to-one name=&quot;person&quot; class=&quot;Person&quot; />
    • </class>
  • 47. Many to many relationship
  • 48. Link table structure
    • EMP_TASKS table
  • 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:
    • WHERE clause
    • ORDER BY clause
    • GROUP BY clause
    • All types of joins (inner, left outer, right outer, outer)
    • Subquery
    • etc
  • 71. Reporting Queries
  • 72. Criteria Queries
  • 73. Simple Criteria example
  • 74. Other query types supported
    • Query By Example
    • Native SLQ query
  • 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 !!!