Java Hibernate Introduction, Architecture and Example with step by step guidance to run the program especially for students and teachers.
Learn More @ http://java2all.com/technology/hibernate
3. Hibernate is an open source object relational mapping tool used
in Java programming.
Hibernate allows data to be inserted, removed or changed in a
database without paying a lot of attention to how it gets there.
Hibernate was founded by Mr. Gavin King, an Australian
developer who needed to solve a problem and ended up creating
Hibernate, an open source tool that is amazingly useful with JAVA.
The central idea of Hibernate is this: “JAVA programmers are
used to creating POJOs [Plain Old Java Objects] in Java.
http://www.java2all.com
4. http://www.java2all.com
So why do they need a second language like SQL, to put or apply
(persist) those POJOs into the database? ”
Hibernate does object-relational persistence and querying.
That means Hibernate puts whole objects into a relational
database and pulls whole objects out of a relational database.
When Can I Use Hibernate?
Hibernate can be used in any Java program that needs to access a
relational database. Hibernate does not need an application server to run.
5. http://www.java2all.com
When Should I Use Hibernate?
There are main three points at where Hibernate is use if :
(1)There is a non-trivial application
(2) There are more than 10 tables in the relational DB.
(3) The application uses an object-oriented Domain Model.
If the application does a lot of business logic—and does much
more than just display tables of data on a webpage—then it is a good
candidate for Hibernate.
6. http://www.java2all.com
Hibernate is best in applications with complex data models, with
hundreds of tables and complex inter-relationships.
So Hibernate maps the java classes to the database tables. It also
provides the data query and retrieval facilities that reduce the
development time.
Note:
Domain Model means an object model of the domain that
incorporates both behavior and data
Note:
Hibernate is most useful with object-oriented domain modes and
business logic in the Java based middle-tier.
It is not the best solutions for data centric application that only
uses the stored-procedures to implement the business logic in database.
8. To use Hibernate, it is required to create Java classes that
represents a table in the database and then map the instance variable in
the class with the columns in the database.
Then Hibernate can be used to perform operations on the
database like select, insert, update and delete the records in the table.
Hibernate automatically creates the query to perform these
operations.
For that Hibernate architecture has three main components:
(1) Connection Management
(2) Transaction management
(3) Object relational mapping
The following diagram describes the high level architecture of hibernate:
http://www.java2all.com
10. The above diagram shows that Hibernate is using the
database and configuration Data(Hibernate.prpperties and XML
Mappping) to provide persistence services (and persistent
objects) to the application.
(1) Connection Management:
Hibernate Connection management service provide efficient
management of the database connections. Database connection is
the most expensive part of interacting with the database as it
requires a lot of resources of open and close the database
connection.
http://www.java2all.com
11. (2) Transaction management:
Transaction management service provides the ability to the user
to execute more than one database statements at a time.
(3) Object relational mapping:
Object relational mapping is technique of mapping the data
representation from an object model to a relational data model.
This part of hibernate is used to select, insert, update and delete
the records form the underlying table.
http://www.java2all.com
12. When we pass an object to a Session.save() method, Hibernate
reads the state of the variables of that object and executes the necessary
query.
Hibernate provides a lot of flexibility in use. It is called "Lite"
architecture when we only use the object relational mapping component.
While in "Full Cream" architecture all the three components
object Relational mapping, Connection Management and transaction
Management) are used.
http://www.java2all.com
13. http://www.java2all.com
NOTE:
Hibernate is very good tool as far as object relational mapping is
concern, but in terms of connection management and transaction
management, it is lacking in performance and capabilities.
So usually hibernate is being used with other connection management
and transaction management tools.
For example apache DBCP is used for connection pooling with
the Hibernate.
15. Write a java hibernate example or program to insert a record in
database.
NOTE:
The all theoretical steps, source code and program to insert a
record in database is described below and the steps you should perform
in MyEclipse to do java hibernate example is available in above video.
Step 1: Right Click on project and select My Eclipse and then select the
Add Hibernate Capabilities.
Step 2: Now Click on Next Button.
Step 3: Again Click on Next Button.
Step 4: Select the appropriate database at DB Driver combo box and
then click on Next button.
http://www.java2all.com
16. Step 5: Select the appropriate Java package by click on Browse button.
Step 6: Then click on OK button.
From here now the programming files will be generated and still
total 8 steps are remaining that is mentioned below this tutorial is
especially created for beginners to do java hibernate example.
Step 7: Then click on Finish button So it will automatically create two
files:
1. This is an xml file(hibernate configuration) which is available
in the src like hibernate.cfg.xml which contains the properties of
connection with database for session-factory like username, url, dialect,
profile, password, driver_class.
http://www.java2all.com
17. http://www.java2all.com
2. Second is a HibernateSessionFactory.java file which is available at the
com package for handling the session and its properties.
Hibernate uses the hibernate.cfg.xml to create the connection
pool and setup required environment.
19. http://www.java2all.com
Hibernate supports many database.
With the use of the Hibernate (Object/Relational Mapping and
Transparent Object Persistence for Java and SQL Databases), we can use
the following databases dialect type property:
28. http://www.java2all.com
package hibernate_demo;
import java.util.Scanner;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.Contact;
public class FirstExample {
public static void main(String [] args)
{
Session session = null;
try {
SessionFactory sessionFactory = new
Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
System.out.println( "Inserting Record…………");
Contact contact = new Contact();
Scanner s = new Scanner(System.in);
System.out.println("Enter Id : ");
int id = s.nextInt(); contact.setId(id);
package hibernate_demo;
import java.util.Scanner;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.Contact;
public class FirstExample {
public static void main(String [] args)
{
Session session = null;
try {
SessionFactory sessionFactory = new
Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
System.out.println( "Inserting Record…………");
Contact contact = new Contact();
Scanner s = new Scanner(System.in);
System.out.println("Enter Id : ");
int id = s.nextInt(); contact.setId(id);
Step 11: Now create the java file by which the data is inserting in
the table (CONTACT) at database.
29. http://www.java2all.com
System.out.println("Enter First name : ");
String firstname = s.next();
contact.setFirstName(firstname);
System.out.println("Enter Last name : ");
String lastname = s.next();
contact.setLastName(lastname);
System.out.println("Enter Email id : ");
String email = s.next();
contact.setEmail(email);
session.save(contact);
System.out.println("Data inserted successfully………");
} catch(Exception e){
System.out.println(e.getMessage());
}
finally {
session.flush(); session.close();
}
} }
System.out.println("Enter First name : ");
String firstname = s.next();
contact.setFirstName(firstname);
System.out.println("Enter Last name : ");
String lastname = s.next();
contact.setLastName(lastname);
System.out.println("Enter Email id : ");
String email = s.next();
contact.setEmail(email);
session.save(contact);
System.out.println("Data inserted successfully………");
} catch(Exception e){
System.out.println(e.getMessage());
}
finally {
session.flush(); session.close();
}
} }