CRUD Operations Using Hibernate
CRUD Operations Using Hibernate
C -> Create/Insert
R -> Retrieve
U -> Update
D -> Delete
Given below are the examples that illustrate the use of Hibernate to perform
CRUD operations. All the examples use MySQL for database management and
‘student_info’ as a sample database.
Example Project
SessionFactoryProvider.java:
Video will
play after…
https://www.geeksforgeeks.org/crud-operations-using-hibernate/ 1/14
3/21/24, 8:45 AM CRUD Operations using Hibernate - GeeksforGeeks
Java
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
Student.java:
Java
import javax.persistence.*;
×
@Entity
public class Student {
@Id
private int id;
private String name; Video will
private int std; play after…
public Student() {
}
https://www.geeksforgeeks.org/crud-operations-using-hibernate/ 2/14
3/21/24, 8:45 AM CRUD Operations using Hibernate - GeeksforGeeks
hibernate.cfg.xml:
XML
https://www.geeksforgeeks.org/crud-operations-using-hibernate/ 3/14
3/21/24, 8:45 AM CRUD Operations using Hibernate - GeeksforGeeks
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</pr
<mapping class="beans.Student"></mapping>
</session-factory>
</hibernate-configuration>
hibernate.cfg.xml:
XML
Video will
play after…
https://www.geeksforgeeks.org/crud-operations-using-hibernate/ 4/14
3/21/24, 8:45 AM CRUD Operations using Hibernate - GeeksforGeeks
Create.java:
Java
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import utilities.SessionFactoryProvider;
sessionFactory.close();
}
} ×
https://www.geeksforgeeks.org/crud-operations-using-hibernate/ 5/14
3/21/24, 8:45 AM CRUD Operations using Hibernate - GeeksforGeeks
The following two methods are used to retrieve the data from the database:
get() load()
Retrieve.java:
https://www.geeksforgeeks.org/crud-operations-using-hibernate/ 6/14
3/21/24, 8:45 AM CRUD Operations using Hibernate - GeeksforGeeks
Java
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import utilities.SessionFactoryProvider;
sessionFactory.close();
}
JavaThe
Arrays Java Strings
following Javawill
details OOPsbe fetched
Java Collection Java database:
from the 8 Tutorial Java Multithreading Java Exception H
If an object doesn’t exist in database, get() returns null whereas load() throws
ObjectNotFoundexception.
Retrieve.java:
Java
×
package crudOperations;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import beans.Student;
Video will
import utilities.SessionFactoryProvider;
play after…
https://www.geeksforgeeks.org/crud-operations-using-hibernate/ 7/14
3/21/24, 8:45 AM CRUD Operations using Hibernate - GeeksforGeeks
{
SessionFactory sessionFactory=SessionFactoryProvider.provideSessionFactory
Session session=sessionFactory.openSession();
sessionFactory.close();
}
}
Output:
Update.java:
Java
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import utilities.SessionFactoryProvider;
sessionFactory.close();
}
https://www.geeksforgeeks.org/crud-operations-using-hibernate/ 8/14
3/21/24, 8:45 AM CRUD Operations using Hibernate - GeeksforGeeks
Delete.java:
Java
package crudOperations;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import beans.Student;
import utilities.SessionFactoryProvider;
sessionFactory.close();
https://www.geeksforgeeks.org/crud-operations-using-hibernate/ 9/14
3/21/24, 8:45 AM CRUD Operations using Hibernate - GeeksforGeeks
}
}
Output:
Feeling lost in the vast world of Backend Development? It's time for a change!
Join our Java Backend Development - Live Course and embark on an exciting
journey to master backend development efficiently and on schedule.
What We Offer:
Comprehensive Course
Expert Guidance for Efficient Learning
Hands-on Experience with Real-world Projects
Proven Track Record with 100,000+ Successful Geeks
Maximize your earnings for your published articles in Dev Scripter 2024! Showcase
expertise, gain recognition & get extra compensation while elevating your tech profile.
Previous Next
×
Share your thoughts in the comments Add Your Comment
Video will
Similar Reads play after…
https://www.geeksforgeeks.org/crud-operations-using-hibernate/ 10/14