简体   繁体   English

如何在Hibernate中使用DAO方法从数据库中检索,更新,删除数据

[英]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.

相关问题 如何使用休眠和春季DAO模式在数据库中插入批量数据 - How to insert bulk data in database using hibernate and spring DAO pattern 如何使用 Hibernate 从 Sql 数据库中更新、删除 object? - how to update, delete an object from Sql database using Hibernate? 如何使用Hibernate从数据库中检索列的Java数据类型? - How to retrieve the java data types of columns from the database using Hibernate? Room DAO 无法从数据库检索数据 - Room DAO fails to retrieve data from Database 使用休眠模式将Json数据存储到Mysql 5.7数据库/从Mysql 5.7数据库获取Json数据 - Store/Retrieve Json data to/from Mysql 5.7 database using hibernate hibernate如何从现有数据库视图中检索数据? - How hibernate retrieve data from existing database view? 如何在DAO中检索关联数据? - how to retrieve association data in DAO? 如何通过使用Spring 2.0和Hibernate SpringMVC从数据库中检索记录 - how to retrieve the record from database by using spring 2.0 with hibernate springMVC 如何从数据库中检索实例并使用struts2和hibernate显示它? - How to retrieve an instance from the database and display it using struts2 and hibernate? 如何使用Hibernate检查值并从数据库中检索它(如果存在)? - How to check the value and retrieve it from database if it exists using Hibernate?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM