0% found this document useful (0 votes)
3 views

Assignment Hibernate

Uploaded by

sunitajadhav2612
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Assignment Hibernate

Uploaded by

sunitajadhav2612
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Hibernate

1.Student.java

import javax.persistence.*;
@Entity
@Table(name = "student_table") public
class Student {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id") private
long studid;
@Column(name = "studentname")
private String studName;
@Column(name = "fee") private
double fee;
public Student(long studid, String studName, double fee) {
this.studid = studid; this.studName =
studName; this.fee = fee;
}
public Student() {
}
public long getStudid() {
return studid;
}
public void setStudid(long studid) {
this.studid = studid;
}
public String getStudName() {
return studName;
}
public void setStudName(String studName) {
this.studName = studName;
}
public double getFee() {
return fee;
}
public void setFee(double fee) {
this.fee = fee;

SM22IF032
}
@Override
public String toString() { return
"Student{" +
"studid=" + studid +
", studName='" + studName + '\'' +
", fee=" + fee +
'}';
}
}

--------------------------------------------------------------

2. AppIntilizer.java

import org.hibernate.Session; import


org.hibernate.Transaction; import
org.hibernate.query.Query; import
java.util.List; public class AppInitializer {
public static void main(String[] args) {
Student student = new Student(2, "Anne", 2000);
saveStudent(student); getAllStudents();
updateStudent("Raja", 6000, 2); deleteStudent(2);
findStudent(1);
}
public static void saveStudent(Student student) { try
(Session session =
HibernateUtil.getSessionFactory().openSession()) { Transaction
transaction = session.beginTransaction();
session.save(student); transaction.commit();
}
}
private static void getAllStudents() {
try (Session session =
HibernateUtil.getSessionFactory().openSession()) {
Query<Student> query = session.createQuery("FROM Student",
Student.class);
List<Student> students = query.list();
System.out.println(students);

SM22IF032
}
}
public static void updateStudent(String name, double fee, long id) {
try (Session session =
HibernateUtil.getSessionFactory().openSession()) {
Student selectStudent = session.find(Student.class, id); if
(selectStudent != null) {
selectStudent.setStudName(name);
selectStudent.setFee(fee);
Transaction transaction = session.beginTransaction();
session.update(selectStudent);
transaction.commit();
} else {
System.out.println("Can't Find Records");
}
}
}
public static void deleteStudent(long id) {
try (Session session =
HibernateUtil.getSessionFactory().openSession()) {
Student selectStudent = session.find(Student.class, id); if
(selectStudent != null) {
Transaction transaction = session.beginTransaction();
session.delete(selectStudent);
transaction.commit();
} else {
System.out.println("Can't Find Records");
}
}
}
public static void findStudent(long id) {
try (Session session =
HibernateUtil.getSessionFactory().openSession()) {
Student selectStudent = session.find(Student.class, id); if
(selectStudent != null) {
System.out.println(selectStudent);
} else {
System.out.println("Can't Find Records");
}
}

SM22IF032
}
}

----------------------------------------------------------------

3. HibernateUtil.java

import org.hibernate.SessionFactory; import


org.hibernate.boot.Metadata; import
org.hibernate.boot.MetadataSources; import
org.hibernate.boot.registry.StandardServiceRegistry; import
org.hibernate.boot.registry.StandardServiceRegistryBuilder; public class
HibernateUtil {
private static StandardServiceRegistry standardServiceRegistry; private
static SessionFactory sessionFactory; static { try {
if (sessionFactory == null) {
standardServiceRegistry = new
StandardServiceRegistryBuilder()
.configure("hibernate.cfg.xml")
.build();
MetadataSources metadataSources = new
MetadataSources(standardServiceRegistry)
.addAnnotatedClass(Student.class);
Metadata metadata =
metadataSources.getMetadataBuilder().build();
sessionFactory =
metadata.getSessionFactoryBuilder().build();
}
} catch (Exception e) { if
(standardServiceRegistry != null) {

StandardServiceRegistryBuilder.destroy(standardServiceRegistry)
;}
e.printStackTrace();
}
}

SM22IF032
public static SessionFactory getSessionFactory() {
return sessionFactory;
}

-----------------------------------------------------------------

4. hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"

"http://www.hibernate.org/dtd/hibernate-configuration-
3.0.dt d">
<hibernate-configuration>
<session-factory>
<property
name="connection.url">jdbc:mysql://localhost:3306/ecomm e
rce_cart?createDatabaseIfNotExist=true</property>
<property
name="connection.driver_class">com.mysql.cj.jdbc.Driver</p
roperty>
<property name="connection.username">root</property>
<property name="connection.password">root123</property>
<property
name="dialect">org.hibernate.dialect.MySQL57Dialect</prop erty>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
</session-factory>
</hibernate-configuration>

SM22IF032
-------------------------------------------------------------

SM22IF032
SM22IF032

You might also like