删除工作经历的DAO方法
public void deleteEmpWork(int WorkId) {
String hql = "Delete From Emp_Work Where ID=?";
Query q = null;
Configuration config = new Configuration().configure();
SessionFactory sf = config.buildSessionFactory();
this.session = sf.openSession();
q = this.session.createQuery(hql);
q.setInteger(0, WorkId);
q.executeUpdate();
this.session.close();
}获取工作经历列表的方法
public ArrayList<Emp_Work> getEmpWorkByCondition(String empId) {
ArrayList<Emp_Work> work_list = null;
Query q = null;
String hql = "From Emp_Work Where empId=?";
Configuration config = new Configuration().configure();
SessionFactory sf = config.buildSessionFactory();
this.session = sf.openSession();
q = this.session.createQuery(hql);
q.setString(0, empId);
System.out.println("333");
work_list = (ArrayList<Emp_Work>) q.list();
System.out.println("444");
session.close();
return work_list;
}
public void deleteEmpWork(int WorkId) {
String hql = "Delete From Emp_Work Where ID=?";
Query q = null;
Configuration config = new Configuration().configure();
SessionFactory sf = config.buildSessionFactory();
this.session = sf.openSession();
q = this.session.createQuery(hql);
q.setInteger(0, WorkId);
q.executeUpdate();
this.session.close();
}获取工作经历列表的方法
public ArrayList<Emp_Work> getEmpWorkByCondition(String empId) {
ArrayList<Emp_Work> work_list = null;
Query q = null;
String hql = "From Emp_Work Where empId=?";
Configuration config = new Configuration().configure();
SessionFactory sf = config.buildSessionFactory();
this.session = sf.openSession();
q = this.session.createQuery(hql);
q.setString(0, empId);
System.out.println("333");
work_list = (ArrayList<Emp_Work>) q.list();
System.out.println("444");
session.close();
return work_list;
}
解决方案 »
- struts2 + MyEclipse8.5 + tomcat6.0 配置错误。java.lang.ClassNotFoundException
- struts2+spring+hibernate中保存对象出现空指针异常
- 在编写JSP页面时遇到的问题在 希望的路径... 中没有找到片段 {0}
- hibernate菜鸟请教
- 根据日期计算时间差 以秒为单位
- 倾家荡产:求解ObjectInputStream 的错误
- 有关实时更新信息的问题(Jsp)
- 我的weblogic7.0总是用一段时间就不能正常启动
- 请问那为大虾有tomcat的性能分析数据,例如支持多少连接,连续运行多长时间?
- 用websocket可以实现 用户调用某个接口的时候 推送消息到后台页面吗?java ssm框架
- spring和hibernate 主键表对象,一对多 set,出现session关闭的问题
- 求Axis2学习资料
Configuration config = new Configuration().configure();
SessionFactory sf = config.buildSessionFactory();
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;/**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42.html }.
*/
public class HibernateSessionFactory { /**
* Location of hibernate.cfg.xml file.
* Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file.
* The default classpath location of the hibernate config file is
* in the default package. Use #setConfigFile() to update
* the location of the configuration file for the current session.
*/
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION; private HibernateSessionFactory() {
}
/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session getAnotherSession() throws HibernateException{
if (sessionFactory == null) {
rebuildSessionFactory();
}
return sessionFactory.openSession();
}
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get(); if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
} return session;
} /**
* Rebuild hibernate session factory
*
*/
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
} /**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null); if (session != null) {
session.close();
}
} /**
* return session factory
*
*/
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
} /**
* return hibernate configuration
*
*/
public static Configuration getConfiguration() {
return configuration;
}
}
比如:做 添加时一个session . 删除时候一个session 。因为session是带缓存的所以不应该将一个session 来完成不同的操作
http://dev.21tx.com/2005/09/20/15730.html好像跑题了,呵呵..
可以能是由于你创建了太多 SessionFactory导致的 建议先改一下 获得Sesssion 的方式后再调试。
就是我 5楼提供的方法。
this.session = SessionFactory.getSession();
再试试?
那个类我直接拿来用就是可以的吧?