Harshith is a manager for an IT (Information Technology) Company who maintains the records of his employees. Store the Employee details with the following attributes Empid, Ename, Dept, Age, Salary and use sequence generator class for Empid. Develop a separate hibernate applications for the following(should not use Hibernate Query Language).
The tasks need to be performed are stated below
a. Retrieve the details of an employee whose Empid =1001
b. Update the salary of an employee whose Empid = 1004 with Average of all the Employee Salaries
c. Delete an employee record from the table whose Salary is Minimum.
Dont forget to add jar files to class path
Video explaining the code and process in telugu language
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- connection details -->
<property name = "connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name = "connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
<property name="connection.user">system</property>
<property name= "connection.password">nikhil4u</property>
<!-- Hibernate details -->
<property name="show_sql">false</property>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.Oracle12cDialect</property>
<!-- mapping resources -->
<mapping resource = "employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
employee.hbm.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"https://hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="skill6.Employee" table="skill6Employee">
<id name = "Empid" type = "int" column = "Empid"/>
<property name = "Ename" column = "Ename" type = "string"/>
<property name = "department" column = "department" type = "string"/>
<property name = "age" column = "age" type = "int"/>
<property name = "salary" column = "salary" type = "int"/>
</class>
</hibernate-mapping>
skill6.java
package skill6;
import java.util.List;
import java.util.Scanner;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class skill6 {
public static Session getSession() throws HibernateException {
String cfg = "hibernate.cfg.xml";
SessionFactory sessionFactory = new Configuration().configure(cfg).buildSessionFactory();
return sessionFactory.openSession();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
boolean exit = false;
while (!exit) {
System.out.println("1. Get employee details");
System.out.println("2. Update employee salary");
System.out.println("3. Delete employee with least salary");
System.out.println("4. Exit the program");
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println("Please enter employee id");
int employeeId = sc.nextInt();
get_employee_details(employeeId);
break;
case 2:
System.out.println("Please enter employee id");
int employId = sc.nextInt();
update_employee_details(employId);
break;
case 4:
exit = true;
break;
case 3:
delete_minimum_salary_employees();
break;
default:
System.out.println("Please Try Again");
break;
}
}
sc.close();
}
public static void get_employee_details(int Empid) {
try (Session session = getSession()) {
CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<Employee> criteria = builder.createQuery(Employee.class);
Root<Employee> employeeRoot = criteria.from(Employee.class);
criteria.select(employeeRoot);
criteria.where(builder.equal(employeeRoot.get("Empid"), Empid));
List<Employee> employeeList = session.createQuery(criteria).getResultList();
if (employeeList.isEmpty()) {
System.out.println("No employee found");
} else {
for (Employee temp : employeeList) {
System.out.println("Details of employee with empid " + Empid + " are");
System.out.println("Employee name " + temp.getEname());
System.out.println("Employee Department " + temp.getDepartment());
System.out.println("Employee age " + temp.getAge());
System.out.println("Employee Salary " + temp.getSalary());
}
}
} catch (Exception e) {
System.out.println("Something went wrong");
}
}
public static void update_employee_details(int Empid) {
try (Session session = getSession()) {
List<Double> average = session.createQuery("select avg(salary) from Employee").getResultList();
Transaction tx = session.beginTransaction();
Integer avg = average.get(0).intValue();
int updatedEntities = session.createQuery("update Employee e set e.salary=:n where e.Empid=:i")
.setParameter("n", avg).setParameter("i", Empid).executeUpdate();
System.out.println(updatedEntities + " Entities updated succesfully");
tx.commit();
} catch (Exception e) {
System.out.println(e);
System.out.println("Something went wrong");
}
}
public static void delete_minimum_salary_employees() {
try (Session session = getSession()) {
List<Integer> minimum = session.createQuery("select min(salary) from Employee").getResultList();
Transaction tx = session.beginTransaction();
Integer min = minimum.get(0);
int updatedEntities = session.createQuery("delete from Employee e where e.salary=:i").setParameter("i", min)
.executeUpdate();
System.out.println(updatedEntities + " Entities deleted succesfully");
tx.commit();
} catch (Exception e) {
System.out.println(e);
System.out.println("Something went wrong");
}
}
}
Employee.java
package skill6;
public class Employee {
private int Empid;
public int getEmpid() {
return Empid;
}
public void setEmpid(int empid) {
Empid = empid;
}
public String getEname() {
return Ename;
}
public void setEname(String ename) {
Ename = ename;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
private String Ename;
private String department;
private int age;
private int salary;
}
sql statments to insert into table
INSERT INTO SKILL6EMPLOYEE (EMPID, ENAME, DEPARTMENT, AGE, SALARY) VALUES ('1920', 'nikhil', 'cse', '20', '200000');
INSERT INTO SKILL6EMPLOYEE (EMPID, ENAME, DEPARTMENT, AGE, SALARY) VALUES ('1943', 'kishan', 'cse', '20', '100000');
INSERT INTO SKILL6EMPLOYEE (EMPID, ENAME, DEPARTMENT, AGE, SALARY) VALUES ('1925', 'goutham', 'cse', '20', '126000');
INSERT INTO SKILL6EMPLOYEE (EMPID, ENAME, DEPARTMENT, AGE, SALARY) VALUES ('1927', 'vyshnav', 'cse', '19', '152000');
INSERT INTO SKILL6EMPLOYEE (EMPID, ENAME, DEPARTMENT, AGE, SALARY) VALUES ('1904', 'mukund', 'cse', '20', '144000');
INSERT INTO SKILL6EMPLOYEE (EMPID, ENAME, DEPARTMENT, AGE, SALARY) VALUES ('1955', 'anirudh', 'cse', '20', '178000');
commit;