Ashok
Ashok

Reputation: 79

How to retrieve,update,delete data from database using DAO method in Hibernate

How to retrieve,update,delete data from database using DAO method in Hibernate.

My DAO look like this:

package com.sample.common.impl;

import java.util.List;
import com.sample.common.Employee;

public interface EmployeeDao {
   public List<Employee> getAllEmployee();     
   public void updateEmployee(Employee emp);
   public void deleteEmployee(Employee emp);
}

My implementation class look like this:

package com.sample.common.impl;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.SessionFactory;
import com.sample.common.Employee;

public class EmployeeDaoImpl implements EmployeeDao {
private SessionFactory sessionFactory;

public List<Employee> getAllEmployee() {        
    return null;
}

 public void updateEmployee(Employee emp) {     

}

public void deleteEmployee(Employee emp) {      

}   
}

How to create the query for select,update and delete. can you please suggest any possible solution

Upvotes: 0

Views: 4056

Answers (1)

Darshan
Darshan

Reputation: 2144

You have to update the code as below

public void deleteEmployee(Employee emp) {
        Session session = sessionFactory.getCurrentSession();
        session.delete(emp);
        logger.debug(emp.getClass());
    }

public void updateEmployee(Employee emp) {
        Session session = sessionFactory.getCurrentSession();
        session.update(emp);
        logger.debug(emp.getClass());
    }

public List<Employee> getAllEmployee(){  
  String query ="SELECT e FROM EMPLOYEE e";
  List<Employee> empList = session.createQuery(query);     
  return empList;
}

Hope this stuff works.

Upvotes: 1

Related Questions