Hibernateexm
Hibernateexm
Hibernate Framework
Hibernate is a Java framework that simplifies the development of Java application to
interact with the database. It is an open source, lightweight, ORM (Object Relational
Mapping) tool. Hibernate implements the specifications of JPA (Java Persistence API) for
data persistence.
ORM Tool
An ORM tool simplifies the data creation, data manipulation and data access. It is a
programming technique that maps the object to the data stored in the database.
The ORM tool internally uses the JDBC API to interact with the database.
What is JPA?
Java Persistence API (JPA) is a Java specification that provides certain functionality and
standard to ORM tools. The javax.persistence package contains the JPA classes and
interfaces.
Hibernate framework is open source under the LGPL license and lightweight.
2) Fast Performance
HQL (Hibernate Query Language) is the object-oriented version of SQL. It generates the
database independent queries. So you don't need to write database specific queries.
Before Hibernate, if database is changed for the project, we need to change the SQL
query as well that leads to the maintenance problem.
Hibernate framework provides the facility to create the tables of the database
automatically. So there is no need to create tables in the database manually.
Hibernate supports Query cache and provide statistics about query and database status.
Hibernate Architecture
1. Hibernate Architecture
2. Elements of Hibernate Architecture
1. SessionFactory
2. Session
3. Transaction
4. ConnectionProvider
5. TransactionFactory
The Hibernate architecture includes many objects such as persistent object, session
factory, transaction factory, connection factory, session, transaction etc.
SessionFactory
The session object provides an interface between the application and data stored in the
database. It is a short-lived object and wraps the JDBC connection. It is factory of
Transaction, Query and Criteria. It holds a first-level cache (mandatory) of data. The
org.hibernate.Session interface provides methods to insert, update and delete the
object. It also provides factory methods for Transaction, Query and Criteria.
Transaction
The transaction object specifies the atomic unit of work. It is optional. The
org.hibernate.Transaction interface provides methods for transaction management.
ConnectionProvider
TransactionFactory
Here, we are going to create the first hibernate application without IDE. For creating the
first hibernate application, we need to follow the following steps:
Employee.java
1. package com.javatpoint.mypackage;
2.
3. public class Employee {
4. private int id;
5. private String firstName,lastName;
6.
7. public int getId() {
8. return id;
9. }
10. public void setId(int id) {
11. this.id = id;
12. }
13. public String getFirstName() {
14. return firstName;
15. }
16. public void setFirstName(String firstName) {
17. this.firstName = firstName;
18. }
19. public String getLastName() {
20. return lastName;
21. }
22. public void setLastName(String lastName) {
23. this.lastName = lastName;
24. }
25.
26.
27. }
o hibernate-mapping : It is the root element in the mapping file that contains all the
mapping elements.
o class : It is the sub-element of the hibernate-mapping element. It specifies the Persistent
class.
o id : It is the subelement of class. It specifies the primary key attribute in the class.
o generator : It is the sub-element of id. It is used to generate the primary key. There are
many generator classes such as assigned, increment, hilo, sequence, native etc. We will
learn all the generator classes later.
o property : It is the sub-element of class that specifies the property name of the
Persistent class.
employee.hbm.xml
1. <?xml version='1.0' encoding='UTF-8'?>
2. <!DOCTYPE hibernate-mapping PUBLIC
3. "-//Hibernate/Hibernate Mapping DTD 5.3//EN"
4. "http://hibernate.sourceforge.net/hibernate-mapping-5.3.dtd">
5.
6. <hibernate-mapping>
7. <class name="com.javatpoint.mypackage.Employee" table="emp1000">
8. <id name="id">
9. <generator class="assigned"></generator>
10. </id>
11.
12. <property name="firstName"></property>
13. <property name="lastName"></property>
14.
15. </class>
16.
17. </hibernate-mapping>
hibernate.cfg.xml
1. <?xml version='1.0' encoding='UTF-8'?>
2. <!DOCTYPE hibernate-configuration PUBLIC
3. "-//Hibernate/Hibernate Configuration DTD 5.3//EN"
4. "http://hibernate.sourceforge.net/hibernate-configuration-5.3.dtd">
5.
6. <hibernate-configuration>
7.
8. <session-factory>
9. <property name="hbm2ddl.auto">update</property>
10. <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
11. <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
12. <property name="connection.username">system</property>
13. <property name="connection.password">jtp</property>
14. <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
15. <mapping resource="employee.hbm.xml"/>
16. </session-factory>
17.
18. </hibernate-configuration>
1. package com.javatpoint.mypackage;
2.
3. import org.hibernate.Session;
4. import org.hibernate.SessionFactory;
5. import org.hibernate.Transaction;
6. import org.hibernate.boot.Metadata;
7. import org.hibernate.boot.MetadataSources;
8. import org.hibernate.boot.registry.StandardServiceRegistry;
9. import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
10.
11.
12. public class StoreData {
13. public static void main(String[] args) {
14.
15. //Create typesafe ServiceRegistry object
16. StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().configure("hibernate.cfg.x
ml").build();
17.
18. Metadata meta = new MetadataSources(ssr).getMetadataBuilder().build();
19.
20. SessionFactory factory = meta.getSessionFactoryBuilder().build();
21. Session session = factory.openSession();
22. Transaction t = session.beginTransaction();
23.
24. Employee e1=new Employee();
25. e1.setId(101);
26. e1.setFirstName("Gaurav");
27. e1.setLastName("Chawla");
28.
29. session.save(e1);
30. t.commit();
31. System.out.println("successfully saved");
32. factory.close();
33. session.close();
34.
35. }
36. }