Introduction to hibernate

2,483 views

Published on

this document provides the basic information about hibernate. it's very useful while beginning with hibernate

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,483
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
5
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Introduction to hibernate

  1. 1. An Introduction to Hibernate
  2. 2. Object-Relational Mapping TechniqueHibernate provides mapping between: Java Classes and the database tables. The Java data types and SQL data types.
  3. 3. Basic Architecture
  4. 4. Hibernate Configuration File<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3307/libms</property> <property name="hibernate.connection.username">root</property><!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property><!-- Enable Hibernates automatic session context management --> <property name="current_session_context_class">thread</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Echo all executed SQL to stouts --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">update</property><mapping resource="com/myapp/struts/hbm/Staff.hbm.xml"/><mapping resource="com/myapp/struts/hbm/Department.hbm.xml"/></session-factory></hibernate-configuration>
  5. 5. Hibernate Mapping File <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//Field Name Data Type EN" "http://www.hibernate.org/dtd/hibernate-Staff_id VARCHAR(1 mapping-3.0.dtd"> <hibernate-mapping> 0) <class name="com.myapp.struts.beans.Staff" table=“Staff" catalog="libms">Staff_name VARCHAR(3 <id name="StaffId" type="string"> 0) <column name=“Staff_id" length=“10" /> <generator class="assigned" />Staff_Depart VARCHAR(3 </id> <property name=“StaffName"ment 0) type="string"> <column name=“Staff_name" length="30" not-null="true" /> </property> <property name=“StaffDep" type="string"> <column name=“Staff_Department" length=“30" /> </class>
  6. 6. public class Employee implements java.io.Serializable { private String StaffId; private String StaffName;POJO private String StaffDep; public Staff() {<?xml version="1.0"?> } public Staff(String id, String name) {<!DOCTYPE hibernate-mapping PUBLIC this. StaffId = id; "-//Hibernate/Hibernate Mapping DTD this. StaffName = name;3.0//EN" } public Staff(String id, String name, String dep) { "http://www.hibernate.org/dtd/hibernate- this.id = id;mapping-3.0.dtd"> this.name = name;<hibernate-mapping> this. StaffDep = dep; <class name="com.myapp.struts.beans.Staff" }table=“Staff" catalog="libms"> public String getStaffId() { <id name="StaffId" type="string"> return this. StaffId; <column name=“Staff_id" length=“10" /> } <generator class="assigned" /> public void setStaffId(String id) { </id> this. StaffId = id; } <property name=“StaffName" type="string"> public String StaffName() { <column name=“Staff_name" length="30" return this. StaffName;not-null="true" /> } public void set StaffName(String name) { </property> this. StaffName = name; <property name=“StaffDep" type="string"> } <column name=“Staff_Department" public String get StaffDep() { return this. StaffDep;length=“30" /> }</class> public void set StaffDep(String department) {</hibernate-mapping> this. StaffDep = department; }
  7. 7. Basic APIs SessionFactory (org.hibernate.SessionFactory) Session (org.hibernate.Session) Persistent objects and collections Transient and detached objects and collections Transaction (org.hibernate.Transaction) ConnectionProvider (org.hibernate.connection.ConnectionProvider) TransactionFactory (org.hibernate.TransactionFactory)
  8. 8. SessionFactory (org.hibernate.SessionFactory) A SessionFactory is an immutable, thread-safe object, intended to be shared by all application threads. It is created once, usually on application startup, from a Configuration instance. A org.hibernate.SessionFactory is used to obtain org.hibernate.Session instances. HibernateUtil helper class that takes care of startup and makes accessing the org.hibernate.SessionFactory more convenient.
  9. 9. HibernateUtil classimport org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() { try { // Create the SessionFactory from hibernate.cfg.xml return new Configuration().configure().buildSessionFactory(); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; }}
  10. 10. Session (org.hibernate.Session) A single-threaded, short-lived object representing a conversation between the application and the persistent store. Wraps a JDBC java.sql.Connection. Used for a single request, a conversation or a single unit of work. A Session will not obtain a JDBC Connection, or a Datasource, unless it is needed. It will not consume any resources until used.
  11. 11. Persistent, Transient and Detached objects Transient - an object is transient if it has just been instantiated using the new operator, and it is not associated with a Hibernate Session. Persistent - a persistent instance has a representation in the database and an identifier value. It might just have been saved or loaded, however, it is by definition in the scope of a Session. Hibernate will detect any changes made to an object in persistent state and synchronize the state with the database when the unit of work completes. Detached - a detached instance is an object that has been persistent, but its Session has been closed. The reference to the object is still valid, of course, and the detached instance might even be modified in this state. A detached instance can be reattached to a new Session at a later point in time, making it (and all the modifications) persistent again.
  12. 12. Transaction (org.hibernate.Transaction) A single-threaded, short-lived object used by the application to specify atomic units of work. It abstracts the application from the underlying JDBC, JTA or CORBA transaction. Security is provided by this API.
  13. 13. ConnectionProvider (org.hibernate.connection.ConnectionProvider) A factory for, and pool of, JDBC connections. It abstracts the application from underlying javax.sql.DataSource or java.sql.DriverManager.
  14. 14. Extended Architecture
  15. 15. JSP Action Mapping Struts-config.xml Success/Failure JSP Fetch ValuesAction Form Refers Action Class Get Parameters Set Parameters Return Values POJO Invoke Methods Update POJO DAO Beans Interacts JDBC Interacts DATABASE
  16. 16. This was a short introduction of hibernate. Want to know more?Query me @ mohdzeeshansafi@yahoo.com Special Thanks to Mr Asif Iqbal

×