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

question1hibernate

Uploaded by

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

question1hibernate

Uploaded by

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

1.Filename:- Student.cfg.

xml

<!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="dialect">org.hibernate.dialect.MySQL8Dialect</property>
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/Hibernetdemo</property>
<property name="connection.username">root</property>
<property name="connection.password">Sakshi@213035</property>
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<mapping class="Beans.Student" />
</session-factory>
</hibernate-configuration>

2.Filename:-Main.java

package Beans;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
Configuration cfg = new Configuration();
cfg.configure("Student.cfg.xml");
SessionFactory sf=cfg.buildSessionFactory();
Session s=sf.openSession();
Transaction tx=s.beginTransaction();
// Student std=new Student();
// std.setStudentId(3);
// std.setGender("male");
// std.setFirstName("ARJUN");
// std.setEmail("Arjun@123");
// std.setLastName("Sharma");
// std.setPhone("9878546123");
//
// std.setDOB(LocalDate.ofEpochDay(2003-06-20));
//
// s.save(std);
//
// tx.commit();
Student std1=s.get(Student.class,3);
if(std1!=null){
System.out.println(std1.toString());
}
else{
System.out.println("NO Student Found With Id 4");
}

}
}

3.Filename:-Student.java

package Beans;

import javax.persistence.Entity;
import javax.persistence.Id;
import java.time.LocalDate;

@Entity
public class Student {
@Id
int StudentId;
String FirstName;
String Gender;
String LastName;
String Email;
String Phone;
LocalDate DOB;

public int getStudentId() {


return StudentId;
}

public void setStudentId(int studentId) {


StudentId = studentId;
}

public String getFirstName() {


return FirstName;
}

public void setFirstName(String firstName) {


FirstName = firstName;
}

public String getGender() {


return Gender;
}

public void setGender(String gender) {


Gender = gender;
}

public String getLastName() {


return LastName;
}

public void setLastName(String lastName) {


LastName = lastName;
}

public String getEmail() {


return Email;
}

public void setEmail(String email) {


Email = email; }
public String getPhone() {
return Phone;
}

public void setPhone(String phone) {


Phone = phone;
}

public LocalDate getDOB() {


return DOB;
}

public void setDOB(LocalDate DOB) {


this.DOB = DOB;
}

@Override
public String toString() {
return "Student{" +
"StudentId=" + StudentId +
", FirstName='" + FirstName + '\'' +
", Gender='" + Gender + '\'' +
", LastName='" + LastName + '\'' +
", Email='" + Email + '\'' +
", Phone='" + Phone + '\'' +
", DOB=" + DOB +
'}';
}
}

1.Filename:-Hibernate.cfg.xml

<!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="dialect">org.hibernate.dialect.MySQL8Dialect</property>
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/Hibernetdemo</property>
<property name="connection.username">root</property>
<property name="connection.password">Sakshi@213035</property>
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping class="Beans.Employee"/>
</session-factory>
</hibernate-configuration>

2.Filename:-Employee.java

import javax.persistence.*;
import java.util.Date;
@Entity
@Table(name="emp_data")

public class Employee {


@Id //primary key
@Column(name="emp_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
int emp_id;

@Column(name="emp_name")
String name;

@Column( length=100,name="emp_add")
String address;
@Column(name="emp_age")

@Transient//jo column nahi chhaiye uske liye


int age;
@Column(name="emp_dateofjoin")
@Temporal(TemporalType.DATE)
Date dateofjoin;
@Column(name="emp_working")

boolean isWorking;

public int getEmp_id() {


return emp_id;
}
public void setEmp_id(int emp_id) {
this.emp_id = emp_id;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public String getAddress() {


return address;
}

public void setAddress(String address) {


this.address = address;
}

public int getAge() {


return age;
}

public void setAge(int age) {


this.age = age;
}

public Date getDateofjoin() {


return dateofjoin;
}

public void setDateofjoin(Date dateofjoin) {


this.dateofjoin = dateofjoin;
}

public boolean isWorking() {


return isWorking;
}
public void setWorking(boolean working) {
isWorking = working;
}

3.Filename:-Main.java

package Beans;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import java.util.Date;

public class Main {


public static void main(String[] args) {
Configuration cfg=new Configuration();
cfg.configure("Hibernate.Cfg.xml");
SessionFactory sessionFactory=cfg.buildSessionFactory();
Session session=sessionFactory.openSession();
Transaction tx= session.beginTransaction();
Employee emp=new Employee();
emp.setAddress("Delhi");
emp.setAge(18);
emp.setEmp_id(37);
emp.setName("Navya");
emp.setWorking(true);
emp.setDateofjoin(new Date());
System.out.println(new Date());
Employee emp_name=session.load(Employee.class,2);
System.out.println(emp_name.name);

session.save(emp);
tx.commit();
}
}

You might also like