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();
Address address1
= componentMappingExample.addAddress(
"ByepassRoad"
,
"Chennai"
,
"TN"
,
"600028"
);
Integer studentId
= componentMappingExample.addStudent(
"Ashwin"
,
"Kumar"
,
10
, address1);
Address address2
= componentMappingExample.addAddress(
"Pattinapakkam"
,
"Chennai"
,
"TN"
,
"600028"
);
Integer student2
= componentMappingExample.addStudent(
"Ruchitaa"
,
"Agarwal"
,
8
, address2);
componentMappingExample.listStudents();
componentMappingExample.updateStudent(studentId,
"Nikilesh"
);
componentMappingExample.listStudents();
}
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;
}
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();
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;
}
public
void
listStudents()
{
Session session = factory.openSession();
Transaction tx =
null
;
try
{
tx = session.beginTransaction();
List students
= session.createQuery(
"FROM Student"
)
.list();
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();
}
}
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();
}
}
}