HIBERNATE - Relational Persistence for Idiomatic Java Hibernate Reference
Intro To Hibernate
-
Upload
amit-himani -
Category
Technology
-
view
4.516 -
download
0
description
Transcript of Intro To Hibernate
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="hibernate.connection.driver_class"> COM.ibm.db2.jdbc.app.DB2Driver</property><property name="hibernate.connection.url">jdbc:db2:SAMPLE</property><property name="hibernate.connection.username">db2admin</property><property name="hibernate.connection.password">db2admin</property><property name="hibernate.connection.pool_size">10</property><property name="show_sql">true</property><property name="dialect">net.sf.hibernate.dialect.DB2Dialect</property><!-- Mapping files --><mapping resource="test_emp.hbm.xml"/>
</session-factory></hibernate-configuration>
Hibernate Configuration... Contd Managed environment (App Server)
<hibernate-configuration><session-factory>
<property name="hibernate.connection.datasource">java:comp/env/jdbc/my_ds1
</property><property name="hibernate.transaction.factory_class">
org.hibernate.transaction.CMTTransactionFactory</property><property name="hibernate.transaction.manager_lookup_class">
org.hibernate.transaction.WebSphereExtendedJTATransactionLookup</property>
<property name="show_sql">true</property> <property name="dialect">org.hibernate.dialect.DB2Dialect</property>
<mapping resource="emp.hbm.xml"/> <mapping resource="dept.hbm.xml"/>
</session-factory></hibernate-configuration>
Hibernate Mapping The hibernate-mapping xml file
<hibernate-mapping> <class name="com.entity.Emp" table="EMP"> <id name="empId" column="EMP_ID" > <generator class="native"></generator> </id> <property name="empName" column="EMP_NAME"></property> <property name="city" column="CITY"></property> <property name="deptId" column="DEPT_ID"></property> <property name="joinDate" column="JOIN_DATE"></property> <property name=“gender" column=“GENDER"></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 classPublic 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");e1.setCity(“Pune");e1.setDeptId(3);e1.setJoinDate(new Date("20-Jul-1995"));
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="com.entity.Emp" table="EMP">
...
...
<many-to-one name="dept"
column="DEPT_ID"
class="Department"
not-null="true" />
</class>
Parent child relationship
<class name="com.entity.Dept" table="DEPT">......<set name="employees" inverse="true" cascade="all-delete-orphan"> <key column="DEPT_ID" /> <one-to-many class="Emp" />
</set></class>
One to one relationship
Employee-Person Object Dig
One to one association
<class name="com.entity.Emp" table="EMP">
...
...
<one-to-one name="person" class="Person" />
</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 LanguageHQL
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 !!!