Before knowing Hibernate you must understand JPA.
Introduction
Java Persistence API (JPA)
provides POJO (Plain Old Java Object) standard and object relational
mapping (OR mapping) for data persistence among applications.
Persistence, which deals with storing and retrieving of application
data, can now be programmed with Java Persistence API starting from EJB
3.0 as a result of JSR 220. This API has borrowed many of the concepts
and standards from leading persistence frameworks like Toplink (from
Oracle) and Hibernate (from JBoss). One of the great benefits of JPA is
that it is an independent API and can nicely integrate with J2EE as well
as J2SE applications. [POJO – Plain Old Java Object is a term used to
refer Java objects that do not extend or implement some specialized
classes. Therefore, all normal Java objects are POJO’s only. The
following classes are not POJO classes
Hibernate
Hibernate is a high-performance Object/Relational persistence and
query service which is licensed under the open source GNU Lesser General
Public License (LGPL) and is free to download. Hibernate not only takes
care of the mapping from Java classes to database tables (and from Java
data types to SQL data types), but also provides data query and
retrieval facilities.
This tutorial will teach you how to use Hibernate to develop your database based web applications in simple and easy steps.
JPA is a specification for accessing, persisting and managing the data between Java objects and the relational database. As the definition says its API, it is only the specification. There is no implementation for the API. JPA specifies the set of rules and guidelines for developing the interfaces that follows standard. Straight to the point : JPA is just guidelines to implement the Object Relational Mapping (ORM) and there is no underlying code for the implementation.
Where as, Hibernate is the actual implementation of JPA guidelines. When hibernate implements the JPA specification, this will be certified by the JPA group upon following all the standards mentioned in the specification. For example, JPA guidelines would provide information of mandatory and optional features to be implemented as part of the JPA implementation.
Hibernate is a JPA provider.
Now we start with our aim Hibernate and Spring Integration.
We can simply integrate hibernate application with spring application.
In hibernate framework, we provide all the database information hibernate.cfg.xml file.
But if we are going to integrate the hibernate application with spring, we don't need to create the hibernate.cfg.xml file. We can provide all the information in the applicationContext.xml file.
So it saves a lot of code.
Understanding problem without using spring:
Let's understand it by the code of hibernate given below:
As you can see in the code of sole hibernate, you have to follow so many steps.
Solution by using HibernateTemplate class of Spring Framework:
Now, you don't need to follow so many steps. You can simply write this:
1) create the table in the database In this example, we are using the Oracle as the database, but you may use any database. Let's create the table in the oracle database
2) Employee.java It is a simple POJO class. Here it works as the persistent class for hibernate.
3) employee.hbm.xml This mapping file contains all the information of the persistent class.
This tutorial will teach you how to use Hibernate to develop your database based web applications in simple and easy steps.
JPA and Hibernate Difference
JPA is a specification for accessing, persisting and managing the data between Java objects and the relational database. As the definition says its API, it is only the specification. There is no implementation for the API. JPA specifies the set of rules and guidelines for developing the interfaces that follows standard. Straight to the point : JPA is just guidelines to implement the Object Relational Mapping (ORM) and there is no underlying code for the implementation.
Where as, Hibernate is the actual implementation of JPA guidelines. When hibernate implements the JPA specification, this will be certified by the JPA group upon following all the standards mentioned in the specification. For example, JPA guidelines would provide information of mandatory and optional features to be implemented as part of the JPA implementation.
Hibernate is a JPA provider.
- See more at: http://www.javabeat.net/jpa-vs-hibernate/#sthash.Qln9BBC6.dpuf
JPA and Hibernate DifferenceJPA is a specification for accessing, persisting and managing the data between Java objects and the relational database. As the definition says its API, it is only the specification. There is no implementation for the API. JPA specifies the set of rules and guidelines for developing the interfaces that follows standard. Straight to the point : JPA is just guidelines to implement the Object Relational Mapping (ORM) and there is no underlying code for the implementation.
Where as, Hibernate is the actual implementation of JPA guidelines. When hibernate implements the JPA specification, this will be certified by the JPA group upon following all the standards mentioned in the specification. For example, JPA guidelines would provide information of mandatory and optional features to be implemented as part of the JPA implementation.
Hibernate is a JPA provider.
- See more at: http://www.javabeat.net/jpa-vs-hibernate/#sthash.Qln9BBC6.dpuf
JPA is a specification for accessing, persisting and managing the data between Java objects and the relational database. As the definition says its API, it is only the specification. There is no implementation for the API. JPA specifies the set of rules and guidelines for developing the interfaces that follows standard. Straight to the point : JPA is just guidelines to implement the Object Relational Mapping (ORM) and there is no underlying code for the implementation.
Where as, Hibernate is the actual implementation of JPA guidelines. When hibernate implements the JPA specification, this will be certified by the JPA group upon following all the standards mentioned in the specification. For example, JPA guidelines would provide information of mandatory and optional features to be implemented as part of the JPA implementation.
Hibernate is a JPA provider.
Now we start with our aim Hibernate and Spring Integration.
We can simply integrate hibernate application with spring application.
In hibernate framework, we provide all the database information hibernate.cfg.xml file.
But if we are going to integrate the hibernate application with spring, we don't need to create the hibernate.cfg.xml file. We can provide all the information in the applicationContext.xml file.
Advantage of Spring framework with hibernate
The Spring framework provides HibernateTemplate class, so you
don't need to follow so many steps like create Configuration,
BuildSessionFactory, Session, beginning and committing transaction etc.So it saves a lot of code.
Understanding problem without using spring:
Let's understand it by the code of hibernate given below:
- //creating configuration
- Configuration cfg=new Configuration();
- cfg.configure("hibernate.cfg.xml");
- //creating seession factory object
- SessionFactory factory=cfg.buildSessionFactory();
- //creating session object
- Session session=factory.openSession();
- //creating transaction object
- Transaction t=session.beginTransaction();
- Employee e1=new Employee(111,"arun",40000);
- session.persist(e1);//persisting the object
- t.commit();//transaction is commited
- session.close();
Solution by using HibernateTemplate class of Spring Framework:
Now, you don't need to follow so many steps. You can simply write this:
- Employee e1=new Employee(111,"arun",40000);
- hibernateTemplate.save(e1);
Steps
Let's see what are the simple steps for hibernate and spring integration:- create table in the database It is optional.
- create applicationContext.xml file It contains information of DataSource, SessionFactory etc.
- create Employee.java file It is the persistent class
- create employee.hbm.xml file It is the mapping file.
- create EmployeeDao.java file It is the dao class that uses HibernateTemplate.
- create InsertTest.java file It calls methods of EmployeeDao class.
Example of Hibernate and spring integration
In this example, we are going to integrate the hibernate application with spring. Let's see the directory structure of spring and hibernate example.
1) create the table in the database In this example, we are using the Oracle as the database, but you may use any database. Let's create the table in the oracle database
- CREATE TABLE "EMP558"
- ( "ID" NUMBER(10,0) NOT NULL ENABLE,
- "NAME" VARCHAR2(255 CHAR),
- "SALARY" FLOAT(126),
- PRIMARY KEY ("ID") ENABLE
- )
- /
2) Employee.java It is a simple POJO class. Here it works as the persistent class for hibernate.
- package com.javatpoint;
- public class Employee {
- private int id;
- private String name;
- private float salary;
- //getters and setters
- }
3) employee.hbm.xml This mapping file contains all the information of the persistent class.
- '1.0' encoding='UTF-8'?>
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping>
- <class name="com.javatpoint.Employee" table="emp558">
- <id name="id">
- <generator class="assigned"></generator>
- </id>
- <property name="name"></property>
- <property name="salary"></property>
- </class>
- </hibernate-mapping>
4) EmployeeDao.java
It is a java class that uses the HibernateTemplate class method to persist the object of Employee class.
- package com.javatpoint;
- import org.springframework.orm.hibernate3.HibernateTemplate;
- public class EmployeeDao {
- HibernateTemplate template;
- public void setTemplate(HibernateTemplate template) {
- this.template = template;
- }
- public void saveEmployee(Employee e){
- template.save(e);
- }
- public void updateEmployee(Employee e){
- template.update(e);
- }
- public void deleteEmployee(Employee e){
- template.delete(e);
- }
- }
5) applicationContext.xml
In this file, we are providing all the informations of the database in the BasicDataSource object.
This object is used in the LocalSessionFactoryBean class object, containing some other informations such as mappingResources and hibernateProperties.
The object of LocalSessionFactoryBean class is used in the HibernateTemplate class. Let's see the code of applicationContext.xml file.
- <?xml version="1.0" encoding="UTF-8"?>
- <beans
- xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
-
"driverClassName" value="oracle.jdbc.driver.OracleDriver">
6) InsertTest.java
This class uses the EmployeeDao class object and calls its saveEmployee method by passing the object of Employee class.
- package com.javatpoint;
- import org.springframework.beans.factory.BeanFactory;
- import org.springframework.beans.factory.xml.XmlBeanFactory;
- import org.springframework.core.io.ClassPathResource;
- import org.springframework.core.io.Resource;
- public class InsertTest {
- public static void main(String[] args) {
- Resource r=new ClassPathResource("applicationContext.xml");
- BeanFactory factory=new XmlBeanFactory(r);
- EmployeeDao dao=(EmployeeDao)factory.getBean("d");
- Employee e=new Employee();
- e.setId(114);
- e.setName("varun");
- e.setSalary(50000);
- dao.saveEmployee(e);
- }
- }
Now, if you see the table in the oracle database, record is inserted successfully.
Enabling automatic table creation, showing sql queries etc.
You can enable many hibernate properties like automatic table
creation by hbm2ddl.auto etc. in applicationContext.xml file. Let's see
the code:
- <property name = "hibernateProperties">
- <props>
- <prop key = "hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
- <prop key = "hibernate.hbm2ddl.auto">update</prop>
- <prop key = "hibernate.show_sql">true</prop>
- </props>
If you write this code, you don't need to create table because table will be created automatically.
Now this is all about Hibernate and Spring Integration. Do comment for any quiries.
Introduction to Java Persistence API(JPA)
ava
Persistence API (JPA) provides POJO (Plain Old Java Object) standard
and object relational mapping (OR mapping) for data persistence among
applications. Persistence, which deals with storing and retrieving of
application data, can now be programmed with Java Persistence API
starting from EJB 3.0 as a result of JSR 220. This API has borrowed many
of the concepts and standards from leading persistence frameworks like
Toplink (from Oracle) a - See more at:
http://www.javabeat.net/jpa/#sthash.iVuc8t0G.dpuf
ava
Persistence API (JPA) provides POJO (Plain Old Java Object) standard
and object relational mapping (OR mapping) for data persistence among
applications. Persistence, which deals with storing and retrieving of
application data, can now be programmed with Java Persistence API
starting from EJB 3.0 as a result of JSR 220. This API has borrowed many
of the concepts and standards from leading persistence frameworks like
Toplink (from Oracle) a - See more at:
http://www.javabeat.net/jpa/#sthash.iVuc8t0G.dpuf
Java
Persistence API (JPA) provides POJO (Plain Old Java Object) standard
and object relational mapping (OR mapping) for data persistence among
applications. Persistence, which deals with storing and retrieving of
application data, can now be programmed with Java Persistence API
starting from EJB 3.0 as a result of JSR 220. This API has borrowed many
of the concepts and standards from leading persistence frameworks like
Toplink (from Oracle) and Hibernate (from JBoss). One of the great
benefits of JPA is that it is an independent API and can nicely
integrate with J2EE as well as J2SE applications. [POJO – Plain Old Java
Object is a term used to refer Java objects that do not extend or
implement some specialized classes. Therefore, all normal Java objects
are POJO’s only. The following classes are not POJO classes - See more
at: http://www.javabeat.net/jpa/#sthash.iVuc8t0G.dpuf
Java
Persistence API (JPA) provides POJO (Plain Old Java Object) standard
and object relational mapping (OR mapping) for data persistence among
applications. Persistence, which deals with storing and retrieving of
application data, can now be programmed with Java Persistence API
starting from EJB 3.0 as a result of JSR 220. This API has borrowed many
of the concepts and standards from leading persistence frameworks like
Toplink (from Oracle) and Hibernate (from JBoss). One of the great
benefits of JPA is that it is an independent API and can nicely
integrate with J2EE as well as J2SE applications. [POJO – Plain Old Java
Object is a term used to refer Java objects that do not extend or
implement some specialized classes. Therefore, all normal Java objects
are POJO’s only. The following classes are not POJO classes - See more
at: http://www.javabeat.net/jpa/#sthash.iVuc8t0G.dpuf
Java
Persistence API (JPA) provides POJO (Plain Old Java Object) standard
and object relational mapping (OR mapping) for data persistence among
applications. Persistence, which deals with storing and retrieving of
application data, can now be programmed with Java Persistence API
starting from EJB 3.0 as a result of JSR 220. This API has borrowed many
of the concepts and standards from leading persistence frameworks like
Toplink (from Oracle) and Hibernate (from JBoss). One of the great
benefits of JPA is that it is an independent API and can nicely
integrate with J2EE as well as J2SE applications. [POJO – Plain Old Java
Object is a term used to refer Java objects that do not extend or
implement some specialized classes. Therefore, all normal Java objects
are POJO’s only. The following classes are not POJO classes - See more
at: http://www.javabeat.net/jpa/#sthash.iVuc8t0G.dpuf
Java
Persistence API (JPA) provides POJO (Plain Old Java Object) standard
and object relational mapping (OR mapping) for data persistence among
applications. Persistence, which deals with storing and retrieving of
application data, can now be programmed with Java Persistence API
starting from EJB 3.0 as a result of JSR 220. This API has borrowed many
of the concepts and standards from leading persistence frameworks like
Toplink (from Oracle) and Hibernate (from JBoss). One of the great
benefits of JPA is that it is an independent API and can nicely
integrate with J2EE as well as J2SE applications. [POJO – Plain Old Java
Object is a term used to refer Java objects that do not extend or
implement some specialized classes. Therefore, all normal Java objects
are POJO’s only. The following classes are not POJO classes - See more
at: http://www.javabeat.net/jpa/#sthash.iVuc8t0G.dpuf
Java
Persistence API (JPA) provides POJO (Plain Old Java Object) standard
and object relational mapping (OR mapping) for data persistence among
applications. Persistence, which deals with storing and retrieving of
application data, can now be programmed with Java Persistence API
starting from EJB 3.0 as a result of JSR 220. This API has borrowed many
of the concepts and standards from leading persistence frameworks like
Toplink (from Oracle) and Hibernate (from JBoss). One of the great
benefits of JPA is that it is an independent API and can nicely
integrate with J2EE as well as J2SE applications. [POJO – Plain Old Java
Object is a term used to refer Java objects that do not extend or
implement some specialized classes. Therefore, all normal Java objects
are POJO’s only. The following classes are not POJO classes - See more
at: http://www.javabeat.net/jpa/#sthash.iVuc8t0G.dpuf
Java
Persistence API (JPA) provides POJO (Plain Old Java Object) standard
and object relational mapping (OR mapping) for data persistence among
applications. Persistence, which deals with storing and retrieving of
application data, can now be programmed with Java Persistence API
starting from EJB 3.0 as a result of JSR 220. This API has borrowed many
of the concepts and standards from leading persistence frameworks like
Toplink (from Oracle) and Hibernate (from JBoss). One of the great
benefits of JPA is that it is an independent API and can nicely
integrate with J2EE as well as J2SE applications. [POJO – Plain Old Java
Object is a term used to refer Java objects that do not extend or
implement some specialized classes. Therefore, all normal Java objects
are POJO’s only. The following classes are not POJO classes - See more
at: http://www.javabeat.net/jpa/#sthash.iVuc8t0G.dpuf
Java
Persistence API (JPA) provides POJO (Plain Old Java Object) standard
and object relational mapping (OR mapping) for data persistence among
applications. Persistence, which deals with storing and retrieving of
application data, can now be programmed with Java Persistence API
starting from EJB 3.0 as a result of JSR 220. This API has borrowed many
of the concepts and standards from leading persistence frameworks like
Toplink (from Oracle) and Hibernate (from JBoss). One of the great
benefits of JPA is that it is an independent API and can nicely
integrate with J2EE as well as J2SE applications. [POJO – Plain Old Java
Object is a term used to refer Java objects that do not extend or
implement some specialized classes. Therefore, all normal Java objects
are POJO’s only. The following classes are not POJO classes - See more
at: http://www.javabeat.net/jpa/#sthash.iVuc8t0G.dpuf
Introduction to Java Persistence API(JPA)
Introduction to Java Persistence API(JPA)
Introduction to Java Persistence API(JPA)
No comments:
Post a Comment