0% found this document useful (0 votes)
3 views16 pages

Labsheet 5

The document outlines a Hibernate program using Maven for processing student marks, detailing the necessary components such as Session, SessionFactory, and Transaction. It provides code examples for creating a Student POJO class, a Hibernate configuration file, and an application file to save student data to a MySQL database. Additionally, it specifies required dependencies and configurations for successful database connectivity and operations.

Uploaded by

tufitufail247
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views16 pages

Labsheet 5

The document outlines a Hibernate program using Maven for processing student marks, detailing the necessary components such as Session, SessionFactory, and Transaction. It provides code examples for creating a Student POJO class, a Hibernate configuration file, and an application file to save student data to a MySQL database. Additionally, it specifies required dependencies and configurations for successful database connectivity and operations.

Uploaded by

tufitufail247
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

LabSheet 5

Hibernate Program using Annotation(Maven)

Problem Statement : Student mark processing using Hibernate with Maven

Session used to get physical connection with DB.

It is the main tool used to communicate with Hibernate.

It provides an API enabling us to create .read ,update and delete persistent objects
(Persistent objects are instances of POJO classes that you create that represent rows in the table in
the database.).

Session factory-it is an interface, used to create session object.(instance of hibernate),it handles


important DB connectivity tasks.

Commit: It automatically flushes the session. (It is used to permanently saves the changes done in
transaction in tables or databases.)

Transaction: It represents a unit of work performed within DBMS against DB.


Install JBoss Tools
Include following dependencies available in mvnrepository.com in pom.xml file

1. Hibernate core 4.1.6


2. mysqlconnector
To create a POJO(Module) class file.

Student.java

package com.pu.test123;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Student {
@Id
private int roll;
private String name;
private int marks;
public int getRoll() {
return roll;
}
public void setRoll(int roll) {
this.roll = roll;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMarks() {
return marks;
}
public void setMarks(int marks) {
this.marks = marks;
}

}
To create hibernate configuration file
Hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?>


<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-
3.0.dtd">

<hibernate-configuration>
<session-factory>
<property
name="hibernate.connection.driver_class">com.mysql.jdbc.Driv
er</property>
<property
name="hibernate.connection.url">jdbc:mysql://localhost:3306/
god?characterEncoding=latin1</property>
<property
name="hibernate.connection.username">root</property>
<property
name="hibernate.connection.password">admin123</property>
<property
name="hibernate.dialect">org.hibernate.dialect.MySQLDialect<
/property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update </property>
<mapping resource="com.pu.student.hbm.xml" />
</session-factory>
</hibernate-configuration>

Open App file and edit

App.java

package com.pu.test123;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
Student student = new Student();
student.setRoll(1);
student.setName("god");
student.setMarks(100);
//1. create Configuration object
Configuration configuration=new
Configuration().configure().addAnnotatedClass(Student.class)
;
//2. create Session Factroy object
SessionFactory
sessionFactory=configuration.buildSessionFactory();
//3. Create Session object
Session session=sessionFactory.openSession();
//4. Begin your transaction
Transaction transaction=session.beginTransaction();
//5.Save your object to database
session.save(student);
//6/ Commit your transaction
transaction.commit();
session.close();
sessionFactory.close();
}
}

You might also like