Professional Documents
Culture Documents
Hibernate
Hibernate
The ORM tool internally uses the JDBC API to interact with the database.
What is JPA?
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/16septhibernate</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.pool_size">3</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<property
name="hibernate.current_session_context_class">thread</property
>
</session-factory>
Pom.xml
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.6.15.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.mysql/mysql-connector-j -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.32</version>
</dependency>
Users.java
import javax.persistence.Entity;
import javax.persistence.*;
@Entity
@Table(name="users")
class Users1 {
}
App.java
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.Session;
import javax.*;
public class App
{
public static void main( String[] args )
{
SessionFactory factory=new
Configuration().configure("hibernate.cfg.xml").addAnnotatedClass(Users1.class).buildS
essionFactory();
Session session =factory.getCurrentSession();
try
{
Users1 user=new Users1(“bharti123","2121","Bharti","Dutta");
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
}
finally
{
session.close();
factory.close();
}
}
• Configuration:
• Configuration is a class which is present in org.hibernate.cfg package.
It activates Hibernate framework. It reads both configuration file and
mapping files.
SessionFactory:
• SessionFactory is an Interface which is present in org.hibernate
package and it is used to create Session Object.
• It is immutable and thread-safe in nature.
Session:
Session is an interface which is present in org.hibernate package.
Session object is created based upon SessionFactory object i.e. factory.
It opens the Connection/Session with Database software through
Hibernate Framework.
It is a light-weight object and it is not thread-safe.
Session object is used to perform CRUD operations.
Hibernate Lifecycle
• Once the object is connected with the Hibernate Session then the
object moves into the Persistent State. In this state. each object
represents one row in the database table. Therefore, if we make any
changes in the data then hibernate will detect these changes and make
changes in the database table.
• Following are the methods given for the persistent state:
• session.persist(e);
• session.save(e);
• session.saveOrUpdate(e);
• session.update(e);
• session.merge(e);
• session.lock(e);
• Example:
// Transient State
Employee e = new Employee("Neha Shri Rudra", 21, 180103);
//Persistent State session.save(e);
State 3: Detached State
• For converting an object from Persistent State to Detached State, we either have to
close the session or we have to clear its cache. As the session is closed here or the
cache is cleared, then any changes made to the data will not affect the database table.
Whenever needed, the detached object can be reconnected to a new hibernate session.
To reconnect the detached object to a new hibernate session, we will use the following
methods as follows:
• merge()
• update()
• load()
• refresh()
• save()
• update()
• Following are the methods used for the detached state :
• session.detach(e);
• session.evict(e);
• session.clear();
• session.close();
• Example
// Transient State
Employee e = new Employee("Neha Shri Rudra", 21, 180103);
// Persistent State session.save(e);
// Detached State session.close();
State 4: Removed State