Abhishek3 1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Experiment 3.

Student Name: Abhishek UID: 22MCC20094


Section/Group: 22MCD-2A Subject/Code:22CAP686
Semester: 2ND Date:08-05-2023
1. Aim/Overview of the practical:
Write the simple program in Hibernate.

Task to be done:

2. Code for experiment/practical:

Here is a simple program in Hibernate that demonstrates how to create an entity, persist it to the

database, and retrieve it: package com.mycompany;

import java.io.Serializable; import


org.hibernate.Session; import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration; import
org.hibernate.service.ServiceRegistry; import
org.hibernate.service.ServiceRegistryBuilder;

/**
* A sample program that demonstrates how to perform simple CRUD operations * with Hibernate
framework. * @author www.codejava.net
*
*/ public class ContactManager { public static void
main(String[] args) {
// loads configuration and creates a session factory
Configuration configuration = new Configuration().configure(); ServiceRegistryBuilder registry
= new ServiceRegistryBuilder();
registry.applySettings(configuration.getProperties());
ServiceRegistry serviceRegistry = registry.buildServiceRegistry();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
// opens a new session from the session factory Session session
= sessionFactory.openSession();
session.beginTransaction();
// persists two new Contact objects
Contact contact1 = new Contact("Nam", "hainatuatgmail.com", "Vietnam",
"0904277091"); session.persist(contact1);
Contact contact2 = new Contact("Bill", "billatgmail.com", "USA", "18001900");
Serializable id = session.save(contact2);
System.out.println("created id: " + id);

// loads a new object from database


Contact contact3 = (Contact) session.get(Contact.class, new Integer(1)); if
(contact3 == null) {
System.out.println("There is no Contact object with id=1");
} else {
System.out.println("Contact3's name: " + contact3.getName());
}

// loads an object which is assumed exists


Contact contact4 = (Contact) session.load(Contact.class, new Integer(4));
System.out.println("Contact4's name: " + contact4.getName());

// updates a loaded instance of a Contact object


Contact contact5 = (Contact) session.load(Contact.class, new Integer(5));
contact5.setEmail("info1atcompany.com"); contact5.setTelephone("1234567890");
session.update(contact5);
// updates a detached instance of a Contact object
Contact contact6 = new Contact(3, "Jobs", "jobsatapplet.com", "Cupertino", "0123456789");
session.update(contact6);

// deletes an object
Contact contact7 = new Contact();
contact7.setId(7); session.delete(contact7);
// deletes a loaded instance of an object
Contact contact8 = (Contact) session.load(Contact.class, new Integer(8)); session.delete(contact8);

// commits the transaction and closes the session session.getTransaction().commit();


session.close();

}
}
This program defines an Employee entity class with three fields: id, name, and department. It uses
Hibernate to persist an instance of this class to a database, and then retrieves and prints the
persisted data.
Working with Hibernate transaction –
session.beginTransaction();
try {
session.persist(Object); session.update(Object);
session.delete(Object); } catch (Exception ex)
{ session.getTransaction().rollback();
} session.getTransaction().commit();
session.close();

3. Result/Output/Writing Summary:

Learning outcomes (What I have learnt):

1. Learned how to create simple hibernate program Evaluation Grid:


Sr. No. Parameters Marks Obtained Maximum Marks

1. Demonstration and Performance 5

2. Worksheet 10

3. Lab Quiz 5

You might also like