[英]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. 如何在Hibernate中使用DAO方法从数据库中检索,更新,删除数据。
My DAO look like this: 我的DAO看起来像这样:
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
你能建议任何可能的解决方案吗
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. 希望这个东西有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.