SlideShare a Scribd company logo
INTRODUCTION TO HIBERNATE
http://raj-hibernate.blogspot.in/
What is hibernate?
• Is one of the most efficient ORM
implementations in Java
http://raj-hibernate.blogspot.in/
What is ORM?
• is Object Relation Mapping (ORM)
• IS A system that maps the object to Relational
model.
• ORM is not only relation to java only, it also
there in cpp, c#
http://raj-hibernate.blogspot.in/
Understanding why ORM?
• We understand most of the enterprise
applications these days are created using oop
LANGUAGES
• That is , OOP Systems(OOP’s)
• In this condition we know that the activities are
distributed into multiple components.
• This introduces a requirement to describe the
business data between these components (with
in the OOP System)
• To meet this requirement we create a DOM
(Domain Object Model)
http://raj-hibernate.blogspot.in/
What is Domain Object Model(DOM)?
• DOM is a object Model designed to describe
the business domain data between the
components in OOP System.
• Now we also understand most of this business
data is need to be persisted.
http://raj-hibernate.blogspot.in/
What is Persistence data?
• Persistence Data is the data that can be outlive the process in which it is created.
• One of the most common way of persisting the data is using RDBMS (i.e: Relational
Data stores)
• In a relational Data Store we find to create the relational model describing the
business data.
• In this situation(context), that is we have a complex Object model (DOM) in the
OOP System (Enterprise Applications) and relational Model in the backend
datastore to describe the business data in the respective environments.
• Both of them are best in there environments.
• In this case we find some problems because of mismatch between these models as
they are created using different concepts that is, OOP and relational.
• It is also identified that these problems are common in enterprise applications.
• Thus we got some vendors finding interest to provide a readymade solution
implementing the logic to bridge between the object and relational model.
• Such systems are referred as ORM’s and Hibernate is one among them.
http://raj-hibernate.blogspot.in/
http://raj-hibernate.blogspot.in/
The definition of ORM, Diagrammatic Representation
http://raj-hibernate.blogspot.in/
The following are the mismatch problems found in
mapping the object and relational models:
1. Problem of identity
2. Problem of Relationships
3. Problem of subtypes
4. Problem of Granularity
5. Problem of Object Tree Navigation
http://raj-hibernate.blogspot.in/
Features of Hibernate
• Hibernate supports Plain Java objects as persistence objects
• Supports simple XML and annotation style of configuring
the system
• Hibernate supports the two level cache (one at session and
other between the sessions) .This can reduce the
interactions with the database server and thus improve the
performance.
• Hibernate supports object oriented Query Language (HQL)
for querying the objects
• Hibernate supports integrating with the JDBC and JTA
Transactions
• Hibernate includes a Criterion API which facilitates creating
the dynamic queries
http://raj-hibernate.blogspot.in/
Understanding the top level elements of Hibernate
Architecture
Configuration:
• This object of Hibernate system is responsible for loading the configurations into the memory
(hibernate system)
SessionFactory:
• This is responsible to initialize the Hibernate System to service the client (i.e: our java Application)
• This performs all the time taken costlier on-time initializations includes understanding the
configurations and setting up the environment like creating the connection pool, starting the 2nd
level cache and creating the proxy classes
Session:
• This is the core (central part) object of the Hibernate system which is used to access the CRUD
operations
• That means we use the methods of session object to create or read or update or delete the objects
• Session object is created by SessionFactory, it also works with JDBC.
• SESSION is just like a front office execute in the office
Transaction:
• This provides a standard abstraction for accessing the JDBC or JTA Transaction Service
• We know that Hibernate includes support to integrate with JTA
http://raj-hibernate.blogspot.in/
TOP LEVEL ARCHITECTURE HIBERNATE
http://raj-hibernate.blogspot.in/
With this information we now want to
move creating a start up example.
• Hibernate start up Examle:
• The following files are required for this example:
• Employee.java
• Is a persistence class
• Will demonstrate the rules in creating the hibernate persistence class
• Employee.hbm.xml
• Is a hibernate mapping XML document
• Demonstrates how define the mappings using XML style
• hibernate.cfg.xml
• is a hibernate configuration XML File
• HibernateTestCase.java
• Demonstrates implementing the steps involved in accessing the
persistence objects using Hibernate API
http://raj-hibernate.blogspot.in/
What is Hibernate Persistence class?
• Ans:
• It is a java class that is understood by the
Hibernate system to manage its instances.
http://raj-hibernate.blogspot.in/
A java class should satisfy the following rules to become a Hibernate
Persistence class
• Should be a public Non-abstract class
• Should have a no-arg constructor: This is because of the following two reasons:
• Hibernate is programmed to create an instance of the persistence class using no-arg constructor.
• For implementing the lazy loading Hibernate may need to create a dynamic proxy class sub type of
the persistence class, for which no-argument constructor is mandatory
• Should have a java Bean style setter and getter methods for every persistence property.
• <access_specifier> <non_void>
• get<property_name_with_first_char_upper_case>()
• <access_specifier>void
• set<property_name_with_first_char_upper_case>(<one_argument>)
• In addition to these rules; it is recommended to follow the below rules also:
• Make the class and the persistence property getter-setter methods to non-final.
• If not followed may need to compromise with lazy loading (as hibernate could not implement it)
• Implement the hashCode() and equals() methods.
http://raj-hibernate.blogspot.in/
Note:
• We can use the term entity to refer the
persistence class
• Lets create the Employee.java following there
rules:
http://raj-hibernate.blogspot.in/
• package com.st.dom;
•
• public class Employee {
• private int empno, deptno;
• private String name;
• private double sal;
• //we should have no arg constructor
• public Employee(){}
• public Employee(int empno, String name, double sal, int deptno)
• {
• this.empno=empno;
• this.name=name;
• this.sal=sal;
• this.deptno=deptno;
• }
• public int getEmpno()
• {
• return empno;
•
• }
• private void setEmpno(int eno)
• {
• empno=eno;
• }
• public String getName()
• {
• return name;
• }
•
http://raj-hibernate.blogspot.in/
• public void setName(String s)
• {
• name=s;
• }
• public double getSal()
• {
• return sal;
• }
• private void setSal(double s)
• {
• sal=s;
• }
• public int getDeptNo()
• {
• return deptno;
• }
• private void setDeptNo(int d)
• {
• deptno=d;
• }
• }
http://raj-hibernate.blogspot.in/
• Now we have implemented the persistence
class, we need to describe the mapping for
this object to the Hibernate.
• To do this we have two approaches:
• Creating Hibernate Mapping XML
• Using Annotations
• For this example we prefer with Hibernate
Mapping XML (hbm XML)
http://raj-hibernate.blogspot.in/
<?xml version="1.0" encoding="UTF-8"?>
<hibernate-mapping>
<class name="com.st.dom.Employee" table="st_emp">
<id name="empno">
<gen<!-- Employee.hbm.xml
Note: the file name need not match with the persistence class name.
However it is recomended to do such for easy maintanance. Also the extension need not be .hbm.xml but
is recommended to be recognized by many tools (includes IDE)
WHICH CAN INCREASE THE CONVINIENCE OF DEVELOPMENT AND MAINTANANCE -->
<!DOCTYPE-->
<!-- Copy this DOCTYPE from any existing hibernate mapping XML or DTD file -->
<?xml version="1.0" encoding="UTF-8"?>
<hibernate-mapping>
<class name="com.st.dom.Employee" table="st_emp">
<id name="empno">
<generator class="assigned"/>
</id>
<property name="name" column="ename"/>
<property name="sal"/>
<property name="deptno"/>
</class>
</hibernate-mapping>
erator class="assigned"/>
</id>
<property name="name" column="ename"/>
<property name="sal"/>
<property name="deptno"/>
</class>
</hibernate-mapping>
http://raj-hibernate.blogspot.in/
Fig: HIBERNATE_MAPPING.JPG
http://raj-hibernate.blogspot.in/
The hibernate.cfg.xml:
• Now we are telling explained the hibernate mapping between the entity class and table, we want to describe the hibernate about the database it
needs to access (i.e we are informing the address of DB Server)
• To do this we create hibernate.cfg.xml
<!-- hibernate.cfg.xml -->
<!-- DOCTYPE -->
<!-- COPY THE DOCTYPE FROM any existing hibernate cfg xml or dtd -->
<hibernate-configuration>
<session-factory>
<property>
name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:XE
</property>
<property>
<property name="connection.username">
system</property>
<property name="connection.password">
manager</property>
<property name="dialect">
org.hibernate.dialect.Oracle9Dialect</property>
<mapping resource="Employee.hbm.xml"/>
</property>
</session-factory>
</hibernate-configuration>
http://raj-hibernate.blogspot.in/
The Hibernate Test Case:
• Because of the first example, lets only use
Hibernate for reading the object
http://raj-hibernate.blogspot.in/
The following steps are involved in working with
Hibernate API
• Step 1. Create the configuration
• Step2: Build the sessionFactory
• Step3: Get the Session
• Step 4: Access the CRUD operations
• Step 5: close the session
http://raj-hibernate.blogspot.in/
HibernateTestCase.java
• //HibernateTestCase.java
• import com.st.dom.Employee;
• import org.hibernate.cfg.*;
• import org.hibernate.*;
• public class HibernateTestCase
• {
• public static void main(String args[])
• {
• // Step 1. Create the configuration
• Configuration cfg=new Configuration();
• cfg.configure();
• //Step2: Build the sessionFactory
• SessionFactory sf=cfg.buildSessionFactory();
• //Step3: Get the Session
• Session session=sf.openSession();
http://raj-hibernate.blogspot.in/
• //Step 4: Access the CRUD operations
• //to read the object
• Employee
emp=(Employee)session.load(Employee.class,101);
• /* 101 is the empno(i.e id) this will query the Employee object with
the identifier (empno) value 101*/
• //to test
• System.out.println("Name :"+emp.getName());
• System.out.println("Salary :"+emp.getSal());
• System.out.println("Deptno "+emp.getDeptNo());
• // Step 5: close the session
• session.close();
• }//main()
•
• }//class
http://raj-hibernate.blogspot.in/
To compile and run this program:
• To compile and run this program:
• * we want to have the following installations /jars
• * (1) JDK
• * (2) Oracle DB (otherwise any other DB Server)
• * (3) Hibernate ORM downloads
• * we can download this from following site:
• * www.hibernate.org
• * We get a simple zip file to download, Extract it you will find all the necessary jar files.
• *
• Do the following to successfully Run this example:
• 1. copy the DOCTYPE into the XML documents (hibertate3.jarorghibernate-zip archieve)
• we can find DTD files in the hibernate3.jar file
• ->open the jar file with winzip or winrar
• ->coy the doctype from hibernate-configuration-3.0.dtd file into the hibernate.cfg.xml
• ->copy the doctype from hibernate-mapping-3.0.dtd file into the Employee.hbm.xml
• 2. set the following jar files into classpath:
• -hibernate3.jar
• -antlr-2.7.6.jar
• -commons-collections-3.1.jar
• -dom4j-1.6.1.jar
• -javassist-3.12.0.GA.jar
• -jta-1.1.jar
• -hibernate-jpa-2.0-api-1.0.1.Final.jar
• -ojdbc14.jar
• (to set the class path better to do batch file and you can execute when u want)
http://raj-hibernate.blogspot.in/
To compile and run this program:
• 3. create the following table and record in the database
server:
• create table st_emp(
• empno number primary-key,
• ename varchar2(20),
• sal number(10,2),
• deptno number);
•
• insert into st_emp values(101,'e101',10000,10);
• commit;
• 4. compile java files and Run
•
http://raj-hibernate.blogspot.in/
• >javac -d . *.java
• >classpath.bat //this executes the set the
class path
• >java HibernateTestCase
http://raj-hibernate.blogspot.in/

More Related Content

What's hot

Interface callable statement
Interface callable statementInterface callable statement
Interface callable statementmyrajendra
 
Java- JDBC- Mazenet Solution
Java- JDBC- Mazenet SolutionJava- JDBC- Mazenet Solution
Java- JDBC- Mazenet Solution
Mazenetsolution
 
Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)
Pooja Talreja
 
Jdbc architecture and driver types ppt
Jdbc architecture and driver types pptJdbc architecture and driver types ppt
Jdbc architecture and driver types ppt
kamal kotecha
 
JDBC,Types of JDBC,Resultset, statements,PreparedStatement,CallableStatements...
JDBC,Types of JDBC,Resultset, statements,PreparedStatement,CallableStatements...JDBC,Types of JDBC,Resultset, statements,PreparedStatement,CallableStatements...
JDBC,Types of JDBC,Resultset, statements,PreparedStatement,CallableStatements...
Pallepati Vasavi
 
Jdbc complete
Jdbc completeJdbc complete
Jdbc complete
Sandeep Rawat
 
Jdbc
JdbcJdbc
Jdbc in servlets
Jdbc in servletsJdbc in servlets
Jdbc in servlets
Nuha Noor
 
Jdbc_ravi_2016
Jdbc_ravi_2016Jdbc_ravi_2016
Jdbc_ravi_2016
Ravinder Singh Karki
 
Chap3 3 12
Chap3 3 12Chap3 3 12
Chap3 3 12
Hemo Chella
 
Overview Of JDBC
Overview Of JDBCOverview Of JDBC
Overview Of JDBC
Mindfire Solutions
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivity
Vaishali Modi
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
Tanmoy Barman
 
java Jdbc
java Jdbc java Jdbc
java Jdbc
Ankit Desai
 
Core jdbc basics
Core jdbc basicsCore jdbc basics
Core jdbc basics
Sourabrata Mukherjee
 
Jdbc (database in java)
Jdbc (database in java)Jdbc (database in java)
Jdbc (database in java)Maher Abdo
 
JDBC
JDBCJDBC

What's hot (20)

Interface callable statement
Interface callable statementInterface callable statement
Interface callable statement
 
JDBC
JDBCJDBC
JDBC
 
Java- JDBC- Mazenet Solution
Java- JDBC- Mazenet SolutionJava- JDBC- Mazenet Solution
Java- JDBC- Mazenet Solution
 
Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)
 
Jdbc architecture and driver types ppt
Jdbc architecture and driver types pptJdbc architecture and driver types ppt
Jdbc architecture and driver types ppt
 
JDBC,Types of JDBC,Resultset, statements,PreparedStatement,CallableStatements...
JDBC,Types of JDBC,Resultset, statements,PreparedStatement,CallableStatements...JDBC,Types of JDBC,Resultset, statements,PreparedStatement,CallableStatements...
JDBC,Types of JDBC,Resultset, statements,PreparedStatement,CallableStatements...
 
Jdbc complete
Jdbc completeJdbc complete
Jdbc complete
 
Jdbc
JdbcJdbc
Jdbc
 
Jdbc in servlets
Jdbc in servletsJdbc in servlets
Jdbc in servlets
 
Jdbc_ravi_2016
Jdbc_ravi_2016Jdbc_ravi_2016
Jdbc_ravi_2016
 
Chap3 3 12
Chap3 3 12Chap3 3 12
Chap3 3 12
 
Overview Of JDBC
Overview Of JDBCOverview Of JDBC
Overview Of JDBC
 
jdbc document
jdbc documentjdbc document
jdbc document
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivity
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
 
java Jdbc
java Jdbc java Jdbc
java Jdbc
 
Core jdbc basics
Core jdbc basicsCore jdbc basics
Core jdbc basics
 
Jdbc (database in java)
Jdbc (database in java)Jdbc (database in java)
Jdbc (database in java)
 
Jdbc
JdbcJdbc
Jdbc
 
JDBC
JDBCJDBC
JDBC
 

Viewers also liked

Jdbc workflow
Jdbc workflowJdbc workflow
Jdbc workflow
myrajendra
 
String classes and its methods.20
String classes and its methods.20String classes and its methods.20
String classes and its methods.20myrajendra
 
Strings In OOP(Object oriented programming)
Strings In OOP(Object oriented programming)Strings In OOP(Object oriented programming)
Strings In OOP(Object oriented programming)
Danial Virk
 
Types of memory
Types of memoryTypes of memory
Types of memorymyrajendra
 
Types of memory 10 to11
Types of memory 10 to11Types of memory 10 to11
Types of memory 10 to11myrajendra
 
Data type
Data typeData type
Data type
myrajendra
 
38 paged segmentation
38 paged segmentation38 paged segmentation
38 paged segmentationmyrajendra
 
Fundamentals
FundamentalsFundamentals
Fundamentals
myrajendra
 
File management53(1)
File management53(1)File management53(1)
File management53(1)myrajendra
 
35. multiplepartitionallocation
35. multiplepartitionallocation35. multiplepartitionallocation
35. multiplepartitionallocationmyrajendra
 
36 fragmentaio nnd pageconcepts
36 fragmentaio nnd pageconcepts36 fragmentaio nnd pageconcepts
36 fragmentaio nnd pageconceptsmyrajendra
 
40 demand paging
40 demand paging40 demand paging
40 demand pagingmyrajendra
 
Thrashing allocation frames.43
Thrashing allocation frames.43Thrashing allocation frames.43
Thrashing allocation frames.43myrajendra
 
39 virtual memory
39 virtual memory39 virtual memory
39 virtual memorymyrajendra
 
37 segmentation
37 segmentation37 segmentation
37 segmentationmyrajendra
 
Segmentation in Operating Systems.
Segmentation in Operating Systems.Segmentation in Operating Systems.
Segmentation in Operating Systems.
Muhammad SiRaj Munir
 
04 cache memory
04 cache memory04 cache memory
04 cache memory
Inshad Arshad
 

Viewers also liked (20)

Jdbc workflow
Jdbc workflowJdbc workflow
Jdbc workflow
 
String classes and its methods.20
String classes and its methods.20String classes and its methods.20
String classes and its methods.20
 
Strings In OOP(Object oriented programming)
Strings In OOP(Object oriented programming)Strings In OOP(Object oriented programming)
Strings In OOP(Object oriented programming)
 
Properties
PropertiesProperties
Properties
 
Starting jdbc
Starting jdbcStarting jdbc
Starting jdbc
 
Types of memory
Types of memoryTypes of memory
Types of memory
 
Types of memory 10 to11
Types of memory 10 to11Types of memory 10 to11
Types of memory 10 to11
 
Data type
Data typeData type
Data type
 
38 paged segmentation
38 paged segmentation38 paged segmentation
38 paged segmentation
 
Fundamentals
FundamentalsFundamentals
Fundamentals
 
File management53(1)
File management53(1)File management53(1)
File management53(1)
 
35. multiplepartitionallocation
35. multiplepartitionallocation35. multiplepartitionallocation
35. multiplepartitionallocation
 
36 fragmentaio nnd pageconcepts
36 fragmentaio nnd pageconcepts36 fragmentaio nnd pageconcepts
36 fragmentaio nnd pageconcepts
 
40 demand paging
40 demand paging40 demand paging
40 demand paging
 
Thrashing allocation frames.43
Thrashing allocation frames.43Thrashing allocation frames.43
Thrashing allocation frames.43
 
39 virtual memory
39 virtual memory39 virtual memory
39 virtual memory
 
37 segmentation
37 segmentation37 segmentation
37 segmentation
 
Segmentation in Operating Systems.
Segmentation in Operating Systems.Segmentation in Operating Systems.
Segmentation in Operating Systems.
 
04 cache memory
04 cache memory04 cache memory
04 cache memory
 
Paging and segmentation
Paging and segmentationPaging and segmentation
Paging and segmentation
 

Similar to Hibernate example1

Hibernate complete Training
Hibernate complete TrainingHibernate complete Training
Hibernate complete Trainingsourabh aggarwal
 
Hibernate tutorial
Hibernate tutorialHibernate tutorial
Hibernate tutorial
Mumbai Academisc
 
Introduction to Spring
Introduction to SpringIntroduction to Spring
Introduction to SpringSujit Kumar
 
Java Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and ExampleJava Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and Example
kamal kotecha
 
12 Introduction to Rails
12 Introduction to Rails12 Introduction to Rails
12 Introduction to Rails
Deepak Hagadur Bheemaraju
 
Spring MVC framework
Spring MVC frameworkSpring MVC framework
Spring MVC framework
Mohit Gupta
 
AtoM's Command Line Tasks - An Introduction
AtoM's Command Line Tasks - An IntroductionAtoM's Command Line Tasks - An Introduction
AtoM's Command Line Tasks - An Introduction
Artefactual Systems - AtoM
 
The Basic Concept Of IOC
The Basic Concept Of IOCThe Basic Concept Of IOC
The Basic Concept Of IOCCarl Lu
 
Hibernate presentation
Hibernate presentationHibernate presentation
Hibernate presentation
Krishnakanth Goud
 
Hibernate presentation
Hibernate presentationHibernate presentation
Hibernate presentation
Manav Prasad
 
Hibernate
HibernateHibernate
What is struts_en
What is struts_enWhat is struts_en
What is struts_entechbed
 
Hibernate interview questions
Hibernate interview questionsHibernate interview questions
Hibernate interview questions
venkata52
 
Owner - Java properties reinvented.
Owner - Java properties reinvented.Owner - Java properties reinvented.
Owner - Java properties reinvented.
Luigi Viggiano
 
They why behind php frameworks
They why behind php frameworksThey why behind php frameworks
They why behind php frameworks
Kirk Madera
 
Implementing a Symfony Based CMS in a Publishing Company
Implementing a Symfony Based CMS in a Publishing CompanyImplementing a Symfony Based CMS in a Publishing Company
Implementing a Symfony Based CMS in a Publishing Company
Marcos Labad
 
Domino java
Domino javaDomino java
Domino java
Sumit Tambe
 
Grails Services
Grails ServicesGrails Services
Grails Services
NexThoughts Technologies
 

Similar to Hibernate example1 (20)

Hibernate complete Training
Hibernate complete TrainingHibernate complete Training
Hibernate complete Training
 
Hibernate tutorial
Hibernate tutorialHibernate tutorial
Hibernate tutorial
 
Spring & hibernate
Spring & hibernateSpring & hibernate
Spring & hibernate
 
Introduction to Spring
Introduction to SpringIntroduction to Spring
Introduction to Spring
 
Java Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and ExampleJava Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and Example
 
12 Introduction to Rails
12 Introduction to Rails12 Introduction to Rails
12 Introduction to Rails
 
Java part 3
Java part  3Java part  3
Java part 3
 
Spring MVC framework
Spring MVC frameworkSpring MVC framework
Spring MVC framework
 
AtoM's Command Line Tasks - An Introduction
AtoM's Command Line Tasks - An IntroductionAtoM's Command Line Tasks - An Introduction
AtoM's Command Line Tasks - An Introduction
 
The Basic Concept Of IOC
The Basic Concept Of IOCThe Basic Concept Of IOC
The Basic Concept Of IOC
 
Hibernate presentation
Hibernate presentationHibernate presentation
Hibernate presentation
 
Hibernate presentation
Hibernate presentationHibernate presentation
Hibernate presentation
 
Hibernate
HibernateHibernate
Hibernate
 
What is struts_en
What is struts_enWhat is struts_en
What is struts_en
 
Hibernate interview questions
Hibernate interview questionsHibernate interview questions
Hibernate interview questions
 
Owner - Java properties reinvented.
Owner - Java properties reinvented.Owner - Java properties reinvented.
Owner - Java properties reinvented.
 
They why behind php frameworks
They why behind php frameworksThey why behind php frameworks
They why behind php frameworks
 
Implementing a Symfony Based CMS in a Publishing Company
Implementing a Symfony Based CMS in a Publishing CompanyImplementing a Symfony Based CMS in a Publishing Company
Implementing a Symfony Based CMS in a Publishing Company
 
Domino java
Domino javaDomino java
Domino java
 
Grails Services
Grails ServicesGrails Services
Grails Services
 

More from myrajendra

Sessionex1
Sessionex1Sessionex1
Sessionex1
myrajendra
 
1 introduction to html
1 introduction to html1 introduction to html
1 introduction to htmlmyrajendra
 
Views
ViewsViews
Views
myrajendra
 
Interface result set
Interface result setInterface result set
Interface result setmyrajendra
 
Interface database metadata
Interface database metadataInterface database metadata
Interface database metadatamyrajendra
 
Interface connection
Interface connectionInterface connection
Interface connectionmyrajendra
 
Get excelsheet
Get excelsheetGet excelsheet
Get excelsheetmyrajendra
 
Get data
Get dataGet data
Get data
myrajendra
 
Exceptions
ExceptionsExceptions
Exceptions
myrajendra
 
Driver
DriverDriver
Driver
myrajendra
 

More from myrajendra (18)

Sessionex1
Sessionex1Sessionex1
Sessionex1
 
Internal
InternalInternal
Internal
 
3. elements
3. elements3. elements
3. elements
 
2. attributes
2. attributes2. attributes
2. attributes
 
1 introduction to html
1 introduction to html1 introduction to html
1 introduction to html
 
Headings
HeadingsHeadings
Headings
 
Forms
FormsForms
Forms
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Interface result set
Interface result setInterface result set
Interface result set
 
Interface database metadata
Interface database metadataInterface database metadata
Interface database metadata
 
Interface connection
Interface connectionInterface connection
Interface connection
 
Indexing
IndexingIndexing
Indexing
 
Get excelsheet
Get excelsheetGet excelsheet
Get excelsheet
 
Get data
Get dataGet data
Get data
 
Exceptions
ExceptionsExceptions
Exceptions
 
Driver
DriverDriver
Driver
 

Recently uploaded

Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 

Recently uploaded (20)

Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 

Hibernate example1

  • 2. What is hibernate? • Is one of the most efficient ORM implementations in Java http://raj-hibernate.blogspot.in/
  • 3. What is ORM? • is Object Relation Mapping (ORM) • IS A system that maps the object to Relational model. • ORM is not only relation to java only, it also there in cpp, c# http://raj-hibernate.blogspot.in/
  • 4. Understanding why ORM? • We understand most of the enterprise applications these days are created using oop LANGUAGES • That is , OOP Systems(OOP’s) • In this condition we know that the activities are distributed into multiple components. • This introduces a requirement to describe the business data between these components (with in the OOP System) • To meet this requirement we create a DOM (Domain Object Model) http://raj-hibernate.blogspot.in/
  • 5. What is Domain Object Model(DOM)? • DOM is a object Model designed to describe the business domain data between the components in OOP System. • Now we also understand most of this business data is need to be persisted. http://raj-hibernate.blogspot.in/
  • 6. What is Persistence data? • Persistence Data is the data that can be outlive the process in which it is created. • One of the most common way of persisting the data is using RDBMS (i.e: Relational Data stores) • In a relational Data Store we find to create the relational model describing the business data. • In this situation(context), that is we have a complex Object model (DOM) in the OOP System (Enterprise Applications) and relational Model in the backend datastore to describe the business data in the respective environments. • Both of them are best in there environments. • In this case we find some problems because of mismatch between these models as they are created using different concepts that is, OOP and relational. • It is also identified that these problems are common in enterprise applications. • Thus we got some vendors finding interest to provide a readymade solution implementing the logic to bridge between the object and relational model. • Such systems are referred as ORM’s and Hibernate is one among them. http://raj-hibernate.blogspot.in/
  • 8. The definition of ORM, Diagrammatic Representation http://raj-hibernate.blogspot.in/
  • 9. The following are the mismatch problems found in mapping the object and relational models: 1. Problem of identity 2. Problem of Relationships 3. Problem of subtypes 4. Problem of Granularity 5. Problem of Object Tree Navigation http://raj-hibernate.blogspot.in/
  • 10. Features of Hibernate • Hibernate supports Plain Java objects as persistence objects • Supports simple XML and annotation style of configuring the system • Hibernate supports the two level cache (one at session and other between the sessions) .This can reduce the interactions with the database server and thus improve the performance. • Hibernate supports object oriented Query Language (HQL) for querying the objects • Hibernate supports integrating with the JDBC and JTA Transactions • Hibernate includes a Criterion API which facilitates creating the dynamic queries http://raj-hibernate.blogspot.in/
  • 11. Understanding the top level elements of Hibernate Architecture Configuration: • This object of Hibernate system is responsible for loading the configurations into the memory (hibernate system) SessionFactory: • This is responsible to initialize the Hibernate System to service the client (i.e: our java Application) • This performs all the time taken costlier on-time initializations includes understanding the configurations and setting up the environment like creating the connection pool, starting the 2nd level cache and creating the proxy classes Session: • This is the core (central part) object of the Hibernate system which is used to access the CRUD operations • That means we use the methods of session object to create or read or update or delete the objects • Session object is created by SessionFactory, it also works with JDBC. • SESSION is just like a front office execute in the office Transaction: • This provides a standard abstraction for accessing the JDBC or JTA Transaction Service • We know that Hibernate includes support to integrate with JTA http://raj-hibernate.blogspot.in/
  • 12. TOP LEVEL ARCHITECTURE HIBERNATE http://raj-hibernate.blogspot.in/
  • 13. With this information we now want to move creating a start up example. • Hibernate start up Examle: • The following files are required for this example: • Employee.java • Is a persistence class • Will demonstrate the rules in creating the hibernate persistence class • Employee.hbm.xml • Is a hibernate mapping XML document • Demonstrates how define the mappings using XML style • hibernate.cfg.xml • is a hibernate configuration XML File • HibernateTestCase.java • Demonstrates implementing the steps involved in accessing the persistence objects using Hibernate API http://raj-hibernate.blogspot.in/
  • 14. What is Hibernate Persistence class? • Ans: • It is a java class that is understood by the Hibernate system to manage its instances. http://raj-hibernate.blogspot.in/
  • 15. A java class should satisfy the following rules to become a Hibernate Persistence class • Should be a public Non-abstract class • Should have a no-arg constructor: This is because of the following two reasons: • Hibernate is programmed to create an instance of the persistence class using no-arg constructor. • For implementing the lazy loading Hibernate may need to create a dynamic proxy class sub type of the persistence class, for which no-argument constructor is mandatory • Should have a java Bean style setter and getter methods for every persistence property. • <access_specifier> <non_void> • get<property_name_with_first_char_upper_case>() • <access_specifier>void • set<property_name_with_first_char_upper_case>(<one_argument>) • In addition to these rules; it is recommended to follow the below rules also: • Make the class and the persistence property getter-setter methods to non-final. • If not followed may need to compromise with lazy loading (as hibernate could not implement it) • Implement the hashCode() and equals() methods. http://raj-hibernate.blogspot.in/
  • 16. Note: • We can use the term entity to refer the persistence class • Lets create the Employee.java following there rules: http://raj-hibernate.blogspot.in/
  • 17. • package com.st.dom; • • public class Employee { • private int empno, deptno; • private String name; • private double sal; • //we should have no arg constructor • public Employee(){} • public Employee(int empno, String name, double sal, int deptno) • { • this.empno=empno; • this.name=name; • this.sal=sal; • this.deptno=deptno; • } • public int getEmpno() • { • return empno; • • } • private void setEmpno(int eno) • { • empno=eno; • } • public String getName() • { • return name; • } • http://raj-hibernate.blogspot.in/
  • 18. • public void setName(String s) • { • name=s; • } • public double getSal() • { • return sal; • } • private void setSal(double s) • { • sal=s; • } • public int getDeptNo() • { • return deptno; • } • private void setDeptNo(int d) • { • deptno=d; • } • } http://raj-hibernate.blogspot.in/
  • 19. • Now we have implemented the persistence class, we need to describe the mapping for this object to the Hibernate. • To do this we have two approaches: • Creating Hibernate Mapping XML • Using Annotations • For this example we prefer with Hibernate Mapping XML (hbm XML) http://raj-hibernate.blogspot.in/
  • 20. <?xml version="1.0" encoding="UTF-8"?> <hibernate-mapping> <class name="com.st.dom.Employee" table="st_emp"> <id name="empno"> <gen<!-- Employee.hbm.xml Note: the file name need not match with the persistence class name. However it is recomended to do such for easy maintanance. Also the extension need not be .hbm.xml but is recommended to be recognized by many tools (includes IDE) WHICH CAN INCREASE THE CONVINIENCE OF DEVELOPMENT AND MAINTANANCE --> <!DOCTYPE--> <!-- Copy this DOCTYPE from any existing hibernate mapping XML or DTD file --> <?xml version="1.0" encoding="UTF-8"?> <hibernate-mapping> <class name="com.st.dom.Employee" table="st_emp"> <id name="empno"> <generator class="assigned"/> </id> <property name="name" column="ename"/> <property name="sal"/> <property name="deptno"/> </class> </hibernate-mapping> erator class="assigned"/> </id> <property name="name" column="ename"/> <property name="sal"/> <property name="deptno"/> </class> </hibernate-mapping> http://raj-hibernate.blogspot.in/
  • 22. The hibernate.cfg.xml: • Now we are telling explained the hibernate mapping between the entity class and table, we want to describe the hibernate about the database it needs to access (i.e we are informing the address of DB Server) • To do this we create hibernate.cfg.xml <!-- hibernate.cfg.xml --> <!-- DOCTYPE --> <!-- COPY THE DOCTYPE FROM any existing hibernate cfg xml or dtd --> <hibernate-configuration> <session-factory> <property> name="connection.driver_class"> oracle.jdbc.driver.OracleDriver </property> <property name="connection.url"> jdbc:oracle:thin:@localhost:1521:XE </property> <property> <property name="connection.username"> system</property> <property name="connection.password"> manager</property> <property name="dialect"> org.hibernate.dialect.Oracle9Dialect</property> <mapping resource="Employee.hbm.xml"/> </property> </session-factory> </hibernate-configuration> http://raj-hibernate.blogspot.in/
  • 23. The Hibernate Test Case: • Because of the first example, lets only use Hibernate for reading the object http://raj-hibernate.blogspot.in/
  • 24. The following steps are involved in working with Hibernate API • Step 1. Create the configuration • Step2: Build the sessionFactory • Step3: Get the Session • Step 4: Access the CRUD operations • Step 5: close the session http://raj-hibernate.blogspot.in/
  • 25. HibernateTestCase.java • //HibernateTestCase.java • import com.st.dom.Employee; • import org.hibernate.cfg.*; • import org.hibernate.*; • public class HibernateTestCase • { • public static void main(String args[]) • { • // Step 1. Create the configuration • Configuration cfg=new Configuration(); • cfg.configure(); • //Step2: Build the sessionFactory • SessionFactory sf=cfg.buildSessionFactory(); • //Step3: Get the Session • Session session=sf.openSession(); http://raj-hibernate.blogspot.in/
  • 26. • //Step 4: Access the CRUD operations • //to read the object • Employee emp=(Employee)session.load(Employee.class,101); • /* 101 is the empno(i.e id) this will query the Employee object with the identifier (empno) value 101*/ • //to test • System.out.println("Name :"+emp.getName()); • System.out.println("Salary :"+emp.getSal()); • System.out.println("Deptno "+emp.getDeptNo()); • // Step 5: close the session • session.close(); • }//main() • • }//class http://raj-hibernate.blogspot.in/
  • 27. To compile and run this program: • To compile and run this program: • * we want to have the following installations /jars • * (1) JDK • * (2) Oracle DB (otherwise any other DB Server) • * (3) Hibernate ORM downloads • * we can download this from following site: • * www.hibernate.org • * We get a simple zip file to download, Extract it you will find all the necessary jar files. • * • Do the following to successfully Run this example: • 1. copy the DOCTYPE into the XML documents (hibertate3.jarorghibernate-zip archieve) • we can find DTD files in the hibernate3.jar file • ->open the jar file with winzip or winrar • ->coy the doctype from hibernate-configuration-3.0.dtd file into the hibernate.cfg.xml • ->copy the doctype from hibernate-mapping-3.0.dtd file into the Employee.hbm.xml • 2. set the following jar files into classpath: • -hibernate3.jar • -antlr-2.7.6.jar • -commons-collections-3.1.jar • -dom4j-1.6.1.jar • -javassist-3.12.0.GA.jar • -jta-1.1.jar • -hibernate-jpa-2.0-api-1.0.1.Final.jar • -ojdbc14.jar • (to set the class path better to do batch file and you can execute when u want) http://raj-hibernate.blogspot.in/
  • 28. To compile and run this program: • 3. create the following table and record in the database server: • create table st_emp( • empno number primary-key, • ename varchar2(20), • sal number(10,2), • deptno number); • • insert into st_emp values(101,'e101',10000,10); • commit; • 4. compile java files and Run • http://raj-hibernate.blogspot.in/
  • 29. • >javac -d . *.java • >classpath.bat //this executes the set the class path • >java HibernateTestCase http://raj-hibernate.blogspot.in/

Editor's Notes

  1. Fig: Hibernate2.JPG