Updated April 1, 2023
Introduction to Hibernate Persist
Hibernate Persist is used to save an entity or a record. There are other methods such as save and update. The persist method is used to change the state of transient entity form to a persisted or managed state. Persist is the method offered by EntityManager of JPA and is also inherited by hibernate Session. A persistEvent is triggered by the persist method which is again handled by a hibernate event listener, Default Persist Event Listener. The persist method does not provide a guarantee that the value assigned to the instance of persistence immediately, at the flush time the assignment might happen.
Syntax:
writer a = new writer();
a.setFirst_name("JACK");
a.setLast_name("ROSE");
session.persist (a);
Creating a new entity object, the entity object gets into its lifecycle state without mapping any record of the database.
From the syntax, ‘a’ has been transitioned to persist state from transient state. In the persistent context, the object is inserted. Though, not saved to the database. The return type of persist is void. The passed object in place is on via changing its subject. The persisted object is referenced by the ‘a’
How Persist in Hibernate Works?
One needs to attach an entity to the context of persistence so as to make it become managed or get persisted in the database. Using either Hibernate save or Java persist method, we can do it. Both are similar with minor differences.
Points to Remember:
- The persistent method return type is void.
- Outside of transaction, this method does not save changes to the database.
- Persistence object exception is thrown unto session.persist(), as this is not permitted.
- Both JPA and Hibernate support the persist method.
- First you need to download and install the hibernate package and the install MySQL database on your operating system.
Create a Maven project, which means, the maven dependencies and
XML Code:
pox.ml typed file will be created. pom.xl has the below code:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hibernate</groupId>
<artifactId>HibernatePersist</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Hibernate Persist Example</name>
<packaging>jar</packaging>
</project>
Now, we need to build the application by developing. There are three steps involved in doing this: Table and database creation, Maven dependencies, and Creating a Java class.
Database Creation:
CREATE DATABASE IF NOT EXISTS persistdb;
USE persistdb;
CREATE TABLE employee (
id INT(50) NOT NULL AUTO_INCREMENT,
name VARCHAR(200) DEFAULT NULL,
designation VARCHAR(200) DEFAULT NULL,
department VARCHAR(200) DEFAULT NULL,
PRIMARY KEY (id)
);
Maven Dependencies:
The dependencies for hibernate framework are specified and the rest of the dependencies are solved by Maven. The below code is the updated version of the file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hibernate</groupId>
<artifactId>HibernatePersist</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Hibernate Persist Example</name>
<packaging>jar</packaging>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.3.7.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
</build>
</project>
Example to Implement Hibernate Persist
Example of hibernate persist is given below:
package com.educba.hibernate.main;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import com.educba.hibernate.model.emp;
import com.educba.hibernate.util.HibernateUtil;
public class PersistExp {
public static void main(String[] args) {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
//Using a transition, let's see hibernate example
Session sess = sessionFactory.openSession();
Transaction trans2 = sess.beginTransaction();
Employee emp2 = PersistExp.getTestEmp();
sess.persist(emp2);
System.out.println("Persist is called");
empp2.setName("Kumar");
// This will be saved and updated in database as well
System.out.println("Name of the employee is updated");
System.out.println("With the transaction id, the name of employee has been called="+empp2.getId()+", address="+emp2.getAddress().getId());
tans.commit();
System.out.println("*****");
//All the resources re closed
sessionFactory.close();
}
}
Output:
Conclusion
In the database, the persistent object exists. These persistent objects are managed by hibernate for persistence objects. The representation of the hibernate database is upto date when the changes are to be committed in the application.
Recommended Articles
This is a guide to Hibernate Persist. Here we also discuss the Introduction and how persist in hibernate works along with an example and its code implementation. You may also have a look at the following articles to learn more –