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

Intro To Hibernate

on

  • 5,277 views

Introduction to Hibernate 3

Introduction to Hibernate 3

Statistics

Views

Total Views
5,277
Views on SlideShare
5,274
Embed Views
3

Actions

Likes
2
Downloads
521
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 Intro To Hibernate Presentation Transcript

  • Introduction to Hibernate
  • 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
  • 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();
    • }
  • 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
  • Hibernate Configuration
    • The hibernate.properties file
      • Used for hibernate configuration
      • Contains
        • Database configuration
        • Datasource configuration
        • Transaction configuration
        • Caching configuration
        • Connection pool configuration
        • Other settings
  • 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
  • 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>
  • 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>
  • 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>
  • Simple standalone hibernate appliation
    • Requirements
      • Hibernate libraries
      • Hibernate configuration file
      • A POJO class
      • A hibernate-mapping file
      • A Main class
  • 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;
    • }
    • }
  • 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();
    • }
    • }
    • }
  • Hibernate Architecture
  • High Level View
  • Understanding the Architecture
  • Hibernate core interfaces
    • Session
    • SessionFactory
    • Configuration
    • Transaction
    • Query
    • Criteria
    • Types
  • 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
    • <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>
  • 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>
  • One to one relationship
    • Employee-Person Object Dig
  • 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>
  • Many to many relationship
  • Link table structure
    • EMP_TASKS table
  • 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:
    • WHERE clause
    • ORDER BY clause
    • GROUP BY clause
    • All types of joins (inner, left outer, right outer, outer)
    • Subquery
    • etc
  • Reporting Queries
  • Criteria Queries
  • Simple Criteria example
  • Other query types supported
    • Query By Example
    • Native SLQ query
  • Cache
  • Mass Update/Deletes
  • Hibernate Cache Architecture
  • Hibernate First Level Cache
  • Hibernate Second Level Cache
  • Caching Strategies
  • Enabling Caching
  • Any Questions ?
  • Thanks !!!