import com.gfg.componentmapping.pojo.Address;
import com.gfg.componentmapping.pojo.Student;
import java.util.Iterator;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class ComponentMappingPatternOfStoringData {
private static SessionFactory factory;
public static void main(String[] args)
{
try {
factory = new Configuration()
.configure()
.buildSessionFactory();
}
catch (Throwable ex) {
System.err.println(
"Failed to create sessionFactory object."
+ ex);
throw new ExceptionInInitializerError(ex);
}
ComponentMappingPatternOfStoringData
componentMappingExample
= new ComponentMappingPatternOfStoringData();
// Address1
Address address1
= componentMappingExample.addAddress(
"ByepassRoad", "Chennai", "TN", "600028");
// Add student records in the database
Integer studentId
= componentMappingExample.addStudent(
"Ashwin", "Kumar", 10, address1);
// Address2
Address address2
= componentMappingExample.addAddress(
"Pattinapakkam", "Chennai", "TN", "600028");
// Add another student record in the database
Integer student2
= componentMappingExample.addStudent(
"Ruchitaa", "Agarwal", 8, address2);
// List down all the students
componentMappingExample.listStudents();
// Update student's name as a sample
componentMappingExample.updateStudent(studentId,
"Nikilesh");
// List down all the students. We can see the
// updated value for the first student
componentMappingExample.listStudents();
}
// Method to add an address record in the database
public Address addAddress(String street, String city,
String state, String zipcode)
{
Session session = factory.openSession();
Transaction tx = null;
Integer addressID = null;
Address address = null;
try {
tx = session.beginTransaction();
address
= new Address(street, city, state, zipcode);
addressID = (Integer)session.save(address);
tx.commit();
}
catch (HibernateException e) {
if (tx != null)
tx.rollback();
e.printStackTrace();
}
finally {
session.close();
}
return address;
}
// Method to add a student record in the database
public Integer addStudent(String fname, String lname,
int salary, Address address)
{
Session session = factory.openSession();
Transaction tx = null;
Integer studentID = null;
try {
tx = session.beginTransaction();
// By using Student constructor, we can see the
// fields
Student student = new Student(fname, lname,
salary, address);
studentID = (Integer)session.save(student);
tx.commit();
}
catch (HibernateException e) {
if (tx != null)
tx.rollback();
e.printStackTrace();
}
finally {
session.close();
}
return employeeID;
}
// Method to list all the student detail
public void listStudents()
{
Session session = factory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
// Take the data from Student table
List students
= session.createQuery("FROM Student")
.list();
// Iterate the details and display them
for (Iterator iterator = students.iterator();
iterator.hasNext();) {
Student student = (Student)iterator.next();
System.out.print("First Name: "
+ student.getFirstName());
System.out.print(" Last Name: "
+ student.getLastName());
System.out.println(" Grade: "
+ student.getGrade());
Address add = student.getAddress();
System.out.println("Address ");
System.out.println("\tStreet: "
+ add.getStreetName());
System.out.println("\tCity: "
+ add.getCityName());
System.out.println("\tState: "
+ add.getStateName());
System.out.println("\tZipcode: "
+ add.getZipCode());
}
tx.commit();
}
catch (HibernateException e) {
if (tx != null)
tx.rollback();
e.printStackTrace();
}
finally {
session.close();
}
}
// Method to update name for a given student
public void updateStudent(Integer studentId,
String firstName)
{
Session session = factory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Student student = (Student)session.get(
Student.class, studentId);
student.setFirstName(firstName);
session.update(student);
tx.commit();
}
catch (HibernateException e) {
if (tx != null)
tx.rollback();
e.printStackTrace();
}
finally {
session.close();
}
}
}